Java data type and parameter transfer, java data type transfer
Java provides two types of data: Basic Data Type and reference data type.
Basic Data Types in Java
Name |
Size |
Value Range |
Byte type (in bytes) |
8bit |
-128-127 (-2 ^ 7 to 2 ^ 7-1) |
Short Type (short integer) |
16bit |
-2 ^ 15 to 2 ^ 15-1 |
Int type (integer) |
32bit |
-2 ^ 31 to 2 ^ 31-1 |
Long (long integer) |
64bit |
-2 ^ 63 to 2 ^ 63-1 |
Float Type (single precision floating point type) |
32bit |
|
Double (double Floating Point) |
64bit |
|
Char (character type) |
16bit |
|
Boolean (boolean) |
True and false |
There are only two results, either true or false" |
Reference data type in Java:
Java is an object-oriented language. All classes, interfaces, and abstract classes used in Java belong to the Reference Data Types of Java.
How to understand the basic data types and Reference Data Types in Java:
1. The basic data type of Java is the data type provided by the Java language itself and does not need to be defined by the user;
2. the reference data type in Java is defined by the user, and the definition of the reference data type needs to use the basic data type;
3. In terms of memory relationships:
Java memory is divided into two major blocks: stack memory and heap memory.
Stack memory is responsible for storing the basic data type variables in the method and the reference variables of objects.
Heap memory is used to store data generated by the new keyword, that is, the attributes and methods in the class after the new keyword.
In Java, the basic data types are stored in the stack memory, while the referenced data types are stored in the stack memory, but the referenced data types are stored in the heap memory. The two are connected through addresses to achieve mutual access.
4. When the data is converted from a small range to a large range, JVM will automatically convert the data type. For example:Int I = 10; long l = I;Similar to this type of data conversion, Java virtual machines can automatically help us complete this job. However, when data is converted from a large range to a small range, you need to manually add a forced type conversion. If data overflow occurs during the transition, the value range of the small data type is changed. For example-129AssignedByteThe output byte value is127If the integer128AssignedByteType, the output is-128.
Passing parameters in Java:
Basic Data Type:
Public void test1 () {int I = 10; long l; l = I; System. out. println (I); // the output value is 10 System. out. println (l); // the output value is 10 l = I + 1; System. out. println (I); // the output value is 10 System. out. println (l); // output value 11}
Because the basic data type is that the variable name is stored in the stack memory together with the variable value,IAndLThese two variables are independent of each other.LThe value assignment operation does not affectIValue.
Reference data type:
Public class Book {private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}} public class Test {public static void main (String [] args) {Book book1 = new Book (); book1.setName ("Hundred Years of loneliness"); book book2 = new Book (); book2.setName ("besieged city"); System. out. println ("book1:" + book1.getName (); // output: book1: "A hundred years of loneliness" System. out. println ("book2:" + book2.getName (); // output: book2: besieged city book1 = book2; book1.setName ("aliyun"); System. out. println ("book1:" + book1.getName (); // output: book1: aliyun System. out. println ("book2:" + book2.getName (); // book2: alive}
For the first time, two new objects were created: book1 and book2, whose names were "Hundred Years of loneliness" and "besieged city ". Then, with a value assignment, book1 points to the same memory area as book2. In this case, operations on book1 and book2 will affect the same memory area. That is why the output is the same. In addition, because the memory space originally opened by book1 is not used, the JVM's garbage collection mechanism will process it and release the unused memory space.