1 / 42

Session: Java.lang package

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)

Télécharger la présentation

Session: Java.lang package

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session: Java.langpackage

  2. Wrapper Classes • Boolean (baobọc) boolean • Character  char • Byte  byte • Short  short • Integer  int • Long  long • Float  float • Double  double

  3. 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");

  4. Boolean Class • Methods (phươngthức) booleangetBoolean(String s) Boolean valueOf(String s) booleanbooleanValue()

  5. Boolean Class

  6. 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;

  7. 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; }

  8. Boolean Class • Vídụ 3: parse a String to a Boolean object • Gợi ý: dùng methods valueOf()

  9. Vídụ 4: return the string representation of a boolean • Gợi ý: toString()

  10. Vídụ 5: Convert String to Boolean • Gợi ý: Boolean.parseBoolean(string)

  11. 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

  12. Character • Vídụ 1: char is 16 bit type and used to represent Unicode characters. Range of char is 0 to 65,536.

  13. Character • Vídụ 2: char variables behave like integers

  14. Character • Vídụ 3: Kiểmtra char is Upper Case • Gợi ý: char a=‘A’ Dùng methods: IsUpperCasekiểmtra

  15. Character • Vídụ 4: tươngtự vd3, kiểmtra char là lower case

  16. Character • Vídụ 5: Kiểmtra char cólà digit

  17. 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

  18. Character • Vídụ 7: Count letters in a String • Gợi ý: • String str=“abdfhg12sd” • Intdem • isLetter

  19. Byte • byte is smallest Java integer type (-128 đến 127)

  20. Byte • Vídụ 1: Convert byte to String • Gợi ý: • toString()

  21. Short • short is 16 bit signed type ranges from -32,768 to 32,767.

  22. Short • Min and Max values of datatype short • Gợi ý: • Short.Min_Value • Short.Max_Value

  23. Short • Convert Short to numeric primitive data types

  24. Short • Convert Java String to Short • Gợi ý: Short valueOf(String)     String str = "100";    Short sObj2 = Short.valueOf(str);System.out.println(sObj2);

  25. 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);

  26. 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);

  27. Long • Convert Long to numeric primitive data types example • Gợi ý: Giống short

  28. Long • Xuấtgiátrị min vagiátrị max của long

  29. Long • BT1: Viếtchươngtrìnhkiểmtrasốlớnnhất • Gợi ý: • long a, long b, long c

  30. Long • BT2: Tìmsốnhỏnhất • Gợi ý: • long a, long b, long c

  31. Long • Convert Java String to Long example     String str = "100";    Long lObj2 = Long.valueOf(str);System.out.println(lObj2);

  32. 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);

  33. 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);

  34. Float • Min and Max values of data type float

  35. 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)

  36. 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("=");    }

  37. 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("=");    }

  38. Float • Boolean isNaN(): return true (value is NaN), false • NaN: Not a Number • Vídụ: Kiểmtra float f

  39. 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);

  40. 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);

  41. Other classes in the java.lang package • Math • System • Object • Class • ThreadGroup • Runtime

  42. Math class

More Related