Nine basic data types in Java and their encapsulation class __java

Source: Internet
Author: User
Tags wrapper

Java has two valid data type raw data type reference data type raw data type

Java supports 8 of original data types.byte type ( in byte)BYTE type is a 8-bit positive and negative binary integer minimum is-128 ( -2^7) The maximum is 127 (2^7-1) The default value is 0 byte type is mainly to save space in computer memory, byte type is smaller than the integer type 4 times times the packing class Java.lang.Byteshort Type (shorter)A short number is a 16-bit positive binary integer minimum is-32768 ( -2^15) The maximum is 32767 (2^15-1) short type is also in order to save memory space, shorter than integer small twice times the default value of 0 packaging class Java.lang.Shortinteger type (int)The integer is a 32-bit positive and negative binary number minimum is-2,147,483,648 ( -2^31) The maximum is 2,147,483,647 (2^31-1) The default value is 01 is applied to the integer value, generally enough to pack the class Java.lang.IntegerLong IntegerThe long integer is a 64-bit positive and negative binary number of the minimum is-9,223,372,036,854,775,808 ( -2^63) The maximum value is 9,223,372,036,854,775,807 (2^63-1) This data type is generally used in a larger application than an integer the default value is 0L wrapper class is Java.lang.Longfloating-point type (float)A floating-point type is a single, 32-bit IEEE754 standard for floating-point data that is primarily intended to save data in large floating-point numbers the default value is 0.0f floating-point type cannot be used for similar precise data such as currency the wrapper class is Java.lang.Floatdoubles (double)Double precision is a kind of double precision 4-bit IEEE754 standard floating-point data double precision is generally used for the default is the value of the decimal number is generally not the user currency, the default value of the exact data is 0.0d packaging class is java.lang.DoubleBoolean (Boolean)Boolean data represents an information bit-Boolean with only two data representations: true (True) and False (false) general user Judgment statement Boolean default is False wrapper class is: Java.lang.Booleancharacter type (char)Character data is a simple 16-bit Unicode standard under the minimum character is: ' \u0000 ' (or 0) the maximum value is: ' \uffff ' (or 65,535) character data can be used to store any letter of the packaging class: Java.lang.CharacterReference Data TypeReference data types are defined by the editor of the class, and they are used to access objects. These variables are defined as specific types that cannot be changed, such as Employee, Puppy, etc. class objects and array variables are the reference data types the default value for any reference data type is empty a reference data type can be used for any type of declaration and compatible type of object

Java also has a data type Void, the wrapper class is java.lang.Void, because it cannot be directly manipulated, so it is no longer described.
Basic data types can be used in virtual machines to drink their packaging type directly to replace, fast, so we can use their wrapper classes like basic data types when assigning and passing values, but it doesn't mean that the basic data type can invoke the wrapper class, because the base data types are final decorated, Therefore, it is not possible to inherit extensions of new classes or to recreate new methods. What is the difference between basic data types and their wrapper classes ?

int is the basic type, direct deposit value
An integer is a class that points to this object with a reference when it produces an object

Java divides memory into two types: one is stack memory, the other is heap memory
Some of the basic types of variables and reference variables defined in the function are allocated in the stack memory of the function
And the actual object is in the storage heap memory
That
int i = 5;//allocate space directly on the stack
Integer i = new Integr (5);//object is in heap memory, and I (reference variable) is in stack memory
The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.
Because the time required to allocate space in the heap is much larger than allocating storage space from the stack, Java is slower than C
When there is a need to put something in the Arraylist,hashmap, the basic type like int,double is not put in, because the container is loaded object, this is the need for these basic types of outer covering class. Each of the basic types in Java has a corresponding outer-covering class.
The difference between int and Integer

Java provides two different types: reference type (or package type, Warpper) and original type (or built-in type, primitive). int is the original data type of Java, and integer is the encapsulated class provided by Java for Int. Java provides a wrapper class for each original type.
Original type encapsulation class
Boolean Boolean
Char Character
BYTE byte
Short Short
int Integer
Long Long
float float
Double Double
The behavior of the reference type and the original type is completely different, and they have different semantics. Reference types and primitive types have different characteristics and usages, including size and speed issues, which type of data structure is stored, and the default value specified when the reference type and the original type are used as instance data for a class. The default value for an object reference instance variable is NULL, whereas the default value of the original type instance variable is related to their type.

int is the basic type, (int) (Math.random () *100) is a number that can be added and multiply. Integer is class, then new Integer (temp) is an object, and you can use the method of the integer class, such as Intvalue () to return the value of this int.

Reference: Http://my.oschina.net/Bruce370/blog/511431#OSC_h3_2
Http://blog.sina.com.cn/s/blog_4fdb887b0101479q.html

Related Article

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.