java basic details

java foundation have a lot of details, it is not clear if the underlying how it works, many of us thought would be consistent since led us to make wrong judgments, did not talk much, take a subversion Three Views of it.

1. Guess what they are outputs?

System.out.println (. 1 + 2 + "A");
System.out.println ( "A" +. 1 + 2);
a first output line 3a, the second output line a12

Because when the execution is left to right, it will encounter strong string to a string.

Therefore, the first row is performed: String str = String.valueOf (1 + 2); String sb = new StringBuffer (str) .append ( "a") toString ();.

The second line is executed: String sb = new StringBuffer ( "a") append ( "1") append ( "2") toString ();...

2. That if the above subject to change about it?

System.out.println (. 1 + 2 + 'A');
System.out.println ( 'A' +. 1 + 2);
a first output line 100, second output line 100 or

We know, from low to high data type is char / short / byte <int <long <float <double, while the upward transition type char were converted to ASCII code, 'a' is the ASCII code 97, so 1+ 2 + 97 output 100

3. The following statement is there a problem?

B1 =. 1 byte;
byte B2 = 2;
byte B3 = B1 + B2;
third row fail to compile because of byte / short / char numerical computation will be cast to type int, the final result is an int .

Therefore, the correct wording of byte b3 = (byte) (b1 + b2)

4. What do you think the result will be output?

Integer i=128;
Integer j=128;
System.out.println(i==j);
false!  why?

Integer caching mechanisms exist, the object will automatically integer -128 to 127 in cache Cache array, once Integer.valueOf (i) method, and -128 <= I call <= 127, will automatically come up to the array, otherwise it will create a new Integer object

5. they are equal it?

A = String "A";
String c = A;
System.out.println (A == c);
 ! To true they are in constant pool references pointing to the same address

That this happen?

String a="a";
String b="b";
String c="a"+"b";
System.out.println(a+b==c);
System.out.println("ab"==c);
false  true  why?

First String type is immutable, then the implementation of the fourth line is as follows:

a + b:.. new StringBuffer ( "a") append ( "b") toString (); i.e. a heap object is newly created, both of the same object is no longer

But the compiler to do some optimization, at compile time, String c = "a" + "b" is the result of compiling String c = "ab".

6. They can exchange it?

public class Test{
public static void main(String[] args){
int a=1;
int b=2;
change(a,b);
System.out.println(a+" "+b);
}

private static void change(int a, int b) {
int c=a;
a=b;
b=c;
}
}
输出结果为1 2

java parameter value passed passed passed by reference and divided according to the parameter type.

The basic data types and their packaging is passed by value, so the change () method, a corresponds to a local variable, change () completes destruction i.e., the original variable a does not have any influence.

public class Test{
public static void main(String[] args){
AB ab=new AB(1, 2);
change(ab);
System.out.println(ab.a+" "+ab.b);
}

private static void change(AB ab) {
int c=ab.a;
ab.a=ab.b;
ab.b=c;
}

static class AB{
private int a;
private int b;
public AB(int a,int b) {
this.a=a;
this.b=b;
}
}
}
上述代码输出: 2 1

The code reference object is passed, it is passed by reference. I.e., the main function of ab and change () method ab share the same address, so the exchange of data parameters that affect the main function of the ab change () in the.

7. Guess what the results of their output, respectively?

the Test class {public
public static void main (String [] args) {
String = A ". 1";
String B = "2";
Change (A, B);
System.out.println (A + "" + B);
}

static void Change Private (a String, String B) {
a = + ". 1";
a = B;
}
}
output: 1 2 because the object is of type String, value for parameter passing is passed

public class Test{
public static void main(String[] args){
StringBuffer a=new StringBuffer("1");
StringBuffer b=new StringBuffer("2");
change(a,b);
System.out.println(a+" "+b);
}

private static void change(StringBuffer a,StringBuffer b) {
a.append(1);
}
}
上述代码输出结果:11 2

The answer is nothing wrong results, the key is if we change about it?

static void Change Private (the StringBuffer A, the StringBuffer B) {
a.append (. 1);
A = B;
}
The output is: 112

Here on the very issue, and obviously a = b, or why output 112

The reason is: When passing references, the original object reference will not change.

We then modify it:

static void Change Private (the StringBuffer A, the StringBuffer B) {
A = the StringBuffer new new ( ". 11");
}
finally print the result is: 12

So that we can sum up: the fact, the only parameter passing java value passed! However facilitate understanding, we pass into and passed by reference value depending on the parameter type, wherein the basic data types and immutable (String class and packaging) is the value taken by transmission, other references passed by reference is taken to the class , passed by reference can modify the contents of the original object's methods, but always a reference to the original object does not change!
----------------
Disclaimer: This article is CSDN bloggers' growing er "in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/qq_37820491/article/details/101049471

Guess you like

Origin www.cnblogs.com/gepanjiang/p/11572405.html