stay Java in , Variables fall into two categories :

1. Basic type variables ,java It's a copy of the delivery

2. All object variables , The essence of a reference copy is to copy a pointer to an address

a. Basic type references

public class BasicTransmit {
public static void test(boolean test){
test =! test;
System.out.println("in test : test = "+test);
} public static void main(String[] args) {
boolean test = true;
System.out.println("Before test : test = "+ test);
System.out.println("After test : test = "+test); // stay java in , For basic type variables (int,long,double,float,byte,boolean,char)
//java It's a copy of the value .

The result of the operation is :

Before test : test = true
in test : test = false
After test : test = true

b. References to objects (String Not a basic data type , But the object )

public static void test (StringBuffer str){
} public static void main(String[] args) {
StringBuffer string = new StringBuffer("Hello");
// Anticipate ,String It's a reference ,java For the reference form, the variables that pass the object type are , It's actually going to quote
// As a copy passing method function . So what does the reference copy in this function point to ?
// It's the address of the object , Find the address by referencing the copy and modify the value of the address , It changes the value of the object

Running results :Hello,wrold!

public static void test (String str){
str = "world";
} public static void main(String[] args){
String string = "Hello";
} // Why is this so ? That's because when it comes to execution str="world" when , The process is : First, the system will automatically generate new string object
// And set the value of this new object to "world", Then assign the reference of this object to str
// understand : by str This key is pointing to "hello" This warehouse's , But now I'm asking for str This key points back to "world"
// We have to be clear that :String Class is final Type of , therefore , You can't inherit or modify this class .
//str="world", In fact, it implicitly makes java Make a new one String object , Since the objects are all new , That's the same as the original
//"Hello" It doesn't matter .
// When the function ends ,str The effect disappears , The contents of the original memory address remain unchanged , So the print is still Hello //StringBuffer The resulting block of memory , The relevant operations of addition, deletion and modification are all carried out in it , So add a sentence to it "world"
// Still on the same memory address ,str The reference to the value has not changed .

c. References to objects

public class ValueTest {
public void first(){
int i = 5;
Value v = new Value();
v.i = 25;
second(v ,i);
} public void second(Value v ,int i){
i = 0;
v.i = 20;
Value val = new Value();
v= val;
System.out.println(v.i + "" + i);
} public static void main(String[] args) {
ValueTest t = new ValueTest();
} class Value{
public int i = 15;

The result is :15 0 20

summary : stay java Both basic data types and objects are copies of references or addresses , When the return function is , The value doesn't change . Unless the value in the space pointed to by this address has been changed during the operation of the function .

Java More articles on data types and object references

  1. “ Cannibalism ” Those of Java Noun : object 、 quote 、 Pile up 、 Stack

    Remember the middle school textbook , There is a piece called < A Madman's Diary > text : At that time, I couldn't understand Lu Xun's central idea of writing this article , Just feel full of “ Cannibalism ” It's depressing : The teacher spoke impassioned on the platform , Most of my classmates are like me , In front of the textbook ...

  2. java Defining classes object , quote , The pointer

    java It's based on object-oriented programming , So there's the concept of class and object , Class is divided into ordinary class and abstract class . One . Defining classes Class by N individual Constructors   Member variables   Method composition , Can not define , It can also be defined according to grammar N individual . [ Modifier ] class Class name { structure ...

  3. Talking about Java Objects and references in

    Talking about Java Objects and object references in stay Java in , There is a group of nouns that often appear together , They are “ Objects and object references ”, Many friends are learning Java You may often confuse this 2 A concept , I think they're one thing , In fact, it's not . Today we're going to come together ...

  4. Java Objects and references in

    <Java Programming idea > There is a passage about the object in : " In popular parlance , Each object is a class (class) An example of (instance), here ,' class ' Namely ' type ' A synonym for ." Jane ...

  5. java Objects in the Method quote What do you mean by some abstract concepts ?

    2020-03-14 I'm a little busy these days , I haven't updated my blog for a long time , I'll stick to it after that , Progress with everyone . I've been learning all this time java, I believe I just started to learn java Little white of , When I first came into contact with some abstract concepts, I must be with you ...

  6. Java The foundation is necessary -- Stack 、 Pass value by reference 、 Garbage collection, etc

      stay Java in , Object as a function parameter is passed by value or reference ?String str = "abc" And String str = new String("abc&quo ...

  7. Java: Pass value or pass reference ?

    This is a Java The classic question of , Most people from C,C++ Introduction to language ,C There are three ways of language transmission : Value passed , Address passing and reference passing . In detail C Language pointer , Quoted from my personal understanding , See link . Java All operations are value transfer operations ! It's all value transfer operations ...

  8. Java memory management -Stackoverflow Question and answer -Java Pass value or pass reference ?( 11、 ... and )

    Don't build a platform in quicksand , I'll pay you back sooner or later . Be a positive person code . Change bug. Improve yourself I have a paradise , Programming Oriented , Spring flowers ! This paper is a guide map : One . Thinking from a question stay Stack Overflow  See such a problem ...

  9. JAVA The journey ( eighteen )—— Object wrapper classes for basic data types , Collections framework , data structure ,Collection,ArrayList, iterator Iterator,List Use

    JAVA The journey ( eighteen )-- Object wrapper classes for basic data types , Collections framework , data structure ,Collection,ArrayList, iterator Iterator,List Use JAVA Define everything as an object , And we want to use data ...

Random recommendation

  1. html The basic label of

    The full page is created by html nesting head.body constitute example : <!DOCTYPE html><html lang="en"> <head> < ...

  2. SharePoint 2013 Direct to AD After the group is granted permission ,AD Users in the group still can't log in SharePoint, Prompt no permission

    Give one directly all person Of AD I got a read jurisdiction , And then we call it “all beijing” The group of is added to all person In the group , however all beijing People in the group can't log in sharepoint, carry ...

  3. find Detailed command

    find Detailed command   source : ChinaUnix Blog date : 2008.07.25 16:04 ( There are comments )  I want to comment on   [url=] ...

  4. solve VS2013+IE11 debugging DevExpress ASP.NET MVC Performance problems of

    Will a MVC The project from 12.2 Upgrade to 14.2,VS2012 Rise to 2013, Discover the use of IE11 Debugging is very slow and stuck ,CPU Occupy 100%, Later, after exclusion , Only found DevExpress Of MVC The project has this problem . Last in Dev ...

  5. linux Process communication between

    Interprocess communication mode :                    Data exchange mechanism with host process : pipe( Nameless pipe ) / fifo( Famous pipeline )/ message queue( Message queue ) And shared memory . The essential foundation : f ...

  6. shell+curl Monitoring website pages ( Domain access status ), And make use of sedemail Send E-mail

    At the request of leaders , Monitor the domain name access of several major sites of the company . Let's share a monitoring script , And make use of sendemail Send by mail . The monitoring script is as follows : The following is to write a multi-threaded website state detection script , Read the site address directly from the file , then ...

  7. R Language notes 4-- visualization

    Pick up R Language notes 3-- example 1 R Visualization functions in languages fall into two categories , Exploratory Visualization ( Strange data sets , Don't understand , You need to explore the information inside : Focus on speed , Convenient tools ) And interpretive Visualization ( Fully understand the data set , The story needs to be explained to others : Emphasis on comprehensive ...

  8. xcode Single step debugging shortcut key

    xcode Single step debugging shortcut key f6, But grinding people f6 It's used to adjust the brightness of the keyboard , In system preference , Keyboard settings inside , Just do the following .

  9. MVC4 load zTree Tree widget

    Preface : Learning to use for the first time MVC frame , Found a hand training project , load zTree Tree widget . Next, I will explain my experience of this practice step by step to record . If there is any mistake , I hope you can help me correct , thank you . First step : utilize VS2010 Create a new one MV ...

  10. IS The road to innovation -- Duchang company is an enabling company HIT The way of enterprise development

    ◆◆ Preface In recent days, , Shanghai Ruijin Hospital told us that :“ Our hospital from 2000 He began to develop his own hospital information system , We have found a way of sustainable information development . Five systems have been built ,284 Subsystem . But our hospital is still trying to promote the informatization of hospital with electronic medical record as the core ...