Conditional Expression Examples

Contact Us or call 1-877-932-8228
Conditional Expression Examples

Conditional Expression Examples

The following are conditional expression examples.

  • a = 3; b = 4;
  • a == b will evaluate to false
  • a != b will evaluate to true
  • a > b will evaluate to false
  • a < b will evaluate to true
  • a >= b will evaluate to false
  • a <= b will evaluate to true
String s = "Hello";
String r = "Hel";
String t = r + "lo";

s == t will evaluate to false

s != t will evaluate to true (they are not the same object - they are two different objects that have the same contents)

s.equals(t) will evaluate to true

String s = "Hello";
String t = s;

s == t will evaluate to true

s != t will evaluate to false (they are the same object)

s.equals(t) will evaluate to true

Note: Java will intern a literal String that is used more than once in your code; that is, it will use the same location for all occurrences of that String

String s = "Hello";
String t = "Hello";

s == t will evaluate to true, because the String object storing "Hello" is stored only once, and both s and t reference that location

s != t will evaluate to false (they are the same object)

s.equals(t) will evaluate to true

Next