TRENDING NEWS

POPULAR NEWS

How Will The Expression Be Evaluated In Java

In java What is the result of the following expression? 25 / 4 + 4 * 10 % 3?

25/4+4*10%3
6+4*1
6+1
7

Does Java allow strings and integers to be concatenated?

Two people have suggested that it's something to do with what println does or expects but this is wrong."foo"+10 is an expression in Java whether it is being passed to println or another method or used in an if() statement etc. It is evaluated and the result of evaluation, whatever it may be, is passed to println or anything else.Expressions are evaluated left to right and have a type which is determined at compile-time.The Java rules for expression evaluation say that if one operand of the + operator is a String and the other isn't, then the one that isn't is converted to a String, then they're concatenated left to right. So what happens is the integer literal is converted to an Integer, by doing new Int(10); then this is converted to a String using Integer.toString(); then "foo" and "10" are concatenated. The result of String concatenation is a String and so the compile-time type of this expression is String. At runtime, a String object will be passed to println().That's how it works. (In principle; in reality this example would get optimized.) For the full gory technical details about how expressions are evaluated in Java see Oracle's documentation: Chapter 15. Expressions and Chapter 5. Conversions and Promotions

Please help me with these Java related questions?

true
-1.9999
B
A
C
True
A
don't know
B
False, not sure
A
A
dunno
True i think
B

Basic java boolean question?

line 4 should be fine, because all this garbage:

x + y < z || 4 * y + z > x && x > y;

should evaluate to true or false.

this is really a question about operator precedence and not so much about booleans.

http://download.oracle.com/javase/tutori...

the first prints b = true, the second prints b = false.

the reason is that this part of the first one 'x + y < z' returns true and the rest is not evaluated at all due to the logical or operator.

boolean b = x + y < z || <- this expression returns true here and the rest is ignored
4 * y + z > x && x > y;

but here,

b = (x + y < z || 4 * y + z > x) && x > y;

the parenthesis forces the second half of the expression to be evaluated, it's basically saying

b = (true) && x > y;

The logical and operator means both sides have to be true to evaluate to true, so both sides are evaluated in this case. x is not greater than y, so this returns false.

TRENDING NEWS