int i = 0;
int k = Integer.parseInt("12");
int j = k;
System.out.println(i+1 + " " + j+1);
Strangely the output received is
1 121
I can not figure out this basic difference. Please help me.
int i = 0;
int k = Integer.parseInt("12");
int j = k;
System.out.println(i+1 + " " + j+1);
Strangely the output received is
1 121
I can not figure out this basic difference. Please help me.
Use brackets as follows
System.out.println((i+1) + " " + (j+1));
From the docs
The + operator is syntactically left-associative, no matter whether it is later determined by type analysis to represent string concatenation or addition. In some cases care is required to get the desired result. For example, the expression:
a + b + c is always regarded as meaning: (a + b) + c
Extending this to your scenario
i+1 + " " + j+1
it becomes
(((i + 1) + " ") + j)+1
Since i
is an int so (i + 1) = 1
, simple addition
" "
is a String
hence ((i + 1) + " ")
= 1
WITH SPACE (String concatenation)
Similarly when j
and last 1
is added, its being added to a String
hence String
concatenation takes place, which justifies the output that you are getting.
See
that is beacuse of " "
.
whenever a String comes, java doesnt do any calculations after that and just append it as string.
So in your case, i+1
is computed to 1
, but " " + j+1
has string in it. So, it just appended together to form 121
" ".concat(j).concat(1)
(or a similar version using StringBuilders, depending on your code and JVM version). It can't guess what you want, so it's pretty natural left to right interpretation. –
Arguello 01 121
... it is conceptually like (0+1) + " ".append(j).append(1)
–
Bierce The reason you see this behavior is that the sequence of +
operators is evaluated left-to-right. So it is evaluated as if parenthesized:
System.out.println((((i + 1) + " ") + j) + 1);
The first operator adds two int
values and produces an int
value. The next +
adds an int
to a String
and produces a String
. After that, everything is string concatenation. You can introduce your own parentheses to get the result you want.
int i = 0;
int k = Integer.parseInt("12");
int j = k;
System.out.println(i+1 + " " + (j+1));
basically when you put + " " + after this java just appends values as string.
and when you put (j+1) in brackets then its precedence gets higher and it is executes it first and perform sum operation.
When you use " "
The expression after that gets evaluated as string.
Using brackets (
and )
around an expression can solve the problem in hand.
System.out.println(i+1 + " " + (j+1));
+
operator is overloaded for addition and String concatenation what you're doing is String concatenation and not addition.. Use brackets for performing addition.
parseint will basically return int (Look at Java API), and there is only one int type in Java. in this example you used " ", where java will treat it as string. in any operation make sure you dont mix up strings with calculations. Always use parenthesis to separate String from calculations.
It happens because the + operator has left associativity and has an overloaded function with strings, so when you have this
int i = 0;
int k = Integer.parseInt("12");
int j = k;
i+1 + " " + j+1
it first sums
i + 1
which gives 1 then it sums 1 + " "
, which uses the overloading function of it to concatenate 1 and " " so it gives a string with the value of "1 ". After that it sums "1 " + j
and since one of the operands is a string, it does the same behavior and so on.
Interger.parseInt (String str) is a wrapper class method which is used to convert String obj type to primitive data type (int). this are generally used in collection frame work for converting primitive data type to object and vice versa
© 2022 - 2024 — McMap. All rights reserved.