02 Basics
02 Basics
Kelas E & F
DDP2 Semester Genap 2020/2021
Fasilkom UI
Referensi
[Buku Thinkjava] A. Downey and C. Mayfield,
Think Java 2nd Edition.
https://greenteapress.com/wp/think-java-2e/
x = 5
y = "lima"
Tipe Data di Python
Ingat, semua data di python adalah object!
x = 5
y = "lima"
Memory
y : str
?
“lima”
x : int
5
Tipe Data di Python
Ingat, semua data di python adalah object!
x = 5
y = "lima"
Memory Memory
y : str str y
“lima”
x : int int
5 x
str int
5
“lima”
Catatan:
Ingat, tipe data primitif
bukan object di Java!
[Baca Buku Thinkjava, page 17-20]
Tipe Data di Java
Inisialisasi variable num yang sudah dideklarasikan
int num;
double velocity = 5.0;
num = 4;
String str = "halo";
velocity = velocity + 1.0;
str = "wow"; Memory
num: int velocity:
double
4 5.0
String
Ingat, String adalah tipe str
String
“halo”
[Baca Buku Thinkjava, page 17-20] Akan dihapus garbage collector java
[Baca Buku Liang, page 67]
Tipe Data Primitif Java
0 1 1 1 1 1 1 1 +127
0 0 0 0 0 0 0 1 +1
+
1 0 0 0 0 0 0 0
Sign 1 artinya integer -
Di representasi two’s complement, ini adalah -128
short hargaBuku = 50000;
hargaBuku += 1;
System.out.println(hargaBuku);
short hargaBuku = 50000;
hargaBuku += 1;
System.out.println(hargaBuku);
Compile Error:
Javac says “...error: incompatible types: possible lossy conversion
from int to short ...”
Double vs Float
The double type values are more accurate than the
float type values
1 6 d ig its
System.out.println(1.0 – 0.9);
// shows 0.0999999999999998
// not 0.1
●
Kita perlu import kelas Scanner
– import java.util.Scanner;
●
Buat sebuah object Scanner
– Scanner input = new Scanner(System.in);
●
Meminta input
– int value = input.nextInt();
Reading Numbers from the Keyboard
Keyword untuk menghidupkan
●
Kita perlu import kelas Scanner sebuah object dari sebuah
kelas.
– import java.util.Scanner;
●
Buat sebuah object Scanner
– Scanner input = new Scanner(System.in);
●
Meminta input
– int value = input.nextInt();
Object Scanner untuk parsing Sumber input: System.in
text streams artinya adalah standard input,
Method pada object Scanner, untuk input seperti keyboard
integer bertipe int
By the way ...
import java.util.Scanner; Object standard input
public class ProgramKu {
public static void main(String[] args) {
//membuat sebuah object Scanner
Scanner input = new Scanner(System.in);
//tampilkan nilai
System.out.println("value = " + value);
}
} Object standard output
Reading Numbers from the Keyboard
Method Description
+ Addition 34 + 1 35
% Remainder 20 % 3 2
x = y = z = 10;
val = 20 / 5 * 3 / 4;
[https://introcs.cs.princeton.edu/java/11precedence/]
Precedence of Operators
When an expression has two operators with the
same precedence, the expression is evaluated
according to its associativity.
x = y = z = 10; x = (y = (z = 10));
Sama seperti
val = ((20 / 5) * 3) / 4;
[https://introcs.cs.princeton.edu/java/11precedence/]
Augmented Assignment Operators
int i = 10;
int newNum = 10 * ++i;
int i = 10;
[Slide Liang, Slide 68-71]
int newNum = 10 * i;
i += 1;
int i = 10;
int newNum = 10 * ++i;
int i = 10;
i += 1;
int newNum = 10 * i;
[Slide Liang, Slide 38]
Casting Primitive Data Type
Widening Casting (Implicit)
byte -> short -> int -> long -> float -> double
double d = 3;
Widening Casting (Implicit)
byte -> short -> int -> long -> float -> double
double d = 3; Memory
d: double
3.0
Tipe: double
Tipe: int
Tipe: double
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte
double pi = 3.14;
int val = (int) pi;
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte
Tipe: int
Tipe: double val: int
3
Casting Eksplisit
Fraction part is truncated!
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte
Conversion Rules
3: int
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Conversion Rules
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
int sum = 0;
sum += 4.5; //sum becomes 4 after this statement
Mengapa?
sum += 4.5; is equivalent to sum = (int)(sum + 4.5);