Difference between int and int received by ParseInt in java

int i = 0;
int k = Integer.parseInt("12");
int j = k;
System.out.println(i+1 + " " + j+1);

It is strange that the result obtained

1 121

I cannot understand this basic difference. Please help me.

+5
source share
9 answers

Use parentheses as follows

System.out.println((i+1) + " " + (j+1));

From docs

The + operator is syntactically left-associative, regardless of whether it is later determined by type analysis to represent string concatenation or append. In some cases, care is required to obtain the desired result. For example, the expression:

a + b + c is always considered to mean: (a + b) + c

Extension of this scenario

i+1 + " " + j+1

he becomes

(((i + 1) + " ") + j)+1

Since it iis int so (i + 1) = 1, a simple addition

" " String, ((i + 1) + " ")= 1 ( )

, j 1, String String , , .

+11

beacuse " ".

, String, java .

, i+1 1, " " + j+1 . , , 121

+6

, , , + . :

System.out.println((((i + 1) + " ") + j) + 1);

int int. + int String String. . , .

+3
    int i = 0;
    int k = Integer.parseInt("12");
    int j = k;
    System.out.println(i+1 + " " + (j+1));

, + "" + java .

(j + 1) , , .

+2

" " .

( ) .

System.out.println(i+1 + " " + (j+1));
+2

+ , - , , - , . .

+1

parseint int (Look at Java API), Java int. "", java . , . , .

+1

, + , ,

int i = 0; int k = Integer.parseInt("12"); int j = k; i+1 + " " + j+1

i + 1, 1, 1 + " ", 1 ", " 1". "1 " + j, , ..

+1

Interger.parseInt(String str) - -, String obj (int).

0

All Articles