Difference between int and int received by ParseInt in java
Asked Answered
O

9

5
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.

Orison answered 13/6, 2012 at 5:11 Comment(0)
U
11

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

Uphemia answered 13/6, 2012 at 5:12 Comment(0)
B
6

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

Bierce answered 13/6, 2012 at 5:13 Comment(6)
But why it is not appending on the first case ? If it is true it should be like 01 121Thundercloud
any primitives/wrappers before the appearance of first String are computed, Anything after that, is just appended together.Bierce
@AurA: because it basically translates to something equivalent to: " ".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
@Bierce thanks for editing the answer not it is quite clear. haylem thanks for explainationThundercloud
No.. it is not like 01 121... it is conceptually like (0+1) + " ".append(j).append(1)Bierce
Yes, I got it he meant that only.Thundercloud
D
3

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.

Dockhand answered 13/6, 2012 at 5:15 Comment(1)
Yeah +1, this indeed is the best answerToor
S
2
    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.

Supervision answered 13/6, 2012 at 5:12 Comment(0)
T
2

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));
Thacher answered 13/6, 2012 at 5:15 Comment(0)
R
1

+ operator is overloaded for addition and String concatenation what you're doing is String concatenation and not addition.. Use brackets for performing addition.

Revivalist answered 13/6, 2012 at 5:16 Comment(0)
B
1

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.

Badoglio answered 13/6, 2012 at 5:18 Comment(0)
Y
1

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.

Yardage answered 13/6, 2012 at 5:22 Comment(0)
W
0

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

Weighin answered 14/9, 2013 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.