Basic Java knowledge (Program result)

Source: Internet
Author: User
Tags gety
try {System.out.println("hello");System.exit(0);} finally {System.out.println("good bye");}/*hello*/
try {int r = 1 / i;} catch (Exception e) {e.printStackTrace();return true;} finally {return false;}/*false*/

 

1. Import java. AWT. Point;
Class assign
{
Public static void main (string ARGs [])
{
Int A = 1;
Int B = 2;
Point X = new point (0, 0 );
Point y = new point (1, 1); // 1
System. Out. println ("A is" + );
System. Out. println ("B is" + B );
System. Out. println ("X is" + x );
System. Out. println ("y is" + y );
System. Out. println ("Discovery assignment and" +
"Setlocation ...");
A = B;
A ++;
X = y; // 2
X. setlocation (5, 5); // 3

System. Out. println ("A is" + );
System. Out. println ("B is" + B );
System. Out. println ("X is" + x );
System. Out. println ("Y is" + y );
}
}
This code generates the following output results?
A is 1
B is 2
X is Java. AWT. Point [x = 0, y = 0]
Y is Java. AET. Point [x = 1, y = 1]
Ming assignment and setlocation...
A is 3
B is 2
X is Java. AWT. Point [x = 5, y = 5]
Y is Java. AET. Point [x = 5, y = 5]
URL: http://zhidao.baidu.com/question/106550817.html

Resolution:

When the basic data type is changed, the original data is not changed. If the object type is changed, the data is changed.

For, because a and B are basic data types, when B is assigned to a, the value of B is only given to A, and the value of a remains unchanged, when A or B changes, the values of A or B remain unchanged.

Because X and Y are object types, when y is assigned to X, (X and Y are only references to the corresponding object ), so we only change X to the reference of the object referred to by Y. At this time, both X and Y are references of object A. Therefore, when X or Y is changed, X and Y will change, they are the same.

Class main {public static void main (string ARGs []) {int A = 1; int B = 2; string stra = "stra"; string strb = "strb "; a aa = new A (); a AAA = new A (); AA. A = 11; AAA. A = 111; system. out. println ("A =" + a); system. out. println ("B =" + B); system. out. println ("AAA =" + AA. a); system. out. println ("AAAA =" + AAA. a); system. out. println ("stra =" + stra); system. out. println ("strb =" + strb); A = B; A ++; AAA = AA; AA. A ++; stra = strb; stra = "buhao"; system. out. println ("A =" + a); system. out. println ("B =" + B); system. out. println ("AAA =" + AA. a); system. out. println ("AAAA =" + AAA. a); system. out. println ("stra =" + stra); system. out. println ("strb =" + strb) ;}} Class A {int A;}/* The result is: A = 1B = 2AAA = 11 AAAA = 111 stra = strastrb = strba = 3B = 2AAA = 12 AAAA = 12 stra = buhaostrb = strb */

2. Pass the native data type and reference type parameters in Java:

Class Point {private int X; private int y; Public point (int x, int y) {This. X = x; this. y = y;} public int getx () {return X;} public void setx (int x) {This. X = x;} public int Gety () {return y;} public void sety (INT y) {This. y = y ;}} public class main {public void changeint (int A) {A = 3;} public void changepoint (point) {point. setx (5); point. sety (6);} public void changestring (string Str) {STR = "ABC"; system. out. println (STR);} public static void main (string [] ARGs) {int A = 1; // Statement (1) Main Pt = new main (); // Statement (2) pt. changeint (a); // Statement (3) system. out. println (a); // Statement (4) 1 point = new point (1, 2); // Statement (5) pt. changepoint (point); // Statement (6) system. out. println (point. getx (); // Statement (7) 5system. out. println (point. gety (); // Statement (8) 6 string STR = "XYZ"; // Statement (9) pt. changestring (STR); // statement (10) abcsystem. out. println (STR); // Statement (11) XYZ }}/ * running result: 156abcxyz */

Question 1: After Statement (4) is executed, what is the printed result? Why?
1. For the native data type, it only transmits the value of A in the main method. After the value is passed, there is no relationship between the two,

Question 2: After the statement (7) (8) is executed, what is the printed result? Why?
5 6. It refers to the reference, that is, the memory address of the object.
Question 3: What is the output after the statement (11) is executed? Why?
XYZ
URL: http://leeldy.blog.163.com/blog/static/1398530620091279551463/
Summary: in Java, the parameter passing of methods, whether native data type or reference type, always uses pass by value. For the native data type, the passed value is the value it is assigned to. For example, if int A = 3, the value 3 is passed to the method. For the reference type, the reference itself is an address and an int type memory address value. Therefore, it passes the value to the method to pass the value.

 

Related Article

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.