if Statements:
-
Can determine whether a piece of code runs based off a specific condition
-
If the condition is true, run the code inside the if statement
-
Skips code if the condition is false
-
-
Example format:
if (booleanExpression) {
//Braces not needed for only one statement
statement;
statement2;
}
Relational and Equality Operators
-
Relational Operators are the same as in math
-
Equality Operators:
-
equals is ’==’
-
Not equals is ‘!=‘
-
Comparing Non-numeric Data:
-
Can use the unicode value of a char to compare them
- Use the operators above
Comparing Strings:
-
To determine if two Strings references point to same memory location:
- Use same operators as char and numbers
-
To determine if two Strings have the same value:
- use the .equals() method
String a = "5";
String b = "5";
boolean x = b.equals(a); //true
String Interning:
-
When a String is created, it is placed inside of the String constant pool
- A special area in the heap
-
If a literal is found that has the same value as the stored String, it reuses it
- Doesn’t create a new String object
The compareTo Method:
-
Relational operators can’t be used on Strings
- Ex. ’>’
-
To compare Strings, the .compareTo() method must be used
-
Converts each string into its unicode values and compares them
-
Returns an integer:
-
if zero is returned: Strings are equal
-
if negative number is returned: x < y
-
if positive number is returned: x > y
-
-
int bigger = x.compareTo(y);
The if-else Statement:
if (booleanExpression){
statement;
}
else if {
statement2;
}
else {
statement3;
}
-
If the if statement isn’t run, the else statement will
-
Multi-way branching:
- use else-if as another if statement
Logical Operators:
-
The three logical operators:
- and (&&), binary, both conditions must be true
- or (||), binary, one condition must be true
- not (!), unary (Applied to one operator), reverses true/false
-
Precedence:
-
Relational operators have higher precedence than logical operators
-
Not (!) takes precedence over AND and OR
-
AND takes precedence over OR
-
Parentheses take precedence over logical operators
-
Short-circuit Evaluation:
-
Improve Java performace
- Specifically when right side is more complex than left side of an equation
-
When using the ’&&’ operator, if one operand is false than the entire expression is false
-
This is used to make Java evaluation faster
- Java will skip the rest of the equation if one side is false
-
-
Can skip errors unintentionally if the error is on the right side of an expression
Ternary Conditional Operator:
-
Short-hand if statement
-
Example:
if (rainInput.startsWith("y")) {
raining = true;
}
else {
raining = false;
}
//Ternary:
raining = rainInput.startsWith("y") ? true : false;
The switch Statement:
- Use switch statement to replace else if
- Also Multi-way branching
switch (expression) {
case value1:
statement(s)
break;
case value2:
statement(s)
break;
default:
statement(s)
}
-
Break is used to ensure that we don’t leak to the other cases
-
Branches:
-
case:
- A possible literal value result for the expression
- A set of statements to execute if that value matches the result.
-
default:
- else replacement
-
-
Downsides:
- Less safe then if-else
- Each case branch can only contain a single value to match the specified expression