The data types provided by Java are mainly divided into two categories: basic data types and reference data types.
Name | Size | Value range |
##byte type (byte) | 8bit | -128-127 (-2^7 to 2^7-1) |
short type (short integer type) |
16bit | -2^15 to 2^15-1 |
int type (integer) | 32bit | -2^ 31 to 2^31-1 |
64bit | -2^63 to 2^63-1 | |
32bit | ##double type (double precision floating point type) | |
64bit | ##char type (character type) | |
16bit | ##boolean type (Boolean type) | |
There are only two results, either "true" or "false" |
Reference data types in Java: Java is an object-oriented language, and all classes in Java are used. Interface and abstract class definitions are all Java reference data types. How to understand the basic data types and reference data types in Java: 1. The basic data types of Java are provided by the Java language itself The data type 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 requires the use of basic data types ; 3. In terms of memory relationship: Java’s memory is divided into two large blocks: stack memory and heap memory The stack memory is responsible for storing the basic data type variables and object reference variables in the method The heap memory is responsible for Store the data generated through the new keyword, that is, the attributes and methods in the class after the new keyword. Basic data types in Java are stored in stack memory, while the type names of reference data types are stored in stack memory, but the contents of reference data types are stored in heap memory. middle. The two are connected through addresses to achieve mutual access.
4. When data is converted from a small range to a large range, the JVM will automatically help us implement type conversion. For example: int i=10;long l=i;Similar to this data conversion, Java's virtual machine can automatically help us complete this work. However, when data is converted from a large range to a small range, forced type conversion needs to be manually added. If data overflow occurs during the conversion process, it will be changed according to the value range of a small range of data types. For example, if the integer -129 is assigned to byte, then the output byte value is 127; if the integer 128 is assigned to byte type, then the output should be -128.
Parameter passing in Java: Basic data types: public void test1(){ int i=10; long l; l=i; System.out.println(i);//输出值10 System.out.println(l);//输出值10 l=i+1; System.out.println(i);//输出值10 System.out.println(l);//输出值11 } Copy after login Since the basic data type is that the variable name and variable value are stored together in the stack memory, i and l The variables are independent of each other, and the assignment operation to l will not affect the i value. 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("《百年孤独》"); Book book2=new Book(); book2.setName("《围城》"); System.out.println("book1:"+book1.getName());//输出:book1:《百年孤独》 System.out.println("book2:"+book2.getName());//输出:book2:《围城》 book1=book2; book1.setName("《活着》"); System.out.println("book1:"+book1.getName());//输出:book1:《活着》 System.out.println("book2:"+book2.getName());//book2:《活着》 } } Copy after login
## For the first time, two new objects are book1 and book2. Their book titles are "One Hundred Years of Solitude" and "Fortress Besieged" respectively. Then through assignment, book1 also points to the same memory area as book2. At this time, whether the operation on book1 or book2 affects the same memory area. This is why the subsequent output is the same. In addition, since the memory space originally opened by book1 is not used, the JVM's garbage collection mechanism will process it and release these unused memory spaces. |
The above is the detailed content of Detailed introduction to Java data types and parameter passing. For more information, please follow other related articles on the PHP Chinese website!