Nine basic data types and object wrapper class

       We often say that java is object-oriented, everything is an object in java. But there is a special case, that is, we often say that the nine basic types, why basic types of special it? Because the new object is stored in the "reactor" rather small and very simple variable, with the new is not cost-effective, and thus not in java to create new variables, but directly to the value stored in the "stack" in it more efficient . Therefore, the basic data types but is a special case -

       The figure is nine java basic data types: ↓

       

         I'm here to do two things to add:

         1, boolean size is not clear. Because " we The depends the Java Virtual Machine ON ", that is, in fact, depends on the size of the boolean java virtual machine, interested students can check out here is not specifically expand, but to say conclusion: a single boolean variable in a virtual machine an int is stored, that is 4 bytes is 32bits, while boolean array in a virtual machine, that is, 1-byte single 8bits.

         2. Why is void of fundamental data type it? Because the java There are two types: primitive types and reference types. The first paragraph of the article also said, the Java stored after use to create a new object "heap", which is a reference type; java not new, it would be stored in the variable "stack", which is the basic type. Ever since the "java programming ideas" and "Think In Java" two java classic books, will be included in the basic type void.

 

       The figure is the default value of the eight basic types of void remove: ↓

        

         He went on to add:

         In java, must be variable value (i.e., initialized). The scope of the two types of variables: global and local variables.

         Global variables can not be manually initiated, java will be given a default value (automatic initialization) to ensure that the variable is initialized.

         Local variables can not not initiated manually, java will return an error to tell you uninitialized variables.

 

 

Object wrapper class

Concept: In order to facilitate operation of the basic data type value, which is packaged in a target, defines the properties and behavior of objects in the rich operation of the data. Used to describe the class of the object is called object wrapper class basic data types, mainly by converting between strings and basic types.

       byte--->Byte

       short---> Short

       int--->Integer

       long--->Long

       float--->Float

       double--->Double

       char--->Character

       boolean--->Boolean
        
       void--->Void

 

 

Conversion between basic types and string:

Note: String String is a commonly used class, not the basic types!

Basic types ---> String

1, base type value + ""

System.out.println(123+123);//246
System.out.println(123+""+123);//123123

2, with the String Class Static Method valueOf ( basic type value );

    valueOf (xxx xxx):  Returns a string representation of the parameter xxx.

     Example: valueOf (I int):  Returns a string representation of the argument int.

3, with Integer static method valueOf ( basic type value );

 Integer x =Integer.valueOf(9);

 

String ---> basic types

1, the static method of packaging XXX parseXxx ( " XXX string type ");  

   Example: 

int parseInt("intstring");

long parseLong("longstring");

boolean parseBoolean("booleanstring");

    Only Character does not parse method ( which package is a character )  

 

If the string being Integer encapsulated object. Using another non-static methods, intValue (): an Integer object into a basic data type value.

 

 

Integer has a different band reflect

 

 Decimal -> Other hex

       * ToBinaryString binary

       * ToOctalString  octal

       * ToHexString hex

System.out.println(Integer.toBinaryString(60));//111100
System.out.println(Integer.toOctalString(60));//74
System.out.println(Integer.toHexString(60));//3c

System.out.println(Integer.toString(60,16));//3c

  Other hex -> decimal.

       *    parseInt("string",radix)

System.out.println(Integer.parseInt("111100",2));//60
System.out.println(Integer.parseInt("74",8));//60
System.out.println(Integer.parseInt("3c",16));//60

 

 

Published 38 original articles · won praise 6 · views 1924

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/97955834