Java base Java basic data types

Source: Internet
Author: User

Basic concepts of 1.Java data types:
The data type in computer language is an abstract expression of memory location, which can be understood as an abstract expression for memory. With each language, there will be data types of understanding, there are complex, simple, all kinds of data types need to understand at the beginning of learning, Java is a strongly typed language, so Java for Data type specification will be relatively strict. The data type is the abstract atomic concept of language, which can be said to be the most basic unit definition in language, in Java, there are essentially two types of data: Simple Type and complex type.
Simple types: Simple data types are not simplified, built-in data types, defined by the programming language itself, and represent real numbers, characters, and integers.
Complex types: The Java language itself does not support a struct (struct) or union (Union) data type in C + +, and its composite data types are typically constructed from classes or interfaces, which provide a way to bundle data and methods while hiding information outside the program.
Basic Types in 2.Java:
1) Concept:
Simple types in Java are conceptually divided into four categories: real numbers, integers, characters, and Boolean values. But one thing to note is that there are only eight primitive types in Java, and the list is as follows:
Real number: double, float
Integers: Byte, short, int, long
Character: Char
Boolean value: Boolean
Complex types and basic types of memory models are inherently different, and simple data types are stored in such a way that all simple data types do not have a "reference" concept, and that simple data types are stored directly on memory stacks in memory, where the value of the data itself is stored in the stack space. In the Java language, only these eight data types are the storage model, while the rest of the complex data types that inherit from the object class are stored according to the memory model of the stored objects in Java, using the Java memory heap and memory stack for this type of data storage, simply say, " References are stored on an ordered memory stack, and the value of the object itself is stored on the memory heap.
2) Detailed Description:
A brief list of Java's simple data is as follows:
Int:int is an integer type that, when stored, is stored in 4 bytes with a range of 2,147,483,648 to 2,147,483,647, and the default value for the int type is 0 when the variable is initialized.
Short:short is also an integer type, stored in 2 bytes when stored, ranging from 32,768 to 32,767, when the variable is initialized, the default value of the short type is 0, in general, because the Java itself transformation reasons, can be directly written as 0.
Long:long is also an integer type, stored in 8 bytes, with a range of 9,223,372,036,854,775,808 to 9,223,372,036, 854,775,807, when the variable is initialized, The default value for the long type is 0L or 0l, or it can be written directly as 0.
Byte:byte is also an integer type, stored in 1 bytes when stored, ranging from 128 to 127, when the variable is initialized, the default value of byte type is also 0.
Float:float belongs to the real type, when stored, with 4 bytes to store, the range is 32 bits ieeee 754 single precision range, when the variable is initialized, the default value of float is 0.0f or 0.0F, when the initialization can write 0.0.
The double:double also belongs to the real type, which is stored in 8 bytes when stored, with a range of 64-bit IEEE 754 double precision ranges, and the default value of double is 0.0 when the variable is initialized.
Char:char is a character type, stored in 2 bytes when stored, because the character set of Java itself is not stored in ASCII code, is the use of the 16-bit Unicode character set, its character range is the Unicode character range, when the variable is initialized, The default value for the char type is ' u0000 '.
Boolean:boolean is a Boolean type that does not use bytes when it is stored, only uses 1 bits to store it, the range is only 0 and 1, the literal is true and false, and the Boolean variable has the default value of False when initializing.

3) Related introduction:
There are several simple features in the Java basic type when using literal assignment:
"1" When the integer type of data using literal assignment, the default value is int type, that is, when using 0 or other numbers directly, the value is of type int, so when using the assignment of long a = 0, there is a data conversion inside the JVM.
"2" When data of real type is assigned by literal value, the default value is double type, that is, when the literal two appears, the JVM uses a double type of data type.
(*: The above two points are explained in detail in the transformation.) )
"3" Starting with JDK 5.0, there is an automatic unpacking unpacking in Java operation, based on this need to do some instructions:

corresponding to the original data type, each data type has a complex type of encapsulation class, Boolean, short, Float, Double, Byte, Int, Long, Character, these types are built-in encapsulation classes, These encapsulation classes (Wrapper) provide a very intuitive approach, for the encapsulation class needs to be explained that each encapsulation class has a xxxvalue () method, in this way it can be referenced by the value of the object into the original variable value, not only that, Each wrapper class also has a valueof (string) method that directly converts the string object to the appropriate simple type.

