Java value transfer

Source: Internet
Author: User

Java FAQ series (v) -- has the value passed or the reference passed?

 

Variable types in Java:

Basic type variables: including char, byte, short, Int, long, float, double, and Boolean.
Reference type variables include classes, interfaces, and arrays (basic type arrays and object arrays ).

When a variable of the basic type is passed as a parameter to a method, the Java Virtual Machine copies the value and then transmits the copied value to the method. After the method is executed, the lifecycle of the local variable ends.

When a referenced variable is passed as a parameter to a method, the Java Virtual Machine also copies the reference held by the variable and then passes it to the local variable created for the method by the Java Virtual Machine, the two variables point to the same object.

 

The Java programming language 2.6.5. parameter Values section: All parameters to methods are passed "by value. "In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments ...... You shoshould note that when the parameter is an object reference, it is the object reference not the object itself that is passed "by value. "Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. but if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it.

 

 

 

Supplement: string type

String is not the basic data type of Java and is defined as follows:

PublicFinalClass string extends object implements serializable, comparable <string>, charsequence

 

Their values cannot be changed after they are created.

 

Run the following code:

Public class test {

Public static void oprator (string test ){
Test. Replace ('A', 'E ');
Test. tolowercase ();
}

Public static void main (string [] ARGs ){
String STR = "Bea ";
Oprator (STR );
System. Out. println (STR );
}
}

 

Bea output

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.