Difference between int and int received by ParseInt in java -


int = 0; int k = integer.parseint("12"); int j = k; system.out.println(i+1 + " " + j+1); 

strangely output received

1 121 

i can not figure out basic difference. please me.

use brackets follows

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

from docs

the + operator syntactically left-associative, no matter whether later determined type analysis represent string concatenation or addition. in cases care required desired result. example, expression:

a + b + c regarded meaning: (a + b) + c

extending scenario

i+1 + " " + j+1 

it becomes

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

since i int (i + 1) = 1 , simple addition

" " string hence ((i + 1) + " ") = 1 with space (string concatenation)

similarly when j , last 1 added, being added string hence string concatenation takes place, justifies output getting.

see


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -