


Detailed comparison of basic type variables and reference type variables in Java
Basic type:
It goes without saying that the basic type is a number, a character or a Boolean value.
Reference type:
is an object type, what is the value? Its value is a reference to the memory space, which is the address. The memory pointed to holds a value or a set of values represented by the variable.
Example:
int a;
a=250; //When declaring variable a, the system allocates space to a.
The reference type is not. Only the reference space is allocated to the variable, and the data space is not allocated, because no one knows what the data is, integer, character? Let's look at an error example:
MyDate today;
today.day = 4; //An error occurred because the data space of the today object is not allocated.
Then how do we assign a value to it? After declaration, a reference type variable must open up data space through instantiation before the object pointed to by the variable can be accessed. For example:
MyDate today; //Allocate the variable to a space to save the reference
today = new MyDate(); //This sentence is a 2-step process, first execute new MyDate( ), open up a data space for the today variable, and then perform the assignment operation
//Assignment of reference variables
MyDate a, b; #a = new MyDate(); //Open up the data space of the MyDate object, and assign the first address of the space to a
b = a; //Write the address in the storage space of a to the storage of b In the space
As shown below
Reference type:
Except when a function passes a value, it is "pass by reference". Any time "=" is used to assign a value to an object variable, it is "pass by reference".
Value transfer:The transfer of basic types belongs to value transfer, just like C language, when Java’s basic data types (such as
int, char, double, etc.) are passed to the function body as entry parameters, the passed parameters become local variables inside the function body. This local variable is a copy of the input parameter. All functions
The internal operations of the number body are all operations on this copy. After the function execution is completed, this local variable has completed its mission, and it will not affect the variables used as input parameters. Parameter passing in this way is called "value
Pass"./*define a class*/class MyDate{
String Year;
String Month;
String Day;
String Date; public MyDate(){
Year = "1900";
Month ="01";
Day = "01";
Date = Year + "." + Month + "."+ Day;
} public String toString(){ return Date;
}
}public class ObjRef{
MyDate mydate1 = new MyDate();
MyDate mydate2 = mydate1;// = 使 mydate1和 mydate2将指向同一内存空间 //MyDate mydate2 = new MyDate();//从新new一个对象,则mydate1和mydate2是指向不同的内存空间的: //引用空/间和数据空间都不一样
public void changeObj(MyDate inObj){
inObj.Date = "2007.09.26";
}
public static void main(String[] args) {
ObjRef oRef = new ObjRef();
System.out.println("Before call changeObj() method: " + oRef.mydate1);
oRef.changeObj(oRef.mydate1);
System.out.println("After call changeObj() method: " + oRef.mydate1);
System.out.println("After call changeObj() method: " + oRef.mydate2);//验证“= mydate1和 mydate2将指向同一内存空间“这一结论
}
}
Related articles:
The above is the detailed content of Detailed comparison of basic type variables and reference type variables in Java. For more information, please follow other related articles on the PHP Chinese website!

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
