This question already has answers here:
Why do we usually use || over |? What is the difference?
(28 answers)
Closed 5 years ago.
A book I'm working from states :
The short-circuit operators are nearly identical to the logical operators,
& and |, respectively, except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression
To test this out I tried
int y = 1;
boolean x = false | (y < 4);
System.out.print(x); //true
Which printed out true as expected.
however
int y = 1;
boolean x = false || (y < 4);
System.out.print(x); //true
also prints out true which seems to contradict the book "the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression".
I was assuming x would take the value of false as the final result can be determined from the left hand side alone
I was assuming x would take the value of false as the final result can be determined from the left hand side alone
No, the final result cannot be determined by the left hand side alone - since the left operand is false, the right operand must be evaluated too (since false OR something == something).
On the other hand, if the left operand was true, the result of the OR operator would be true, and the right operand could be ignored.
You should understand that short-circuit operators should give the same result as their non-short circuited counterparts (except for cases where evaluating the right operand may throw an exception and the short-circuited operators can determine the result by the left operand alone).
There would be no scenarios in which the short circuited operator returns false and the corresponding non-short circuited operator returns true or vice versa.
Related
class Output
{
public static void main (String[] args)
{
int a = 5;
a += 5+ (++a) + (a++);
System.out.print(a);
}
}
Evaluation:
a += 5 + (++a) + (a++)
=> a+= 5 + 6 + (a++) [++a :increment value of a and then use it. So:increment a = 6, then use a=6]
=> a+= 5 + 6 + 6 [a++ :use and then increment value of a. So: use a=6, then increment a=7]
=> a+= 11 + 6
=> a+= 17
=> a = a+17
=> a = 7 + 17
=> a = 24
The += operator, like all compound operators, evaluates the variable on the left first, before evaluating all other operands, and before any operations are actually performed. This is specified by the JLS, Section 15.26.2:
If the left-hand operand expression is not an array access expression, then:
First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.
(bold emphasis mine)
That means that a is evaluated (and saved) to 5 on the left side before the a++ and ++a are evaluated on the right side. The right side does evaluate to 17, but because the left side is still 5, the sum is 22, not 24.
The expression
a += 5 + (++a) + (a++);
is equivalent to
a = a + 5 + (++a) + (a++);
Meaning that the new assigned value of a is the sum of the operands from right to left (because of left-to-right associativity) evaluated in the following way:
take the variable a: evaluated as 5
add the constant 5
increment the variable a and then evaluate the result: 6
evaluate the variable a and then increment it. The value used in the sum will be the evaluated value: 6
The sum is 22.
This question already has answers here:
How can a Java variable be different from itself?
(11 answers)
Closed 5 years ago.
Recently I had an interview with a Software company where the following question was asked in the technical aptitude round:
Declare i in such a way that the condition is always true :
while(i != i) {
}
Is it technically possible in java to assign something of this sort??
NaN is not equal to itself, so
double i = Double.NaN;
But I don't think this is a good interview question.
Quote from the Java Language Specification:
NaN is unordered, so:
The numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1).
The equality operator == returns false if either operand is NaN. In particular, (x<y) == !(x>=y) will be false if x or
y is NaN.
The inequality operator != returns true if either operand is NaN (§15.21.1). In particular, x!=x is true if and only if x
is NaN.
class Tester {
public static void main(String[] arg) {
byte b=10;
b +=(b<127)?b>-128? b+=10 :0 :5;
System.out.println(b);
}
}
i know that the conditions are evaluated true and took the control to b+=10
so now logically b+=b+=10; is adding the value of b and 10 which evaluates 20, assigning to b. Now i'm unable to evaluate it further.
JLS 15.7.1. Evaluate Left-Hand Operand First has a similar example :
If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.
Example 15.7.1-2. Implicit Left-Hand Operand In Operator Of Compound Assigment
In the following program, the two assignment statements both fetch and remember the value of the left-hand operand, which is 9, before the right-hand operand of the addition operator is evaluated, at which point the variable is set to 3.
class Test2 {
public static void main(String[] args) {
int a = 9;
a += (a = 3); // first example
System.out.println(a);
int b = 9;
b = b + (b = 3); // second example
System.out.println(b);
}
}
This program produces the output:
12
12
Therefore, in your case, the original value of b (10) is remembered and added to the result of the ternary conditional operator (which is 20, since the value of b+=10 is the result of that expression), giving you 30.
First (b<127) is evaluated which is true so it moves to part b>-128? b+=10
In this b>-128 is evaluated which is also true so it moves to b+=10 which makes b=20 and adds to the left side of expression in which value of 10 is stored in b.So b+=(b=20) makes b=30
I have a java class as follows:
class A{
public static void main(String[] args){
int a=10;
a*=a++ +a;
System.out.println(a);
}
}
Output:210
In my opinion the output should be 231 calculated as follows:
a*=10+11;
a*=21;
a=a*21;
a=11*21;
a= 231;
Can anyone please explain me where am I wrong and why?
In any statement of the type:
x *= y;
The initial value of the LHS is evaluated before the RHS. So your statement:
a *= a++ + a;
Is equivalent to:
a = a * (a++ + a);
Which sets a to the value 10 * (10 + 11) => 210.
If you're particularly interested in the formal specification related to this point you can find it here which contains the rule "If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation."
Consider 15.7.1. Evaluate Left-Hand Operand section of java specs where it says - First, the left-hand operand is evaluated to produce a variable then the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator
In your case it is a = 10 * ((11)+10) = 201
This question already has answers here:
What is the purpose of Java's unary plus operator?
(7 answers)
Closed 8 years ago.
I ran across this while reviewing a colleague's code. She'd left it in by accident (it used to be a String concatenation), and I assumed that it wouldn't compile. Turns out I was wrong, so I tried seeing what that operator did:
public static void main(String[] args) {
int i = -1;
System.out.println(String.format("%s", +i));
System.out.println(String.format("%s", +i));
}
As far as I can tell, it does nothing, but I'm curious if there is a reason it's allowed to compile. Is there some hidden functionality to this operator? It's similar to ++i, but you'd think the compiler would barf on +i.
That is the plus unary operator +. It basicaly it does numeric promotion, so "if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int".
Another unary operator is the increment operator ++, which increments a value by 1. The increment operator can be applied before (prefix operator) or after (postfix operator) the operand. The difference is that the prefix operator (++i) evaluates to the incremented value, whereas the postfix operator (i++) evaluates to the original value.
int i = -1;
System.out.println(+i); // prints -1
System.out.println(i++); // prints -1, then i is incremented to 0
System.out.println(++i); // i is incremented to 1, prints 1