430 likes | 562 Vues
Session: Java.lang package. Wrapper Classes. Boolean ( bao bọc ) boolean Character char Byte byte Short short Integer int Long long Float float Double double. Boolean Class. f asle và true Constructor: Boolean ( boolean b)
E N D
Wrapper Classes • Boolean (baobọc) boolean • Character char • Byte byte • Short short • Integer int • Long long • Float float • Double double
Boolean Class • faslevà true • Constructor: Boolean (boolean b) Vídụ: Boolean b1 = new Boolean (false); Boolean (String s) Vídụ: Boolean b2 = new Boolean ("true");
Boolean Class • Methods (phươngthức) booleangetBoolean(String s) Boolean valueOf(String s) booleanbooleanValue()
Boolean Class • Vídụ 1: viếtchươngtrình kiểmtra nếu a>b: true ngượclại:false Gợi ý: booleanb3 = (a > b) ? true : false;
Boolean Class • Vídụ 2: viếtchươngtrìnhxuấttừ 1 đến n nếu n làsốlẻ 1, false nếu n làsốchẳn 2, true • Gợi ý: boolean b; for(….) { if(….) { …. b=false; } else … b=true; }
Boolean Class • Vídụ 3: parse a String to a Boolean object • Gợi ý: dùng methods valueOf()
Vídụ 4: return the string representation of a boolean • Gợi ý: toString()
Vídụ 5: Convert String to Boolean • Gợi ý: Boolean.parseBoolean(string)
Character • Các methods: booleanisDigits(char c): return true (c=digit), false booleanisLetter(char): return true (c=letter), false booleanisLetterOrDigit(char c): …. booleanisLowerCase(char c): …. booleanisUpperCase(char c): … booleanisWhitespace(char c): …. char toLowerCase(char c) char toUpperCase(char c) • Vídụ: “ISCOL1-ISCOL2” - letter: 10 - digit: 2 - symbol: 1 - tong char: 13
Character • Vídụ 1: char is 16 bit type and used to represent Unicode characters. Range of char is 0 to 65,536.
Character • Vídụ 2: char variables behave like integers
Character • Vídụ 3: Kiểmtra char is Upper Case • Gợi ý: char a=‘A’ Dùng methods: IsUpperCasekiểmtra
Character • Vídụ 4: tươngtự vd3, kiểmtra char là lower case
Character • Vídụ 5: Kiểmtra char cólà digit
Character • Vídụ 6: (nângcao) kiểmtramộtchuỗiđócótoànlà digit không • Gợi ý: String a= “1234456” true String b= “12334f1” false
Character • Vídụ 7: Count letters in a String • Gợi ý: • String str=“abdfhg12sd” • Intdem • isLetter
Byte • byte is smallest Java integer type (-128 đến 127)
Byte • Vídụ 1: Convert byte to String • Gợi ý: • toString()
Short • short is 16 bit signed type ranges from -32,768 to 32,767.
Short • Min and Max values of datatype short • Gợi ý: • Short.Min_Value • Short.Max_Value
Short • Convert Short to numeric primitive data types
Short • Convert Java String to Short • Gợi ý: Short valueOf(String) String str = "100"; Short sObj2 = Short.valueOf(str);System.out.println(sObj2);
Short • Use toString method of Short class to convert Short into String. • Gợi ý: String toString() short s = 10; Short sObj = new Short(s); String str = sObj.toString();System.out.println(str);
Long • Constructor: • Long(long l) • Long(String str) Vídụ: long l = 10; Long longObj1 = new Long(l); Long longObj2 = new Long("5");System.out.println(longObj1);System.out.println(longObj2);
Long • Convert Long to numeric primitive data types example • Gợi ý: Giống short
Long • Xuấtgiátrị min vagiátrị max của long
Long • BT1: Viếtchươngtrìnhkiểmtrasốlớnnhất • Gợi ý: • long a, long b, long c
Long • BT2: Tìmsốnhỏnhất • Gợi ý: • long a, long b, long c
Long • Convert Java String to Long example String str = "100"; Long lObj2 = Long.valueOf(str);System.out.println(lObj2);
Long • Use toString method of Long class to convert Long into String. Long lObj = new Long(10); String str = lObj.toString();System.out.println(str);
Float • Java float is 32 bit single precision type and used when fractional precision calculation is required. • Constructor: • Float (float f) • Float (double d) • Vídụ: float f = 12.3f; System.out.println(f);
Float • Min and Max values of data type float
Float • BT1: so sánh 2 con số • Gợi ý: Cách 1: inti= Float.compare(a, b) Cách 2: int j = a.compareTo(b)
Float • Cách 1: float f1 = 5.5f;float f2 = 5.4f;inti1 = Float.compare(f1, f2);if (i1 > 0) {System.out.println(">"); } else if (i1 < 0) {System.out.println("<"); } else {System.out.println("="); }
Float • Cách 2: Float fObj1 = new Float(5.5); Float fObj2 = new Float(5.4);inti2 = fObj1.compareTo(fObj2);if (i2 > 0) {System.out.println(">"); } else if (i2 < 0) {System.out.println("<"); } else {System.out.println("="); }
Float • Boolean isNaN(): return true (value is NaN), false • NaN: Not a Number • Vídụ: Kiểmtra float f
Float Cách 1 float f = (float) Math.sqrt(-10);booleanb1 = Float.isNaN(f);System.out.println(b1); Cách 2 float f = (float) Math.sqrt(-10); Float fObj = new Float(f);booleanb2 = fObj.isNaN();System.out.println(b2);
Float • Use toString method of Float class to convert Float into String. • Vídụ: Float fObj = new Float(10.25); String str = fObj.toString();System.out.println(str);
Other classes in the java.lang package • Math • System • Object • Class • ThreadGroup • Runtime