Java variable data type __java

Source: Internet
Author: User
variable data type of Java variable

Java Data type diagram:
1. Basic Data Types

The basic data type, also known as the built-in type, is the ability to allocate memory directly on the stack, which is why Java retains the most basic data types: performance. Here's a reference to why Java needs to preserve basic data types.
Also, note that Java is based on the JVM, so its bytes are fixed, regardless of the machine platform, where all the memory size is unified (except Boolean, and the memory of each cell is implemented by the JVM itself).
There are four categories of eight basic data types (Note 1):
1. Integral type: All are signed type.
1.byte:1 byte (8bit), High is the symbol bit, the remaining 7 bits are data bits, the range: 2 of 7 ~2 of 7-1 (1111,1111~0111,1111), that is, -128~127 (the following calculation method is the same);

Note: The byte type, although it occupies 1 bytes in semantics (logic), is actually seen as an int in the JVM
, which in fact occupies 32 bits and 4 bytes, so its efficiency is no different from that of an int, and short is the same. The reason for the
byte/short type is that the range types are explicitly used in some places, and the second is that
in the byte[] array, the JVM stores the true 1 bytes, short[]2 bytes. (but also some JVM its byte[]
array is also 4 bytes 1 bits)

2.short:2 byte (16bit), high for sign bit, the remaining 15 bits are data bits, the range: 2 of 15 Times Square ~2 15 times-1, namely -32768~32767;

3.int:4 byte (32bit), the range-2 of the 31-~2 side of the 31-side -1;java The default integer type, namely:

Long L = 0xfffffffffff;//0x indicates that this number is 16, and 0 represents 8.
//Compiler error, because the right side of the default is int, but it is out of range (no more than the Int range
//compiler will implicitly convert int long), so the error (the same errors will appear in float).

The same goes for:

Short S = 123;//(this 123 is also the int type, here, = Operation compiler can implicitly convert) 
s = s + 123;//compiler error, that is because s+1 is the int type (the compiler first converts s to int, then + 1),
//here, + The action compiler cannot be implicitly convertible (it prompts for distortion, that is, the precision may be compromised), the correct approach:
s = (short) (S + 123)//note, not (short) s + 123.

Type conversions are described in: Java data type conversion.
4.long:8 byte (64bit), Range:-2 of 63 ~2 of 63 Times Square-1; Declares a large long method:

Long L = 0xfffffffffffl;//plus L or L at the back.
//(Forced conversion: long L = (long) 0xfffffffffff no use)

2). Floating point Type
5.float:4 byte (32bit), single precision, data range: ( -2^128) ~ ( -2^ (-23-126))-(0)-(2^-149) ~2^128. Floating point numbers, in layman's terms, are decimals, however, this is a precision requirement, that is, in this interval float is not able to express any decimal, but in a certain precision, such as the float effective bit 7~8 bit (including integer digits and decimal places, effective decimal places are 6~7 bit, why here is 7~8 (6~7), Reference: Float/double value range and precision in Java That is, the 9JVM behind the 0.123456789 is not recognized (8 can recognize, integer digit 0 is not a significant bit, for example, 12.1234567 after 7 also do not know, only 6 digits effective decimal places (note, see is a valid bit, not a valid decimal place, float has 7~8 bit valid bit)), namely:

if (0.123456781f = = 0.123456789f) {//Note the following plus f/f, or double
    System.out.println ("true");
} else{
    System.out.println ("false");
}
Print Result: True
//In fact, the comparison of floating-point numbers can not be directly judged by = =, where the reason is to be traced to the floating point of memory structure
//Floating-point comparison may use a difference, but this situation is only approximate comparison
//If you want to be accurate, You can use the BigDecimal
System.out.println (float.min_value);//1.4e-45 = 2^-149
//Here "minimum" means the smallest number of Float can be represented, In fact, the Float minimum equals the maximum value to take the negative
System.out.println (float.max_value);//3.4028235e38 = 2^128

6.double:8 byte (64bit), double precision, Range: -2^1024~ ( -2^ ( -1022-52)) -0-(2^-1074) ~2^1024,java default floating-point type, where not followed by f/f, default is double type, namely:

float f = 1.23;//compile an error, because
float f = 1.23f;//or float f = 1.23F;
By default, double,1.23 (double) is converted to float, but double to float is
///The value range of a large conversion to the value of the small loss of precision, so can not be converted (see Java data type Conversion)
// So why, int can be converted into byte, Short,int range is larger than that.
//As mentioned earlier, byte and short are actually int on the JVM, so the compiler does not think it will lose precision
//But int is not convertible to Boolean, although Boolean is also 4 bytes (General JVM), But in the JVM's view that this
//both is totally two things, of course not convertible (coercion is not, you can't force the cat into a bird, completely two species), and byte, short are integral type, the same int is a type

