风也温柔

计算机科学知识库

The Memory Layout Of Java Integer 1.1.1 And The Formal Analysis Of Integer %

  1. Basic data types 1.1 integers 1.1.1 integers

  <pre class="brush:java;">
int a = Integer.MAX_VALUE;//int最大值
int b = Integer.MIN_VALUE;//int最小值</pre>

  int occupies 4 bytes in java and has nothing to do with the operating system. This is also the portability of java.

  The range that int can represent is -231~231-1. Overflow occurs if the maximum value is exceeded or the minimum value is less than.

  <pre class="brush:java;">
public static void main(String[] args) {

        int a = Integer.MAX_VALUE;//int最大值
        int b = Integer.MIN_VALUE;//int最小值
        System.out.println("MAX_VALUE="+a);
        System.out.println("MIN_VALUE="+b);
        a = a+1;
        b = b-1;
        System.out.println("---------------");
        System.out.println("MAX_VALUE+1="+a);
        System.out.println("MIN_VALUE-1="+b);

}
</pre>

  在这里插入图片描述

  We found that the maximum value of int plus 1 will equal the minimum value of int, and the minimum value of int minus 1 will be equal to the maximum value of int. This situation exists not only in int, but also in long and short

  在这里插入图片描述

  1.1.2 Long integer: long

  long occupies 8 bytes, and the range that can be represented is -263~-263-1

  <pre class="brush:java;">
long a = 100L;
long b = 100l;
long c = 100;</pre>

  When creating a long variable, all three a, b, and c can be used. When we generally use the first one, it is easier to identify when the uppercase L is a long integer.

  1.1.3 Short Integers: Short

  short occupies 2 bytes, and the range that can be represented is -215~215-1

  The range that short can represent is -32768~32767, which is relatively small and generally not recommended, so we will use int and long more in the future.

  1.2 floating point numbers 1.2.1 double precision floating point numbers: double precision

  <pre class="brush:java;">
public static void main(String[] args) {

        int a1 = 1;
        int a2 = 2;
        double b1 = 1.0;
        double b2 = 2.0;
        System.out.println("a1/a2="+a1/a2);
        System.out.println("b1/b2="+b1/b2);

}
</pre>

  在这里插入图片描述

  In java, int divided by int can only get int type because the fractional part is rounded. If you want to get decimals, you need to use floating point numbers to calculate.

  Check out another interesting code

  <pre class="brush:java;">
public static void main(String[] args) {

        double a = 1.1;
        System.out.println(a*a);
re>

  在这里插入图片描述

  Here a*a is theoretically equal to 1.21 but why not here?

  1. Although the double in Java is also 8 bytes, the memory layout of floating-point numbers is very different from that of integers, and the data range cannot be simply represented in the form of 2n.

  2. Java's double type memory layout follows the IEEE 754 standard (same as C language), trying to use limited memory space to represent potentially infinite decimals, potentially

  There must be some margin of precision error, there are no exact decimal places in the computer

  1.2.2 Single-precision floating-point numbers: float

  Flaot occupies 4 bytes

  <pre class="brush:java;">
public static void main(String[] args) {

        float a1 = 1.1;
        float a2 = (float)1.1;
        float b = 1.1F;
        float c = 1.1f;

}
</pre>

  在这里插入图片描述

  Why is there an error here? Because Java is a strongly typed language, it is more secure. In Java, decimals are of type double by default. When creating a floating point variable, you need to add an F(f) or cast after the decimal point.

  But the precision that float can represent is very small, so we generally use double

  1.3 Character type variable char

  The char in java is different from the C language, it occupies two bytes, the character in the computer is essentially an integer. In C, ASCII is used to represent characters, while in Java, Unicode is used to represent characters. Therefore, one character occupies two bytes, representing more types of characters, including Chinese.

  <pre class="brush:java;">
public static void main(String[] args) {

        char a = 97;
        char b = 65;
        char c = '好';
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

}
</pre>

  在这里插入图片描述

  In Java, a character type is just a character type. Don't link with numbers. Remember that character types in Java cannot give negative numbers.

  How many bytes does int occupy in java _how many bytes does int occupy in java _how many bytes does int occupy in c

  在这里插入图片描述

  1.4 Byte type variable byte

  byte occupies 1 byte, and the range that can be represented is -128~127

  A byte cannot be assigned a number outside its range, so other types can also be inferred.

  Look at a piece of code:

  在这里插入图片描述

  Here b3 and b5 report an error, but b4 does not report an error?

  1. This is because when code b3 is executed, both b1 and b2 are variables. The compiler doesn't know what's in there. To be on the safe side, report an error.

  2. And b4 does not report an error because 5+10 has been replaced by 15 at compile time, which is equivalent to b4 = 15.

  3. Why does b5 report an error? This is where plastic surgery is involved. How many bytes are int in java ,

  Briefly talk about integer promotion: for example, when the variable is less than 4 bytes, the bytes here will be promoted to integer int before calculation. And byte is a byte that cannot receive int type variables, so an error is reported. I'll mention plastic reinforcement later

  1.5 Boolean variable boolean

  <pre class="brush:java;">
boolean a = false;
boolean b = true;</pre>

  1. The boolean type in java has only two values, false and true.

  2. The boolean and int types in java cannot be converted to each other, there is no 1 for true java integer 1.1.1 memory layout and integer % formal analysis, 0 for false usage

  3. The boolean type JVM implementations occupy 1 byte and some occupy 1 bit. size not specified

  1.6 String type variable String

  Java strings can be concatenated with +

  <pre class="brush:java;">
public static void main(String[] args) {

        String str1 = "这是一个字符串";
        String str2 = "java";
        String tmp = str1 + str2;
        System.out.println(tmp);

}
</pre>

  在这里插入图片描述

  You can also splice numbers

  <pre class="brush:java;">
public static void main(String[] args) {

        String str1 = "这是一个字符串";
        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);

}
</pre>

  在这里插入图片描述

  When outputting, if the front is a string, the following numbers will be spliced ​​into characters, which can be operated or spliced ​​according to your own needs.

  1. Java uses double quotes + several characters to represent string literals.

  2. Unlike the above types, String is not a primitive type, but a reference type (to be introduced in a later article).

  2. Constants

  The variables discussed above are variables of various rules, and each type of variable also corresponds to a constant of the same type.

  Constant means that the type cannot be changed at runtime.

  Constants come in two main forms

  2.1 Literal Constants

  在这里插入图片描述

  2.2 … constants modified by the final keyword

  在这里插入图片描述

  The final modified constant cannot be modified directly

  3. Type Conversion and Numerical Conversion 3.1 Type Conversion

  How many bytes does int occupy in java _how many bytes does int occupy in java _how many bytes does int occupy in c

  Java is a strongly typed programming language. When variables of different types are assigned to each other, there will be strict checks. Let's take a look at the following example

  在这里插入图片描述

  long represents a larger range, int can be assigned to long, long cannot be assigned to int.

  double represents a larger range, int can be assigned to double, but double cannot be assigned to int.

  Conclusion: Assignment between variables of different numeric types means that a smaller-scoped type can be implicitly converted to a larger-scoped type, but not vice versa.

  3.1.1 Mandatory

  在这里插入图片描述

  Conclusion: Use (type) to cast double type to int. but

  1. Coercion may result in loss of precision. As in the above example, 20.5 becomes 20 after the assignment, and the part after the decimal point is ignored.

  2. Coercion does not guarantee success, and cannot be coerced between unrelated types

  在这里插入图片描述

  For example, two completely unrelated types, int and boolean, cannot be cast.

  Type conversion summary:

  1. Assignment between variables of different numerical types, indicating that a type with a smaller range can be implicitly converted to a type with a larger range.

  2. If you need to assign a type with a large range to a type with a small range, type conversion is required, but precision may be lost.

  3. When assigning literal constants, Java automatically checks the range of numbers.

  3.2 Attribute improvement

  在这里插入图片描述

  Conclusion: When int and long are mixed, int will be promoted to long, and the result is still of type long. You need to use a variable of type long to

  Receive the result. If you must use an int to receive the result, you need to use cast.

  Let's look at another example, which was mentioned earlier in the byte type, and will be described in detail here.

  在这里插入图片描述

  Conclusion: byte and byte are both of the same type, but a compilation error occurs. The reason is that although a and b are both bytes, the calculation of a+b will first upgrade both a and b to int, and then perform the calculation to obtain the result is also int, and then assign it to c, the above error will occur.

  Because a computer's CPU typically reads and writes data from memory in 4-byte units. For the convenience of hardware implementation, types such as byte and short less than 4 bytes will be upgraded to int first, and then participate in the calculation again.

  In simple terms, type promotion is to speed up the cpu running speed

  If you have to do this calculation, you need to force it.

  Type promotion summary:

  1. Mixed operation of different types of data In java, int occupies several bytes , and a small range will be upgraded to a large range.

  2. For short and byte types less than 4 bytes, they will be upgraded to 4 bytes int first, and then calculated.

  Four, int and String mutual conversion 4.1int to String

  <pre class="brush:java;">
public static void main(String[] args) {

        int a = 10;
        //方法1
        String str1 = 10+" ";
        //方法2
        String str2 = String.valueOf(a);

}
</pre>

  4.2 String to int

  <pre class="brush:java;">
public static void main(String[] args) {

        String str = "123456";
        int b = Integer.valueOf(str);
        int c = Integer.parseInt(str);

}
</pre>

  V. Summary

  在这里插入图片描述

  There are 8 built-in data types, and each built-in type corresponds to a wrapper class, which will not be explained in depth here. Simply put, it is convenient for data processing

  Thanks for reading!

  This is the introduction of this article on the basic details of java data type knowledge points summary. For more related java data types summary, please search for previous articles on the Self-learning Programming Network or continue to browse related articles below. I hope you will support the self-learning programming network in the future!

  文章来源:https://www.zxbcw.cn/post/210419/