In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you a sample analysis of Java data types, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Basic data type 1.1 shaping 1.1.1 intint a = Integer.MAX_VALUE;//int maximum int b = Integer.MIN_VALUE;//int minimum
Int occupies 4 bytes in java, every relationship with the operating system. This is also the portability of java.
The range that int can represent is-231-1, and overflow will occur if the maximum value is exceeded or the minimum value is less than.
Public static void main (String [] args) {int a = Integer.MAX_VALUE;//int maximum int b = Integer.MIN_VALUE;//int minimum System.out.println ("MAX_VALUE=" + a); System.out.println ("MIN_VALUE=" + b); a = aqui1; b = bmer1 System.out.println ("-"); System.out.println ("MAX_VALUE+1=" + a); System.out.println ("MIN_VALUE-1=" + b);}
We find that the maximum value of int plus 1 equals the minimum value of int, while the minimum value of int minus 1 equals the maximum value of int. This situation exists not only in int, but also in long and short
1.1.2 long plastic surgery: long
Long occupies 8 bytes and can be expressed in the range of-263 bytes 263-1.
Long a = 100L * long b = 100l *
When creating a long shaping variable, aQuery, b and c are all available. When we generally use the first one, capitalization L is better to identify its duration shaping.
1.1.3 short plastic surgery: short
Short occupies 2 bytes and can be expressed in the range of-215. 215-1.
The range that short can express is-32768-32767, which is relatively small and generally not recommended, so we will use more int and long in the future.
1.2floating point type 1.2.1 double precision floating point type: doublepublic static void main (String [] args) {int A1 = 1; int a2 = 2; double b1 = 1.0; double b2 = 2.0; System.out.println ("A1 a1/a2 a2 =" + a1/a2); System.out.println ("b1System.out.println b2 =" + b1/b2);}
In java, int divided by int can only get the int type, because the decimal part will be dropped. If you want to get a decimal, you need to calculate it with a floating point number.
Let's take a look at an interesting code.
Public static void main (String [] args) {double a = 1.1; System.out.println (Avoca);}
Here apoca is theoretically equal to 1.21, but why not here?
Although the double in 1.Java is also 8 bytes, the memory layout of floating point numbers is very different from that of integers, and the data norm can not be expressed simply in the form of 2n.
The memory layout of 2.Java 's double type conforms to the IEEE 754 standard (like the C language). Try to use limited memory space to represent potentially infinite decimals.
There must be a certain margin of accuracy error, and there is no exact number of decimal numbers in the computer.
1.2.2 single precision floating point type: float
Flaot occupies 4 bytes
Public static void main (String [] args) {float A1 = 1.1; float a2 = (float) 1.1; float b = 1.1F; float c = 1.1f;}
Why did you report an error here? Because Java is a strongly typed language, it is more secure. Decimals are double by default in Java. When you create a float variable, you need to add an F (f) or cast after the decimal.
However, the precision of float is relatively small, so we usually use double.
1.3 character type variable char
Unlike C language in java, char occupies two bytes. The character in computer is essentially an integer. ASCII is used to represent characters in C language, while Unicode is used to represent characters in Java. Therefore, a character occupies two bytes, representing more types of characters, including Chinese.
Public static void main (String [] args) {char a = 97; char b = 65; char c = 'good'; System.out.println (a); System.out.println (b); System.out.println (c);}
In Java, character types are character types that are not linked to numbers. Remember that character types in java cannot be given negative numbers.
1.4 byte type variable byte
Byte occupies 1 byte and can be expressed in the range of-128 to 127.
Byte cannot assign numbers beyond its range, so it can be inferred that other types are the same.
Let's look at a piece of code:
Here b3 and b5 are wrong, but b4 is not wrong?
1. This is because when executing b3 code, b1 and b2 are both variable compilers and do not know what is inside, so they report an error for the sake of security.
two。 While b4 does not report an error because 5: 10 has been replaced with 15 at compile time, which is equivalent to b4 = 15.
3. Then why does b5 report an error? Plastic surgery and promotion are involved here.
Briefly talk about shaping promotion: when the variable is less than 4 bytes, for example, the byte here will promote it to shaping int before calculating. Byte is a 1-byte variable that cannot receive an int type, so an error is reported. Plastic surgery and lifting will be mentioned later.
1.5 Boolean type variable booleanboolean a = false;boolean b = true
1. In java, there are only two values for Boolean types, false and true.
Boolean and int types in 2.java cannot be converted to each other. There is no use of 1 for true and 0 for false.
Some JVM implementations of the 3.boolean type occupy 1 byte and some occupy 1 bit. The size is not clearly specified.
1.6 string type variable String
Java strings can be concatenated with +
Public static void main (String [] args) {String str1 = "this is a string"; String str2 = "java"; String tmp = str1 + str2; System.out.println (tmp);}
You can also splice numbers.
Public static void main (String [] args) {String str1 = "this is a string"; String str2 = "java"; String tmp = str1 + str2; System.out.println (tmp); int a = 10; int b = 20; System.out.println (tmp+a+b); System.out.println (a+b+tmp);}
When outputting, if the front is a string, the following numbers will be spliced as characters, and can be calculated or stitched according to their own needs.
1.Java uses double quotes + several characters to represent the literal value of the string.
two。 Unlike the above types, String is not a basic type, but a reference type (which will be discussed in a later article).
Second, constant
All kinds of regular variables are discussed above, and each type of variable corresponds to the same type of constant.
Constant means that the runtime type cannot be changed.
Constants are mainly embodied in the following two forms
2.1 literal constant
2.2... Constant modified by final keyword
The constant modified by final cannot be modified directly.
Type conversion and numerical conversion 3.1 Type conversion
As a strongly typed programming language, java teaches strict checking when variables of different types are assigned to each other. Let's take a look at Liezi.
Long represents a wider range. You can assign int to long, but not long to int.
Double represents a wider range. You can assign int to double, but not double to int.
Conclusion: the assignment between variables of different numerical types means that smaller types can be implicitly converted to larger ones, and vice versa.
3.1.1 cast
Conclusion: the double type can be forcibly converted to int. But
1. Forced type conversion may result in loss of precision. As in the example just now, after assignment, 20.5 becomes 20, and the part after the decimal point is ignored.
two。 Cast may not be successful, and it cannot be forced between unrelated types
For example, the two completely unrelated types, int and boolean, cannot be forcibly changed.
Type conversion summary:
1. Assignment between variables of different numeric types, indicating that smaller types can be implicitly converted to larger ones.
two。 If you need to assign a type with a large range to a type with a small range, you need to force a cast, but precision may be lost.
3. When you assign a literal constant, Java automatically checks for a range of numbers.
3.2 numerical improvement
Conclusion: when int and long are mixed, int will be upgraded to long, and the result is still of type long, which requires the use of variables of type long.
Receive the results. If you have to use int to receive results, you need to use cast.
Let's take a look at another example, which was mentioned earlier in the byte type, so let's talk about it in more detail here.
Conclusion: both byte and byte are of the same type, but there is a compilation error. The reason is that although both an and b are byte, calculating a + b first promotes both an and b to int, and then calculates, and the result is also int, which is assigned to c.
Because the CPU of the computer usually reads and writes data from memory in 4 bytes. For hardware convenience, types such as byte and short, which are less than 4 bytes, will be promoted to int before participating in the calculation.
To put it simply, type promotion is to speed up the operation of cpu.
If you have to calculate in this way, you need to make a strong turn.
Summary of type promotion:
1. Different types of data mixed operations, the range of small will be promoted to a large range.
two。 For short, byte, which is smaller than 4 bytes, will be promoted to 4 bytes of int before operation.
4. Conversion between int and String 4.1 int to Stringpublic static void main (String [] args) {int a = 10; / method 1 String str1 = 10 + "; / / method 2 String str2 = String.valueOf (a);} 4.2 String to intpublic static void main (String [] args) {String str =" 123456 " Int b = Integer.valueOf (str); int c = Integer.parseInt (str);} above is all the content of the article "sample Analysis of Java data types". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.