Java basic knowledge trap (2)
This article is published on my blog. The last time I talked about some string-related knowledge, it was quite basic. This time I also talked about object address issues, such as passing parameters. First read the following code: public void changeInt (int a) {a = 3;} int a = 1; changeInt (a); System. out. println (a); I believe this beginner knows the result and the value is passed. Let's take a look at the process: public void changestr (String str) {str = "www. luoliang. me ";} String s =" luoliang. me "; changestr (s); System. out. println (s); I believe many people know the result, however, when asked about the specific process, some people do not know what to do, including the one or two simple words that I did not talk about at all about the string pool and other ideas. This describes the memory conversion of strings. First, we know that a function changestr with the String parameter is declared, and then this "luoliang. me "(Judgment), after the creation is complete, the changestr method is called to put the s address in the string pool" luoliang. me "Address to the str variable, this function is changed to point the str variable to another" www. luoliang. of course, this process needs to determine whether the string pool exists. After the modification, the address of s is printed as the address of "luoliang. me. In this process, all the strings have not changed, because they are constants and only change the address pointed to by the variable! Now I want to understand the following code: copy the code public class Test {public static void changePoint (Point point) {point. x = 3; point. y = 4;} public static void main (String [] args) {Point point = new Point (1, 2); System. out. println ("x =" + point. x + "; y =" + point. y); changePoint (point); System. out. println ("x =" + point. x + "; y =" + point. y) ;}} class Point {int x; int y; public Point (int x, int y) {this. x = x; this. y = y ;}}