2013年12月1日星期日

Some doubt, wait for the god doubts

package org.bruce.foundation.test;

public class TestXX {

public static void main(String args[]) {
Boolean bValue = new Boolean(false);
String str = new String("false");
Integer integer = Integer.valueOf(3);

change(bValue);
System.out.println(bValue);

change(str);
System.out.println(str);

change(integer);
System.out.println(integer);
}

public static void change(Integer integer) {
++ integer;
}

public static void change(Boolean bValue) {
bValue = new Boolean(true);
}

public static void change(String str) {
str = "true";
}
}

what the results are ? Why is there such a result?
------ Solution ---------------------------------------- ----
world all programming languages ​​have only the value passed so-called " reference" , the value of its underlying implementation is still passed . To change (String str), Integer, Boolean use the same ideas to consider:
1, when the implementation of the seventh row
String str = new String("false");

when memory is this:


2, when calling line 28 change (String str) function when :
public static void change(String str) {
        str = "true";
    }

memory below the stack memory is characterized by " extend from high to low , and continuous , complexity is O (1), second only to register ", and heap memory is characterized by" extend from low to high , always looking for a static list of idle region complexity is O (N), it is slower than the stack memory , and not necessarily in a row " :

3, when the change (String str) function is executed, the function execution time to line 14 :
System.out.println(str);

change (String str) str will be recycled in immediately , new String ("true") has no references to it , gc thread " reference counting collector" will look for opportunities to recycle it , but do not some immediate recovery , because the main thread may be run.

can use the same idea to understand Boolean, Integer, Person, in the course of research -oriented , object-oriented at the same time , do not forget to go "for the Memory" , or even the same as @ zhao4zhong1 go "for the compilation " " for binary . "
This also explains a common interview questions "String, StringBuffer similarities and differences ", StringBuffer achieve similar ArrayList, is a scalable char [], during the expansion , when the implementation of the "Application for a new array , release the old array , " the two operations ; whereas String share in the static area of memory will not be released throughout the program , so frequently invoke the" + "operator , resulting in inefficiency .
------ Solution ---------------------------------------- ----
call the following assumptions :

Boolean value = new Boolean(false);
change(value);
public static void change(Boolean bValue) {
        bValue = new Boolean(true);
    }

call this method at the time , would you pass a parameter (value) pointing to a copy of a reference address is assigned to a method parameter (bValue), which is then passed when calling the method parameter (value) and the definition of when parameter method (bValue) point to the same object reference address, when executed bValue = new Boolean (true); when will the newly created object is new Boolean (true) address assigned bValue, this time bValue no longer pointing to the object value , while the method of execution is completed, the local variables bValue as methods, will be destroyed ( this change method is actually no sense ) . So the value is still pointing to new Boolean (false) the object, so print it out or false.
------ Solution ---------------------------------------- ----

has been used for so long that are passed by reference ah , say   
   public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}
  
  
The output should be:   
22   
33   
  
This work Why?   
  
   void set (int value) {
value = 4;
}
  
it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.   
in the end is how to define the circumstances under which the value passed by reference or pass it?   
  
 

landlord values ​​are passed in Java , but some are content value , some value is the address so you can consider your question:
p is stored in the address pointing to person , call the method test, a copy of the address , two addresses are pointing to person objects, test method changes the person object, then the address of the original point where the person p object has changed.

As for your question on the first floor , is the same reason :
bValue stored in the address points to a "false" the object, and then call the method change, copy and bValue the same address to the parameter bValue ( Note that both bValue not the same as a parameter an argument ) , then parameter points to a "true" object , but outside of the argument and no relationship .

These are the personal thoughts , in the end considering how the inventor of Java , authoritative book is how to write, I do not know , I just think it seems to be able to explain the results considering running

------ For reference only ---------------------------------- -----
Well , the results offer , but we first ponder it
false
false
3

------ For reference only ------- --------------------------------
parameters and arguments ah. Incoming method parameter , parameter change, but the same argument .
------ For reference only -------------------------------------- -
false
false
3

 public static void change(Integer i) {
        ++ i;
    }
     
    public static void change(Boolean b) {
        b= new Boolean(true);
    }
     
    public static void change(String s) {
        s= "true";
    }


landlord you look at my code in turn , with this effect is the same
------ For reference only ------------------- --------------------
you're going to find out , and pass the address of the value passed to understand this . You will understand why this is so
------ For reference only -------------------------------- -------

has been used for so long that are passed by reference ah , say
public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}


The output should be:
22
33

This work Why?

void set (int value) {
value = 4;
}

it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.
in the end is how to define the circumstances under which the value passed by reference or pass it?


------ For reference only ---------------------------------- -----
basic types int, double , etc. by value , reference types passed by reference
------ For reference only ----------------- ----------------------
false
false
3

basics, call by value , commonplace topic.
------ For reference only -------------------------------------- -

here is Integer and Boolean , ah, not int, bool, seeking to explain
------ For reference only ---- -----------------------------------
landlord Refbacks look to speak on the basis of video, episode are painted memory , understand the next memory is kind of how I hope to help you
------ For reference only -------------------- -------------------

seen four years ago , but now forgotten , then look no time
- ---- For reference only ---------------------------------------
< br /> vivid ah
------ For reference only ------------------------------- --------
weak weak to ask that this code can compile it? ? ? + + integer that can? ? Integer can be automatically converted to int? ? ?
------ For reference only -------------------------------------- -
java years are passed by value
------ For reference only ---------------------------- -----------

has been used for so long that are passed by reference ah , say   
   public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}
  
  
The output should be:   
22   
33   
  
This work Why?   
  
   void set (int value) {
value = 4;
}
  
it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.   
in the end is how to define the circumstances under which the value passed by reference or pass it?   
  
 

