Java packaging classes

Source: Internet
Author: User

Java packaging classes

Speaking of the packaging classes in Java, we have to introduce the basic data types in Java (eight types): byte, short, int, long, float, double, char, and boolean. The following describes the eight data types in a table:

  Byte Short Int Long Float Double Char Boolean
Number of digits 8 16 32 64 32 64 16 1
Bytes 1 2 4 8 4 8 2 1 (1/8)
Default Value 0 0 0 0L 0.0f 0.0d   False
Packaging type Byte Short Integer Long Float Double Character Boolean

 

 

 

 

 

Note:

  • The char type occupies two bytes, so it can represent Chinese characters.
  • Boolean theoretically occupies 1 bit (1/8 bytes), while actually processed by 1 byte (1 byte)
1 public class DataTypeDemo {2 3 byte a; 4 short B; 5 int c; 6 long d; 7 float e; 8 double f; 9 boolean g; 10 char h; 11 12 public static void main (String [] args) {13/* 14 * size of several basic data types (bit) 15 */16 System. out. println (Byte. SIZE); // 817 System. out. println (Short. SIZE); // 1618 System. out. println (Integer. SIZE); // 3219 System. out. println (Long. SIZE); // 6420 System. out. println (Float. SIZE); // 3221 System. out. println (Double. SIZE); // 6422 System. out. println (Character. SIZE); // 1623 24/* 25 * default value of basic data type 26 */27 DataTypeDemo dtd = new DataTypeDemo (); 28 System. out. println (dtd. a); // 029 System. out. println (dtd. b); // 030 System. out. println (dtd. c); // 031 System. out. println (dtd. d); // 032 System. out. println (dtd. e); // 0.033 System. out. println (dtd. f); // 0.034 System. out. println (dtd. g); // false35 System. out. println (dtd. h); // 36 37/* 38 * char type can indicate 39 */40 char ch = 'hangzhou'; 41 System. out. println (ch); // listen 42} 43 44}
View Code 1. Understanding of Packaging

To meet the Object-Oriented Java feature, each of the above basic data types has a packaging class in the java. lang Package, which encapsulates each basic type into a class. Common packaging classes include Character, Number, and Boolean. For details, see the table above.

Here we will summarize some characteristics of the packaging class:

  • All packages can use the corresponding basic data types as parameters to create their instance objects.
  • In addition to the Character class, other packaging classes can use a string as a parameter to construct their instances.
  • When the constructor parameter of the Boolean class is of the String type, if the String is true (case-insensitive), the object indicates true; otherwise, the object indicates false.
  • When the Number constructor of the packaging class is of the String type, the String cannot be null, and the String must be able to be parsed as the basic type of data

Sample Code:

1 public static void main (String [] args) {2 // All packages can use the corresponding basic data type as a parameter to create their Instance Object 3 Integer a = new Integer (100 ); 4 Double B = new Double (100.00); 5 Character c = new Character ('A'); 6 Boolean d = new Boolean (true); 7 System. out. println (a + "" + B + "" + c + "" + d); // 100 100.0 A true 8 9 // except for the Character class, other packaging classes can use a string as a parameter to construct their instances 10 Integer a1 = new Integer ("100"); 11 Double b1 = new Double ("100.00"); 12 Bo Olean d1 = new Boolean ("true"); 13 System. out. println (a1 + "" + b1 + "" + d1); // 100 100.0 true 14 15/* 16 * When the constructor parameter of the Boolean class is of the String type: 17 * If the string is true (case-insensitive), the object indicates true; otherwise, false18 */19 Boolean d2 = new Boolean ("True "); 20 Boolean d3 = new Boolean ("TRUE"); 21 Boolean d4 = new Boolean ("hello"); 22 System. out. println (d2 + "" + d3 + "" + d4); // true false23 24/* 25 * When the Number constructor of the packaging class is of the String type, the string cannot be null26 * and This string must be able to parse to Basic Data Type 27 * or a numeric format exception will be thrown. 28 */29 Integer a2 = new Integer (""); // NumberFormatException: For input string: "" 30 Integer a3 = new Integer (null); // NumberFormatException: null31 Integer a4 = new Integer ("abc"); // NumberFormatException: For input string: "abc" 32 33}
Ii. Functions of Packaging

In general, packaging has the following purposes:

  • The collection cannot store basic data types, so common packaging classes
  • Contains the relevant attributes of each basic data type, such as the maximum value, minimum value, and number of digits.
  • As the class type corresponding to the basic data type, it provides a series of practical object operations, such as type conversion and hexadecimal conversion.
1 private static void getInfo () {2 3 // obtain the related attributes of the basic data type 4 System. out. println (Byte. MIN_VALUE); //-128 5 System. out. println (Byte. MAX_VALUE); // 127 6 System. out. println (Byte. SIZE); // 8 7 8/* 9 * some practical operations of the packaging type, such as type conversion, hexadecimal conversion, etc. 10 * Here Integer is used as an example, this section describes intValue, (static) parseInt, (static) toString, and other methods 11 */12 // call the constructor to convert the int type to the integer type, call the intValue method to convert the integer type to the int type 13 Integer in1 = new Integer (100); 14 int in2 = in1.intValue (); 15 16 // convert the string to the int Number of the decimal number. It is often used to convert any hexadecimal number to the decimal number 17 int num1 = Integer. parseInt ("100"); 18 System. out. println (num1); // 10019 // convert the string to the int number 20 int num2 = Integer in any hexadecimal notation. parseInt ("100", 8); 21 int num3 = Integer. parseInt ("ff", 16); 22 System. out. println (num2); // 6423 System. out. println (num3); 24 25 // convert the number of int to the corresponding number of String types 26 String s1 = Integer. toString (100); 27 System. out. println (s1); // 10028 // converts the int value to a number of any hexadecimal String type. It is often used to convert the decimal value to any hexadecimal number 29 String s2 = Integer. toString (100,8); 30 String s3 = Integer. toString (100,16); 31 System. out. println (s2); // 14432 System. out. println (s3); // 6433}

In fact, JDK1.5 has introduced the syntax function of the Automatic Disassembly box, that is, the system will automatically convert the basic data type and the corresponding packaging type, this makes it easier for programmers to write code.

1 // automatic packing 2 int m = 10; 3 Integer in = m; 4 System. out. println (in); // 105 6 // automatic unpacking 7 Integer inn = new Integer (10); 8 int n = inn; 9 System. out. println (n); // 10
Supplement: Comparison of "=" and "equals" in the packaging class

It is worth noting that the equals method in the packaging class is the same as the equals method in the String class, And the equals method in the Object class is overwritten. Therefore, the content instead of the address is compared, while "=" is still the address of the referenced variable, but when the packaging type is compared with the corresponding basic type, "=" is automatically split.

 1     private static void equals() { 2         // TODO Auto-generated method stub 3         Integer a = new Integer(-100); 4         Integer b = new Integer("-100"); 5         int c = -100; 6         System.out.println(a == b);//false 7         System.out.println(a == c);//true 8         System.out.println(b == c);//true 9         10         System.out.println(a.equals(b));//true11     12         String s1 = new String("hello");13         String s2 = new String("hello");14         System.out.println(s1 == s2);//false15         System.out.println(s1.equals(s2));//true16         17     }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.