3). Character type
7.char:2 byte (16bit), representing a character (can be Chinese), character encoding using Unicode (more accurate, character set (charset) using UCS-2, Encoding (encoding) using UTF-16), is actually a 16-bit unsigned integer, but note that as development, the number of characters that Char can represent (the UCS-2 character set) is defined dead, so it is not recommended. (More, as well as about Unicode, UTF8/16 references: Unicode, UTF8, and Java char.) )

Char C = 3+5;//is correct, char is unsigned integer, but cannot be
int a1 = 3;int A2 = 5;char C0 = a1+a2;//You need to cast it here.
Char C1 = -3;//compilation error, Char cannot represent Negative numbers, even if
char c2 = (char) -3;//compiled correctly, but meaningless (garbled)
char c3 = ' 3 '; correct, output character 3
char C4 = "3";//Compile error, double quotation mark, indicating string
Char C5 = ' 65 '//Compile error, here 65 is two characters

4). Boolean type
8.boolean: 1bit, but in fact, Boolean does not specify that it is all about the JVM implementations, but the Java Virtual Machine specification gives a definition of 4 bytes (byte interpretation) and one byte of the Boolean array.

Note 1:
(1). This method is a popular method of division, in fact, it should be two kinds: numerical type and Boolean type. Numeric types are divided into integers and floating-point type. Integral types include: Byte, short, int, long, char, floating-point: float, double, bool Boolean. Char is considered an integral type because char exists in the JVM as an unsigned integral type.
(2). In fact, in Java, except for these 8 types and object types, there is a more special type of existence, that is void. Java.lang.Void is a placeholder class that is not instantiated and holds the class object of the Java keyword Void. Why do you say it's special? is clearly a class, is not the object type. That is because the void.class.isPrimitive () (this method is used to determine whether a class object is a basic type) returns True, so void is one of the basic types (wrong), but it is more special, not a kind of data, Just a symbol.
20160921 change: The above mistake, the void and void two mixed together, in fact, you can simply consider the relationship between the two similar packaging and basic types of relationships, such as the integer and int relationship, Java.lang.Void is a placeholder class that is not instantiated to hold a reference to a class object that represents the Java keyword void:

public static final Class<void> TYPE = Class.getprimitiveclass ("Void");

And the integer also has a similar statement:

public static final class<integer>  TYPE = (class<integer>) class.getprimitiveclass ("int");

The difference is that void is only for void service, that is, the so-called placeholder class, not to be used by him. So the void class is just a normal class, and void can be recognized as a basic type like int. 2. Reference data type

Also called object variable type, composite data type, contains class, interface, array (except basic type, is reference type). The biggest difference between a reference type and a basic type is:

int a = 5;//here A is an object (strictly not an object, just a symbol identifier), 5 is the numeric
integer a = 5;//Here's A is a reference, 5 is an object, more image common is: Object
o = new Object (); /O is the reference (in the stack), and the new object () is the object (in the heap)
//The second line of code, 5 is automatically wrapped into an integer object

The reference here is a bit like a pointer in C + +, but unlike a pointer, you can't change the value it points to by changing its value. That

ClassA p = new ClassA ();//c++, this is the time to do so:
p = p + 1;//move a unit forward, Java is not
//This operation, in fact, is the direct operation of memory, it is clear that Java does not allow programmers to do this

The essence is that Java references do not support direct memory operations, while pointers can, so Java is more secure, but not flexible, while pointers, degrees of freedom, but at the same time, to be more careful due to improper pointer operation caused by a variety of memory problems. In Java, any object needs to be accessed by reference, and objects that are not referenced are treated as garbage objects and will be reclaimed.
Reference, in fact, the same quality as the pointer (can be understood as a limited pointer), the store is an address, as for the instance object's address, or a point to the handle pool address (here can refer to: (3) Java memory structure), is entirely to see the implementation of the JVM. The enumeration types in
Java, which are subclasses of the Enum class, are either one of the classes or reference types. A
reference type, also known as an object variable type, is relative to the base data type (the base data type is not an object) and is called a composite data type, so that the data of the reference type is ultimately composed of the base data type. As with interfaces, interfaces are not instantiated, and the final implementation is implemented by classes, and the implementation of arrays in the JVM is implemented through classes, one-dimensional arrays of each type, two-dimensional arrays ... is a class, except that it is a special class whose object headers are different from the object headers of a generic object (the main thing is that the array object headers have object lengths).
In addition, you can refer to Java references as references in Java.

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.