This article is divided into three parts:
1. Introduction to basic data types
2. Type conversion
3. Packing and Unpacking
--------------------------------------I'm a friendly divider--------------------------------------
Java has 8 basic data types, including 4 integers, 2 floating-point types, 1 character types used to represent Unicode-encoded character cells, and a Boolean type that is used to represent true values in char and 1.
Type |
Boolean |
Char |
|
short |
int |
long |
Float |
Double |
Storage requirements (bytes) |
1 |
2 |
1 |
2 |
4 |
8 |
4 |
8 |
1, the Boolean value range has two, true and false, used to determine the logical condition.
And the integer value and the Boolean value cannot be converted to each other.
2, the char type is originally used to represent a single character, but now some unicole characters can be described with a char value, and some unicole characters can use two char values.
Literal values of type char are enclosed in single quotation marks.
And Unicode escape sequences are processed before the code is parsed.
3, the integer is used to denote a number without a decimal part, it is allowed to be negative.
Java provides 4 integer types: Byte, short, int, long.
There is no unsigned (unsigned) type of int, short, long, or byte in Java.
And if you want to represent long shaping data, the latter is prefixed with L or L. If you want to represent a binary number, prefix 0B or 0b, or prefix 0 if you want to represent octal numbers, or 0X or 0x if you want to represent hexadecimal numbers.
Note that the value range for byte is: -128~127.
4. Floating-point types are used to represent numeric values that have a decimal part.
There are two types of floating-point in Java.
Double means that this type of data precision is twice times the type of float (double precision), and most applications use a double type.
A value of type float has a suffix f or f, and a floating-point value without a suffix f defaults to a double type.
Floating-point values can be represented in hexadecimal. For example, 0.125=2-3 can be represented as 0x1.0p-3, in 16 notation, p is used to denote an exponent, not e, and the mantissa is 16 binary, and the exponent is 10 binary. The cardinality of the index is 2, not 10.
All floating-point numeric computations follow the IEEE754 specification, which represents three special floating-point values for overflow and error conditions: positive infinity, negative infinity, and Nan.
If you do not allow any rounding errors in numeric calculations, this use should use the BigDecimal class.
--------------------------------------I'm a friendly divider--------------------------------------
Type conversions can be divided into two types, one for automatic type conversion and the other for forced type conversions.
Automatic type conversion
Automatic type conversion We can compare to be "Jiquanshengtian Legion".
If we are doing two operations on two values, if a is a value of type short, B is a numeric value of type int, then A is converted to int type when the calculation is made, so the result is also the data of type int.
sum = a+b;
So we can know that the direction of the automatic type conversion is from low to high, similar to:
Longchar---int, short,int Double , float
Here, there may be a question as to why short and char should be written separately. Aren't they all 2 bytes?
The reason is that the short type is a signed type, that is, it has a sign bit, but Char does not have a sign bit, that is, the char and the short type are the same amount of memory, but the range represented is different (the representation range of Char is 0~2^16-1 (0 to 65535), Short's representation range is -2^15 to 2^15-1 (? 32,768 to 32,767)).
Forcing type conversions
Forcing type conversions is kind of like going to the grocer's. A type conversion that might lose some information is allowed in Java, which needs to be implemented by forcing type conversions.
The syntax format for forcing type conversions is to give the target type that you want to convert in parentheses, followed by the name of the variable to be converted. Such as:
double d = 2.3333; int i = (int) D;
However, if you attempt to cast a numeric value from one type to another and beyond the range of the target type, the result is truncated to an entirely different value.
It is like not to turn into a Boolean or byte type.
--------------------------------------I'm a friendly divider--------------------------------------
Packing and unpacking
Boxing and unpacking are a pair of relative concepts.
Boxing means wrapping the basic types with their corresponding reference types so that they have the properties of the object.
Unpacking is the simplification of objects of reference types into data of value types.
Give me a chestnut:
// Packing int n = i; // Unpacking
Use Javap-v to decompile and then know that the two statements corresponding to the box and unboxing are not.
Then we look at the Integer.valueof method and the source code of the Integer.intvalue method:
Public Static Integer valueOf (int i) { if (i >= integercache.low && i <= integercache . High) return integercache.cache[i + (-integercache.low)]; return New Integer (i); }
Public int intvalue () { return value; }
Is this process of boxing a little familiar? For more details, take a look at the note I wrote earlier: https://www.cnblogs.com/NYfor2018/p/9482390.html
The process of unpacking is simple and non-cosmetic, and we can see that its return value is directly int.
But is it that the eight basic types of boxing process are similar to each other? No, but they are similar to the classification of the previous eight basic data types, that is, the boxing process of integer, short, Long, and Byte, the boxing process of double and float, The Boolean boxing process is similar to the character of the boxing process.
Below the source of the festival:
Public StaticInteger ValueOf (inti) {if(I >= integercache.low && i <=Integercache.high)returnIntegercache.cache[i + (-Integercache.low)]; return NewInteger (i);} Public StaticLong ValueOf (Longl) {Final intoffset = 128; if(l >= -128 && L <= 127) {//would cache returnlongcache.cache[(int) L +offset]; } return NewLong (l);} Public StaticShort ValueOf ( Shorts) {Final intoffset = 128; intSasint =s; if(Sasint >= -128 && sasint <= 127) {//must cache returnShortcache.cache[sasint +offset]; } return NewShort (s);} Public StaticByte ValueOf (byteb) {Final intoffset = 128; returnbytecache.cache[(int) B +offset];}
Public Static Double valueOf (double d) { returnnew double (d);}
Public Static Float valueOf (float f) { returnnew float (f);}
Public Static Boolean ValueOf (boolean b) { return (b? True:false);}
Public Static Character valueOf (char c) { if// must cache return charactercache.cache[(int) c]; } return New Character (c);}
So, when it comes to interviewing questions of the same type of data but with different values, the remedy should be the case.
Have questions about the basic data types can read this article, write very good: 69668807
Reference:
"Java Core technology I"
Https://www.cnblogs.com/dolphin0520/p/3780005.html
69668807
Basic data types of Java