System.out.println(info + ": " + ++x);
is this statement equivalent to
x++;
System.out.println(info + ": " + x);
and
System.out.println(info + ": " + x++);
is equivalent to
System.out.println(info + ": " + x);
x++;
As JVM can only process one statement at a time, does it divides these statements like this?
Yes and yes.
++x will be executed before the containing statement, ie the value of x will be incremented before it's used.
x++ will be executed after the containing statement, ie the value will be used and then the variable x incremented.
To be clear: in both cases the value of variable x will be changed.
Related
Java
int x = 5;
System.out.println(" x + 5 is " + x + 5);//correct
System.out.println("x += 5 is " + x += 5);// why wrong?
Even though, these 2 println is including calculation but why second println is error.Thanks
What you are doing causes an error because the + is seen as an operator to seperate parts of the string. Try placing that part between brackets like:
System.out.println("x += 5 is " + (x += 5));
This might fix it as you exclude the + from the string. Hope this helps you a bit, and that I am correct in my statement.
I was doing java assignment and I ran into this code.
int x=3, y=5;
System.out.println(x + y + " = " + y + x);
and the output is "8=53". Why does the first x+y gets evaluated and the last y and x expression gets printed? Left me wondering. Thanx in advance guys.
Remember that in Java, an operator (like +) can be overloaded. That means it will do different things, depending on its operands. For +, there are (at least) two choices: integer addition and string concatenation.
Which overload is chosen depends more so on the left-hand-side operand. Also, string concatenation with a non-string operand can cause automatic conversion to a string.
The whole thing will be evaluated left-to-right like this:
x + y + " = " + y + x
3 + 5 + " = " + 3 + 5 // 3+5 chooses the integer addition overload of +
8 + " = " + 3 + 5 // 8 + " = " chooses string concatenation
"8" + " = " + 3 + 5 // so 8 is converted to "8" first
"8 = " + 3 + 5 // and then concatenated with " = "
"8 = " + "3" + 5 // now 3 is converted to "3"
"8 = 3" + 5 // and concatenated with "8 ="
"8 = 3" + "5" // finally 5 is converted to "5"
"8 = 35" // and concatenated with the rest
FWIW, it's ambiguity like this that leads me to dislike implicit conversions1.
1 - In C#. I love it in Python :-)
output is "8=53". Why does the first x+y gets evaluated and the last
y and x expression gets printed?
Because first x + y are not appended with a string so integer calculation is done. Second ones are appended to a string because of " = " + and hence they are printed as individual values.
To prove this just do:
System.out.println("" + x + y + " = " + x + y);
and the output will be:
35 = 35
The first part is getting interpreted as integer addition, but in the second part (as you have introduced a string) it is getting interpreted and string concatenation.
Assuming you want 8 = 8
try
System.out.println(x + y + " = " + (y + x));
if you want 3 + 5 = 5 + 3
then I would use String.format as
in
System.out.println (String.format ("%d + %d = %d + %d", x, y, y, x));
The reason the output is "8 = 53" is because:
x + y + " = " + y + x
Is calculated from left to right so, if we break it down piece by piece:
x + y = 8
8 + " = " = "8 = "
"8 = " + y = "8 = 5"
"8 = 5" + x = "8 = 53"
That is how the compiler gets your answer :)
The first + is between two numbers so the result is eight. The 2nd have strings on either side of them so numbers get converted to strings and concatenated together. The plus operator binds tightest to the left, and gets evaluated left to right. If you wanted the last addition to be numerical then the expression should be in brackets ( ).
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
JLS 15.18.1 String Concatenation Operator +
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
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
I was solving some programming exercises in java. Everything was perfectly fine until my mind kind of froze a bit on the N Queens exercise.
Perhaps there is something wrong with the thing that is returned by s.get(x)? Since
s.showAll()
says that there is stuff in the stack, it is strange that
(s.get(x)==y)
is always false. You could try inserting:
for (int x=1; x<=boardSize; x++){
for (int y=1; y<=boardSize; y++){
System.out.println("(" + x + ", " + y + "): " + s.get(x) + " == " + y + " -> " + (s.get(x) == y));
}
}
at the end, to check whether the results of s.get(x) agrees with s.showAll().
The rest of the code here looks fine.
Hey,
I have the following two lines of code:
result[i] = temp[i] + temp[i + 1] + " " + temp[i + 2];
i += 2;
I am wondering if this line of code would do the same:
result[i] = temp[i] + temp[i++] + " " + temp[i++];
Can I be sure, that EVERY VM would process the line from the left to the right?
Thanks,
Tobi
From Java language specification:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.
It should be
result[i] = temp[i] + temp[++i] + " " + temp[++i];
if I am not wrong, so that the indexes are computed after each incrementation.
Apart from that it should work.
Let's just try actually quoting the source.
Operators on the same line have equal
precedence. When operators of equal
precedence appear in the same
expression, a rule must govern which
is evaluated first. All binary
operators except for the assignment
operators are evaluated from left to
right; assignment operators are
evaluated right to left.
It looks like someone found a link to the spec as well.
No it's not the same. When you put the ++ after i it implies that it is postfix, i.e. i will first be used and THEN incremented.
So:
result[i] = temp[i] + temp[i++] + " " + temp[i++];
would be the same as the below if i = 1:
result[1] = temp[1] + temp[1] + " " + temp[2];
and after this statement i would be sitting with value of 3.
For it to be the same as:
result[i] = temp[i] + temp[i + 1] + " " + temp[i + 2];
You should use the prefix increment operator, i.e:
result[i] = temp[i] + temp[++i] + " " + temp[++i];
i++ will output the value and increment
++i will increment the value and output.