CodeGym /Kursus Jawa /Multithreading Jawa /Angka (Integer, Long, Byte)

Angka (Integer, Long, Byte)

Multithreading Jawa
tingkat , wulangan
kasedhiya

"Amarga pangembang bisa nggawe kelas sing nggambarake angka, mula dheweke mutusake dadi kreatif, kaya pangembang nyata."

"Kaping pisanan, dheweke teka karo kelas Nomer abstrak, sing asale Byte, Short, Integer, Long, Float, lan Double. Nduwe cara sing mbantu ngowahi angka menyang jinis numerik liyane."

Metode saka kelas Number
1
int intValue()
2
long longValue()
3
float floatValue()
4
double doubleValue()
5
byte byteValue()
6
short shortValue()

"Bener. Sawise kabeh, sampeyan ora bisa nulis iki:"

Long x = 100000;
Integer y = (Integer) x;

"Ya, jinis iki dudu primitif. Mulane kita nggunakake metode kelas Angka:"

Long x = 100000;
Integer y = x.intValue();

"Nanging isih ana sawetara perkara sing kudu ditimbang. Amarga Integer dudu int, obyek Integer ora bisa dibandhingake karo operator «==» klasik.

Mbandhingake jinis primitif
int x = 500;
int y = 500;
x == y; //true
Mbandhingake jinis non-primitif
Integer x = 500;
Integer y = 500;
x == y; //false
x.equals(y); //true

"Pancen. Kepriye aku ora langsung mikir."

"Nanging ana liyane."

"Sampeyan nggawe korsleting sirkuit! Apa maneh?"

"Nalika kita nemtokake nilai int menyang variabel Integer, metode Integer.valueOf diarani:"

Kode Apa pancene kedadeyan
Integer x = 5;
Integer x = Integer.valueOf(5);

“Iya, aku wis ngerti yen saka conto ing ndhuwur.”

"Nanging, fungsi valueOf ora tansah nggawe obyek Integer anyar."

"Eh, apa maksudmu "ora mesthi"?"

"Iku cache nilai saka -128 kanggo 127."

Kode Apa pancene kedadeyan Katrangan
Integer x = 300;
Integer y = 300;
Integer z = 300;
Integer x = Integer.valueOf(300);
Integer y = Integer.valueOf(300);
Integer z = Integer.valueOf(300);
Variabel x, y, lan z ngemot referensi kanggo obyek sing beda
Integer x = 100;
Integer y = 100;
Integer z = 100;
Integer x = Integer.valueOf(100);
Integer y = Integer.valueOf(100);
Integer z = Integer.valueOf(100);
Variabel x, y, lan z ngemot referensi kanggo obyek sing padha.
Integer x = new Integer(10)
Integer y = new Integer(10)
Integer z = 10;
Integer t = 10;
Integer x = new Integer(10)
Integer y = new Integer(10)
Integer z = Integer.valueOf(10);
Integer t = Integer.valueOf(10);
Variabel z lan t ngemot referensi kanggo obyek sing padha.

"Ing tembung liyane, kahanan iki:"

1)  Yen kita nulis «integer anyar ()», banjur kita dijamin kanggo njaluk obyek anyar.

2)  Yen kita nelpon Integer.valueOf (), tegas utawa liwat autoboxing, banjur cara bisa bali obyek anyar utawa obyek cache yen pitakonan nomer ing sawetara saka -128 kanggo 127.

"Apa sing elek banget babagan cara ngasilake obyek saka cache?"

"Ora apa-apa. Sampeyan mung kudu ngerti yen kadhangkala, nalika sampeyan ora nyana, obyek bisa uga padha. Kabeh kanthi kesetaraan rumit. Yen kita mbandhingake primitif karo non-primitif, banjur dibandhingake primitif: "

Masalah mbandhingake
int x = 300;
Integer y = 300;
Integer z = 300;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //false (comparison based on references)
Conto sing luwih menarik. Cache ngetik gambar ing kene
int x = 100;
Integer y = 100;
Integer z = 100;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //true (comparison based on references; they point to the same object)
Nanging cache ora melu kene
int x = 100;
Integer y = new Integer(100);
Integer z = 100;

x == y; //true (comparison based on primitive value)
x == z; //true (comparison based on primitive value)
y == z; //false (comparison based on references; they point to different objects)

"Apik ... Lan carane aku bakal apal kabeh iki?"

"Sampeyan ora perlu apal iki. Sampeyan mung kudu ngerti carane iki kabeh diatur lan apa bener mengkono nalika primitif lan non-primitif pasangan teka menyang muter ".

"Aku uga menehi saran supaya sampeyan ndeleng metode kelas Integer. Wis sawetara cara sing apik lan migunani. Sampeyan malah wis kerep nggunakake salah sijine."

"Iya, aku inget. Integer.parseInt();"

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION