Java - Operator

> Procedural Languages > Java

1 - Tables

Operators Precedence
postfix expr++ expr–
unary ++expr –expr +expr -expr ~ !
multiplicative * / %
additive + -
shift « » »>
relational < > ⇐ >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
conditional operator (ternary) ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

2 - Equality

2.1 - ==

The == operator only compares two references- i.e. whether the two object references point to the same object. The equals function actually compares the two values for equality:

2.2 - String

A string is by nature an array.

myString.equals("myWord");
Advertising

2.3 - null

a == b
a == null
a != b
a != null

2.4 - Collection

myCollection.size() != 0

3 - In

The Java language doesn't have a IN operator. You must implement it by using another logic.

3.1 - String

String[] arr = { "Nico", "Fred" };
Set<String> names = new HashSet<String>(Arrays.asList(arr));
 
System.out.println(names.contains("Nico")); // true
System.out.println(names.contains("Eric")); // false
String s = "Today is Monday";
boolean hasCheese = s.contains("Monday");

3.2 - Date

Set<Integer> weekendDay = new HashSet<>();
weekendDay.add(Calendar.SATURDAY);
weekendDay.add(Calendar.SUNDAY);
weekendDay.contains(calendar.get(Calendar.DAY_OF_WEEK));

4 - Documentation / Reference

Advertising
lang/java/operator.txt · Last modified: 2017/09/17 17:36 by gerardnico