Journey to Java: Episode 3 “Operators, Expressions, and Conditionals”

Learning Java so far has been an interesting process after learning other programming languages. I have studied the contents of episodes 1 and 2 for hours and only have down data types. I was impressed and shocked how many notes could be taken for every data type as opposed to learning Ruby and Javascript were pretty simple. Continuing my third week of this it is time to learn about Javas take on operators and related topics.

Operators

Operators are special symbols that perform specific actions on operands then return a result. Operands are any object being manipulated by the operator. Expressions are the entire operation together.

3 + 7;
// 3 and 7 are operands
// + is the operator
3 + 7 is the expression

List of Java Operators

+ Addition

- Subtraction

* Multiplication

/ Division Divides

% Modulus

++ Increment

-- Decrement

Conditional Logic

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

If-then looks at a condition to see if it is true.

boolean isOverTwentyOne = true;if (isOverTwentyOne == true){
System.out.println("over twenty one");
}
// checking to see if isOverTwentyOne is true then performing a task if it is.

&& and operator

The and operator can be used in a conditional to check multiple operands being true.

int high = 70;
int low = 20;
int testNum = 55;
if (testNum > low && testNum < high) {
System.out.println("test number is in the middle");
}

|| Or operator

The or operator is basically as it sounds and is the contrast to and operator. It only requires one operand to be true.

char a = "A";
char b = "B";
char newGrade = "A";
if (NewGrade == a || newGrade == b) {
System.out.println("this is a good grade");
}

Shorthand for == and !=

If you want to check the truthiness of something you can just say if followed by the variable name. The bang operator will be the opposite value you want.

boolean happy = true;if (happy){
System.out.println("you are happy");
{
if (!happy){
System.out.println("you are not happy");
}

Ternary Operator

Another way to check if then is to use the ternary. It checks the first condition to see its boolean value. If it is true the left operand happens and if it is false the right operand happens.

boolean isInvited = trueisInvited ? System.out.println("invited") : System.out.println("not invited");

Review Test

public class Practice {   public static void main(String[] args) {      //conditional test      double twenty = 20.00;      double eighty = 80.00;      double solution = (twenty + eighty) * 100.00 % 40.00;      boolean divisable = (solution == 0) ? true : false;
if (!divisable) { System.out.println("got some remainder"); } }}

Final Thoughts

I will probably have to revisit some of these topics in the future because some things have not been covered yet. I will make a follow up containing if then else statements and more complex expressions. Thankfully these topics are exactly the same as other languages I have learned. It is just going to take some repetition getting used to declaring variables with the data type it requires. I am happy to build on my foundational knowledge of programming while learning a new language that is so in demand. I am excited to start learning about java exclusive topics that are different than things I have learned in the past.