You can not pass this code is compiled . Under
correction , the output is 22 , 33 pairs of ah. p is passed by reference
------ For reference only ---------------------------------- -----

has been used for so long that are passed by reference ah , say     
     public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}
    
    
The output should be:     
22     
33     
    
This work Why?     
    
     void set (int value) {
value = 4;
}
    
it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.     
in the end is how to define the circumstances under which the value passed by reference or pass it?     
    
         
  
You can not pass this code is compiled .   Under
correction , the output is 22 , 33 pairs of ah. p is passed by reference  
Well, I temporarily hand fight. . There are all values ​​which say java pass
------ For reference only -------------------------- -------------

has been used for so long that are passed by reference ah , say     
     public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}
    
    
The output should be:     
22     
33     
    
This work Why?     
    
     void set (int value) {
value = 4;
}
    
it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.     
in the end is how to define the circumstances under which the value passed by reference or pass it?     
    
         
  
You can not pass this code is compiled .   Under
correction , the output is 22 , 33 pairs of ah. p is passed by reference  
I do not know the following example can illustrate the problem
package org.bruce.foundation.test;

/**
 * @author yang3wei
 *
 */
public class Person {

// Members
private String name;
private int age;
private int grade;

// Constructors
public Person() {

}
public Person(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}

@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("name = ").append(this.name);
sb.append(", age = ").append(this.age);
sb.append(", grade = ").append(this.grade);
return sb.toString();
}

// getters & setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}

package org.bruce.foundation.test;

public class TestAgain {

public static void changePerson(Person p) {
p = new Person("excellent", 18, 1);
}

public static void changeInteger(Integer i) {
i = 888;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person("normal", 25, 3);
System.out.println("原始:" + p);

changePerson(p);
System.out.println("加工后:" + p);

System.out.println("----------------- 分割线 ----------------");

Integer i = new Integer(250);
System.out.println("原始:" + i);

changeInteger(i);
System.out.println("加工后:" + p);
}

}

------ For reference only ----------------------------------- ----

has been used for so long that are passed by reference ah , say       
       public class Person {
public int i = 0;
public int j ; = 1;
}

void test (Person person) {
person.i = 22; < br /> person.j = 33;
}

public static void main (String args []) {
Person p = new Person ();
test (p);
System.out. println (pi);
System.out.println (pj);
}
      
      
The output should be:       
22       
33       
      
This work Why?       
      
       void set (int value) {
value = 4;
}
      
it certainly does not change the value of the argument , but the kind of object types for the above parameters are changed.       
in the end is how to define the circumstances under which the value passed by reference or pass it?       
      
               
    
You can not pass this code is compiled .     Under
correction , the output is 22 , 33 pairs of ah. p is passed by reference          
Well, I temporarily hand fight. . There is that there are all passed by value java  
second knock on the wrong code , corrections :
package org.bruce.foundation.test;

public class TestAgain {

public static void changePerson(Person p) {
p = new Person("excellent", 18, 1);
}

public static void changeInteger(Integer i) {
i = 888;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person("normal", 25, 3);
System.out.println("原始:" + p);

changePerson(p);
System.out.println("加工后:" + p);

System.out.println("----------------- 分割线 ----------------");

Integer i = new Integer(250);
System.out.println("原始:" + i);

changeInteger(i);
System.out.println("加工后:" + i);
}

}

output:
Original : name = normal, age = 25, grade = 3
processed : name = normal, age = 25, grade = 3
----------------- dividing line ---- ------------
original : after 250
processing : 250


------ For reference only ---------------------------------- -----
false
false
3


------ For reference only ---------------------------------- -----
plus one :

public static void change(StringBuilder str) {
    str.append("true");



------ For reference only ---------------------------------- -----

cause trouble ?
------ For reference only -------------------------------------- -

great God , please by disciples worship ! Although he did not understand how , but on a serious reply attitude enough admirable !
------ For reference only -------------------------------------- -

great God , please by disciples worship ! Although he did not understand how , but on a serious reply attitude enough admirable !  
wrong, it should be little brother !
------ For reference only -------------------------------------- -

cause trouble ?  
moderators certainly do not make trouble .
------ For reference only -------------------------------------- -
great God really do not deserve it , multi- point theory to understand it .
------ For reference only ---------------------------------------

gray often clear , reminds me of the situation Refbacks video memory schematic drawing of gratitude !
------ For reference only -------------------------------------- -

cause trouble ?          
moderators certainly do not make trouble .  
This added up the results of the implementation of the ah , huh
------ For reference only ------------------------ ---------------

basically clear , the parameter value will actually copy the value ( the value of heap objects new String ("false") address ) ,
Then, in changeStr () method , bValue after accepting the assignment , but also points to a new object (new String ("true")) address
so the value of the value has no impact
------ For reference only ---------------------- -----------------
upstairs good answer , I will add this:
landlord example , is indeed passed by reference , but the key question is not here, it took the String class

String s = new String("a");//创建s对象,并赋值为"a",但s的值就无法再改变了!!所以String类中没有类似setValue()的方法
s = "b";//执行这一句,实际上并没有改变s对象中的值,而是把"b"的引用赋值给了s,也就是说已经没有引用指向以前的"a"了

So the key issue is that the contents of String, Integer, Boolean , etc. These objects are read-only.
------ For reference only -------------------------------------- -

sharp !
------ For reference only -------------------------------------- -

here is Integer and Boolean , ah, not int, bool, seeking to explain  

disassembly package wrapper class concept .
------ For reference only -------------------------------------- -

cause trouble ?  
I mean , to distinguish between final and non- final classes , the distinction between String and StringBuffer (StringBuilder)

没有评论:

发表评论