Java Basics 1.03

Operators

  • follows C standards

 

Types

  • Assignment
  • Equality or relational
  • Mathematical
  • Conditiona
  • Ternary (short-hand conditional)

 

Assignment and Math Operators

=, +, -, *, /, %

 

Incrementing

intValue = 10;

intValue ++; // 11

intValue --; // 9

intValue += 5; // 15

intValue -= 5; // 5

intValue *= 5; // 25

intValue /= 5; // 2

 

PostFix vs Prefix Incrementing

int intValue = 10;

 

PostFix

Do operator, then evaluate

System.out.println(intValue ++)

output = 10

new value = 11

 

Prefix (a.k.a.) Unerary

Evaluate, then do operator

System.out.println(+ intValue);

output = 11

new value = 11

 

Comparing Values

 

>, <, >=, <=, instanceof (Class Membership)

 

String s = “Hello”;

if (s instanceof java.lang.String) {

     System.out.println(“s is a String”);

}

 

 

Comparing Strings

String s1 = “Hello”;

String s2 = “Hello”;

if (s1 == s2) {

     System.out.println(“They Match!);

} else {

     System.out.println(“No Match!);

}

> No Match!

 

if (s1.equals(s2)) {

     System.out.println(“They Match!);

} else {

     System.out.println(“No Match!);

}

>They Match!

 

Equality Operators

==                          Equality

!=                          IInequality

if(!this)     Reversing logic: booleans only

 

Conditional Operators

&&          AND

||          OR