Prior to JDK 5.0, there was no automatic unpacking operation, i.e. auto box operation, so it was not possible to use the following method of assignment code before:
Integer a = 0; This assignment cannot be done in JDK 1.4 and the JDK compiler below, but JDK 5.0 has an automatic unpacking operation, so in the case of a compiler above JDK 5.0, the above code can be passed, About automatic unpacking and unpacking I'll use an additional 1.4 to 5.0 upgrade to explain it in detail.

Transformation of basic data types in 3.Java:
The conversion of simple data types in Java is divided into two types: automatic conversion and casting
1) Automatic conversion:
When a "small" data and more "big" data together, the system will automatically convert the "small" data into larger data, and then the operation.
In the method invocation procedure, if the actual parameter is smaller, and the function's formal parameter is "large", it will be called directly with the larger parameter function unless there is a matching method.
2) Casting:
When converting "large" data to "small" data, you can use coercion type conversions, which must be used when casting a type:
int a = (int) 3.14;
It is possible that the loss of precision can occur when this type of conversion is on the top.
For automatic promotion of types, follow the rules below:
All values of byte, short, char type are promoted to int type;
If one operand is of type long, the result is a long type;
If one operand is of type float, the result is a float type;
If one operand is of type double, the result of the calculation is double type;
The automatic type conversion diagram is as follows:
Byte->short (char)->int->long->float->double
If this is a cast, the upper graph is reversed
3) Conversion Attach:
When two types are automatically converted, the condition needs to be met: the "1" type is compatible, and the value range of the "2" destination type should be greater than the range of the source conversion value. The extended scope follows the automatic type conversion tree above, and when both conditions are met, the extended conversion occurs, and for several primitive type conversion processes, the compatibility Boolean and char should be independent, while the other six types are compatible in the casting process, The only possible special is that char and int are convertible, but use the ASCII values of char such as:
int a = (int) ' A ';
The value of a is 97 after the output of the conversion.
High-precision number in 4.Java:
Java provides two specialized classes for high-precision operations: BigInteger and BigDecimal, although Java primitive variables have corresponding package types, but these two variables do not have corresponding primitive types, but by means of the method to provide some of the two types of operations, The meaning is that the normal type can do the operation, both types of corresponding have, just because the accuracy is too high may not be efficient. For more information on these two classes, refer to the relevant API documentation for the JDK.

5. Some tips on data types: (refer to a source document below)
If the result of precision is required, avoid using float and double as possible:
The float and double types themselves are designed to do a scientific operation, that is, to perform binary floating-point operations, but they do not provide completely accurate results, so it is not particularly appropriate to avoid using float and double,float and double in currency operations in the numeric values that require precision. It is impossible for float and double to express exactly 0.1. Test the code below to understand:
System.out.println (2.02-0.42);
The result is not unexpected, the result is not accidental, but the JVM itself is designed to determine the purpose. To solve this problem, the best way is to use bigdecimal, int or long to do the related operations, especially currency operations, using BigDecimal instead of double is a good way.
The only drawback of BigDecimal is that BigDecimal does not have a corresponding primitive type, so it is necessary to make a method call in order to perform the basic numeric operation, which will make it incompatible with our programming habits, and if you use int and long, you need to perform a simple encapsulation operation.
Therefore, in the task of calculating the accuracy of the answer, it is generally prudent to use float and double, if you are doing business operations and require rounding or simple rounding behavior, using BigDecimal may be more convenient. So try to avoid using float and double in precision operations, especially in our usual currency operations.
5. Summary:
The above is a summary of the development process for the Java data type, the last technique is found in a business system operation, and then refer to a lot of documents on the Web, accidentally found in a blog, the original text advocated in the precision operation as far as possible without float and double, And the basic data type of Java, the top should cover all the development will be used in the content, but also a few special values in the comparison, there is no explanation, such as Double.NaN = = The Double.NaN output is false, and so on, the special types of all encapsulation classes are not described in detail.

Java base Java basic data types

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.