Description
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!
Explanation & Answer
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.
Review
Review
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Similar Content
Related Tags
Tess of the DUrbervilles
by Thomas Hardy
The Tipping Point
by Malcolm Gladwell
Sharp Objects
by Gillian Flynn
Cat on a Hot Tin Roof
by Tennessee Williams
Silas Marner
by George Eliot
Little Women
by Louisa May Alcott
Into Thin Air
by Jon Krakauer
Beowulf
by Anonymous Anglo-Saxon poet
Pride and Prejudice
by Jane Austen