This is the 8th day of my participation in the More text Challenge. For details, see more text Challenge
Java error prone point 1
If have the word of understanding error, implore everybody to point out!!
memory
Java automatically manages stacks and heaps, and programmers cannot directly set stacks or heaps.
- Stack memory:
- Some basic types of variables (int, short, long, byte, float, double, Boolean, char) and reference variables to objects (object handles).
- When a variable is defined, memory space is allocated on the stack. When the scope of the variable is exceeded, the memory allocated for the variable is automatically freed, and the memory space can be immediately used for other purposes.
- Access speed is faster than heap, second only to register
- Data can be shared
- The size and lifetime of the data in the stack must be determined and inflexible
- Heap memory
- Hold the objects and arrays created by new.
- Managed by the Java Virtual Machine’s automatic garbage collector.
- Once an array or object is created, you can also define a special variable in the stack whose value is equal to the array or object’s first address in the heap. This variable becomes a reference variable to the array or object. Use reference variables in the stack to access arrays or objects in the heap. When no reference variable points to it, it becomes garbage and can no longer be used, but it still occupies memory and is later released by the garbage collector at an unspecified time. A variable in the stack points to a variable in the heap, which is a pointer in Java!
- Runtime data area from which space is allocated for a class. These objects are created using the new, newarray, anewarray, and Multianewarray directives
- The memory size is allocated dynamically and the lifetime is not told to the compiler in advance
- Because memory is dynamically allocated at run time, access is slow.
The sample code
People object
package com.wangscaler;
public class People {
private int age;
private String name;
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(a) {
return "People{" +
"age=" + age +
", name='" + name + '\' ' +
'} '; }}Copy the code
The main function
package com.wangscaler;
public class Main {
public static void main(String[] args) {
// write your code here
int a = 10;
int b = 20;
String c = "wang";
Integer d = 30;
System.out.println(The value of c is + c + ", the memory address is" + System.identityHashCode(c));
System.out.println("The value of D is" + d + ", the memory address is" + System.identityHashCode(d));
System.out.println("The value of a is" + a);
System.out.println("The value of b is" + b);
chage(a, b);
System.out.println("The value of a is" + a);
System.out.println("The value of b is" + b);
People people = new People();
people.setAge(30);
people.setName("wang");
System.out.println("The value of people is" + people + ", the memory address is" + System.identityHashCode(people));
System.out.println(The value of people.name is + people.getName() + ", the memory address is" + System.identityHashCode(people.getName()));
System.out.println("The value of people.age is" + people.getAge() + ", the memory address is" + System.identityHashCode(people.getAge()));
People people1 = new People();
people1.setName("scaler");
people1.setAge(40);
System.out.println("The value of people1 is" + people1 + ", the memory address is" + System.identityHashCode(people1));
chagePeople(people, people1);
System.out.println("Value of people after the exchange" + people + ", the memory address is" + System.identityHashCode(people));
System.out.println("Value of People1 after exchange" + people1 + ", the memory address is" + System.identityHashCode(people1));
}
public static void chage(int a1, int b1) {
int c1 = a1;
a1 = b1;
b1 = c1;
System.out.println(The value of C1 is + c1);
System.out.println(The value of A1 is + a1);
System.out.println("The value of B1 is" + b1);
}
public static void chagePeople(People people2, People people3) {
People people4 = people2;
people2 = people3;
people3 = people4;
System.out.println("The value of people4 is" + people4 + ", the memory address is" + System.identityHashCode(people4));
System.out.println(The value of people3 is + people3 + ", the memory address is" + System.identityHashCode(people3));
System.out.println("The value of people2 is" + people2 + ", the memory address is"+ System.identityHashCode(people2)); }}Copy the code
The execution result
The value of c is wang and the memory address is356573597D has a value of30, the memory address is1735600054A value of a10A value of b20The value of c1 is10A value of a120A value of b110A value of a10A value of b20The value of people is people {age=30, name='wang'}, and the memory address is21685669The value of people.name is wang and the memory address is356573597People. The value of the age30, the memory address is1735600054People1 is People{age=40, name='scaler'}, and the memory address is2133927002People4 is People{age=30, name='wang'}, and the memory address is21685669People3 is People{age=30, name='wang'}, and the memory address is21685669People2 is People{age=40, name='scaler'}, and the memory address is2133927002People{age=30, name='wang'}, and the memory address is21685669People{age= people140, name='scaler'}, and the memory address is2133927002
Process finished with exit code 0
Copy the code
The principle of
-
Before 14 lines
A and B are both basic types, so only stack memory space is allocated, while String and Integer (Integer is a wrapper class for int, and Integer is actually a reference to an object) are only defined in stack memory as a special variable, which specifies the first address in heap memory.
-
Lines 15-17 and 34-41
After change is executed, the values of a and b are only passed to A1 and b1, without any effect on a and B.
At this point, the change method is executed, and only the values of A1 and b1 are exchanged. When the change is executed, a1 and B1 end their life cycle, and the space in the fixed memory will be freed.
-
18 to 27
When a People object is created, it allocates space in the heap and defines a special variable in the stack that points to the first address in the heap. When a value is assigned to the object, it points to the address of the value, so both c and people.name refer to the same heap address.
The process for creating People1 is the same as above
-
The remaining code
When you swap, because you are passing values, you pass the address of the heap memory. So just like a1, B1,c1, their exchange, it has nothing to do with people, people1.
An array of
The sample code
package com.wangscaler;
public class TestArray {
public static void main(String[] args) {
int[] data = new int[3];
int[] temp = new int[3];
System.out.println("The value of data is" + data + ", the memory address is" + System.identityHashCode(data));
System.out.println("The value of data is" + temp + ", the memory address is" + System.identityHashCode(temp));
data[0] = 99;
data[1] = 20;
data[1] = 30;
data = temp;
temp[0] = 100;
System.out.println("The value of data[0] is" + data[0] + ", the memory address is" + System.identityHashCode(data));
System.out.println("Temp [0] value is" + temp[0] + ", the memory address is" + System.identityHashCode(temp));
System.out.println("The value of data is" + data + ", the memory address is" + System.identityHashCode(data));
System.out.println("Temp value is" + temp + ", the memory address is"+ System.identityHashCode(temp)); }}Copy the code
The execution result
The value of data is [I@1540e19d and the memory address is 356573597. The value of data is [I@677327b6 and the memory address is 1735600054. The value of data[0] is 100 and the memory address is 1735600054 The value of temp[0] is 100, and the memory address is 1735600054. The value of data is [I@677327b6, and the memory address is 1735600054. The value of Temp is [I@677327b6, and the memory address is 1735600054Copy the code
Data and temp point to the same heap memory address, so the output push address is the same, [I@677327b6]. If you modify the data of either array, the other will change.
Floating point numbers cannot be used as loop variables
Because of the precision problem of floating point numbers, when the program runs, it will lose accuracy. If the program uses floating point numbers as loop variables, it often cannot achieve the desired effect
The sample code
public class TestFloat {
public static void main(String[] args) {
float data = 2000000010f;
for (float i = 2000000000f; i <= data; i++) { System.out.println(i); }}}Copy the code
The results
2.0 e9
2.0 e9
2.0 e9
2.0 e9
2.0 e9.// Infinite loop
// Change I <= data to I
Copy the code
The principle of
1, here f converts binary representation of 2000000000 to 1110111001101011001010000000000, in computer storage, you need to use the binary scientific notation, said computer don’t know decimal number.
2, which is 1110111001101011001010000000000 = 1.110111001101011001010000000000 * 2 ^ 30
3. The exponent is 30+127(the single-precision offset agreed by IEEE754)=157, which is converted to 10011101 in binary
4. The mantissa part is cut to 23 digits after the decimal point (the single precision mantissa length agreed by IEEE754)
4, so 2000000000F becomes 0(sign bit 1, 0 is positive, 1 (negative) 10011101 (8) 11011100110101100101000 (23 mantissa part) is stored in the memory of binary for 01001110111011100110101100101000
5, the same 20000000010 f — — — — > 1110111001101011001010000001010-30 – > > 1.110111001101011001010000001010 * 2 ^ index for the same
6, 20000000010 f said in memory for 0100111011101110011010110010100
7. Comparison shows that 2000000000F and 20000000010F have the same binary representation in memory. So in this case 2000000000f and 2000000010f are equivalent to the program, so we can’t get the desired effect (print ten times).