Java_ Exercise Solutions

Source: Internet
Author: User

The Java problem encountered, recorded. Here are some questions and better answers.

1.

public class example{
String Str=new string ("good");
Char[]ch={' A ', ' B ', ' C '};
public static void Main (String args[]) {
Example ex=new Example ();
Ex.change (ex.str,ex.ch);
System.out.print (ex.str+ "and");
System.out.print (ex.ch);}
public void Change (String Str,char ch[]) {
str= "Test OK";
Ch[0]= ' G ';}
}

The output is: good and GBC
The answers are as follows:
 

public class example{//Let me analyze the memory allocation situation!
String str= new String ("good"); String str, the str reference is placed in the stack space, the value is null, when the new String is generated in the heap space a good, and str points to it!
Char[]ch={' A ', ' B ', ' C '}; CHAR[]CH, the CH reference is placed in the stack space, an array is generated in the heap space a,b,c and ch points to them
public static void Main (String args[]) {
Example ex=new Example (); Example EX, put the ex reference in the stack space, when new Example,
An object is generated in the heap space and ex points to it without any attribute values because you do not write the constructor
Ex.change (ex.str,ex.ch); Call method, see method Analysis!
System.out.print (ex.str+ "and");
System.out.print (ex.ch);
}
public void Change (String Str,char ch[]) {
Maybe here you get the misunderstanding that STR here is different from the outside STR,
This means that String str ' new ' is a str in the stack space, but there is already a str, which is the one above
The two of them are not the same, this str is a local variable, depends on the method, as long as the method ends, it automatically dies, and the outside that is still there,
So here it is, send the outside Str in, assign the value to this str, at this time two are "good"//and Char [] analysis is different,
Please remember that CH [0],ch[1], ch[2] ... There are heap spaces, which are their address spaces,
For variables of this type of reference (for example, arrays, objects), they actually put the incoming value directly at the reference address.
That is, ch[0] is always only one, and points to the heap space, the parameters are passed in the heap space.
str= "Test OK";
Then, when you do this, the STR value changes, that is, test OK, and remember that there are two str in the stack space at this point.
The outside is still good, but inside the change into test OK, OK, continue ...

Ch[0]= ' G ';

According to the above analysis, it is clear that ch[0] is always one, and the storage and modification are directly in the heap space, so here ch[0] becomes ' g '

}
Well, this method finally runs to the end, the point is, here who dies, that's the one that relies on method existence local variable str
So now there is only one STR, and its value is good, when the output of the console to call STR,//You say that will also call the dead Str, obviously called the existence of STR is good

}

In fact, if you want to see clearly, you can do this, the method inside the STR, to THIS.STR.
You will be able to differentiate: they are really two str, and the use of this (the class) is called the member variable!
Not depend on the method, is the life is hard, cannot die!
Do you understand this analysis?

Java_ Exercise Solutions

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.