Java

Programming
Tutor: None Selected Time limit: 3 Days

I know the following Java relational expressions are valid. Can you explain to me why they are valid?

  • 5 <= 4+2

  • "Hello" != "World"

  • a < a - 0 (where a is an initialized non-local variable of integer type)

Thanks!

Nov 30th, -0001
1. 5 <= 4+ 2 : In Java, the arithmetic operators are evaluated first before the relational operators. So, this expression is just equivalent to 5 <= (4+2) which is 5 <= 6 . In words, you are testing if 5 is smaller than or equal to 6. Of course, 5 is smaller than 6 so this expression is true.

2. In this two strings are compared. As the two strings are not same and the expression itself says "hello" not equal to "world", thus this expression is also evaluated to be true.

3. In this same logic as the first one is applied, you evaluate a < a -0 so this is same as saying a < (a - 0) which is same as a < a. But, we all know that a is equal to a, hence this expression is evaluated to be false.
Jun 25th, 2013

So the last (a < a -0) one is invalid, right?

Because a < a is false.


Jun 25th, 2013

Invalid means that the code is not as per the syntax of language. The syntax of this expression is totally correct but the expression itself evaluates to be false. The result of this expression is always false. 

Something is invalid and something evaluates to be false. These are two different things.

Jun 25th, 2013

I should say at the very beginning. Are all 3 relational expressions are valid or invalid in Java. Sorry for my mistake.

Jun 25th, 2013

Then the first 2 is valid and the last one is an invalid expression then.

Jun 25th, 2013

Yes.

Jun 25th, 2013

Are you studying on the go? Check out our FREE app and post questions on the fly!
Download on the
App Store
...
Nov 30th, -0001
...
Nov 30th, -0001
Dec 3rd, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer