Java

User Generated

WY1234

Programming

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!

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

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.


Anonymous
Really useful study material!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags