Java programming for beginners - Lesson 2 - Operators
Hello everyone, welcome to the third lesson in my Java programming for beginners tutorials series. The second lesson(lesson - 1) - Variables can be found at - https://steemit.com/programming/@robertlyon/java-programming-for-beginners-lesson-1-variables
In this lesson, I am going to be focusing on operators and conditional statements. We are going to look at mathematical operators first. Then we will take a look at relational and logical operators which will set us up for covering If statements in the next lesson.
Operators
So operators are ways of manipulating our data, you will be familiar with basic math operators such as + and - which I will cover in this section. These operators allow us to perform actions on our variable and create our logic. In this section, I will cover all the basic operators that you need for working with Java.
Unary Operators
Below is a list of the unary operators.
Prefix | Postfix |
---|---|
++num | num++ |
--num | num-- |
In this table num represents a variable that will hold a value.
Unary operators are operators that are performed on just 1 operand. If we consider the statement 1 + 2. This statement has 2 operands the number 1 on the left-hand side and the number 2 on the right-hand side. In contrast, unary operators only have the one operand an example of this in Java is.
postfix
In this program i++ can be thought of as i = i + 1;
int i = 1;
System.out.println(i++);
The output of this program will be 1 as can be seen in the image below.
note the output of 1.
Now you may be thinking if i = 1 + 1. Then why is the output not 2. this is because i++ is a postfix operator, that means the ++ part is evaluated last. So after the value of i is printed to the screen. The variable i is incremented by 1. See the code and image below.
int i = 1;
System.out.println(i++);
System.out.println(i);
As can be seen in the output above, the second println statement produced the value 2. This shows that the variable i was indeed incremented in the line above.
prefix
Now if we wanted the first println statement to print ou the value 2 we could have used the prefix operator++i. This operator increments the variable by 1 before it gets printed out. See below for an example of this.
int i = 1;
System.out.println(++i);
As can be seen above the output for this program is indeed 2.
The use of the unary operators will become more clear when we cover loops.
Arithmetic
Arithmetic operators are operators that allow us to perform mathematical calculations within our program. You should be familiar with these basic operators as most of them are covered in elementary mathematics. See the table below for a list of these operators.
Operator | Description | Example |
---|---|---|
+ | Addition operator | 1 + 1 = 2 |
- | Subtraction operator | 1 - 1 = 0 |
* | Multiplication operator | 6 * 2 = 12 |
/ | Division operator | 6 / 2 = 3 |
% | Modulo/ remainder | 6 % 4 = 2 |
The only operator here you are likely to not have seen before is the modulo operator which simply returns the remainder of the sum.
Lets look at some examples of code before we move onto creating a simple calculator.
Try to guess what the output of each of these calculations will be.
int num1 = 1;
int num2 = 2;
System.out.println(num1 + num2);
System.out.println(num2 - num1);
System.out.println(num2 * num2);
System.out.println(num2 / num1);
System.out.println(15 % num 2);
//A few more difficult ones
System.out.println((num1 * num2) + num1);
System.out.println((num2 * num2) % num2);
Answers can be seen above.
Note my use of parentheses on the last two examples. Operators in Java follow the same precedence rules as they do in real-world mathematics.
Relational Operators
Relational operators help us to check the relationship between two pieces of data. See below for a list of the relational operators.
Operator | Description | Example |
---|---|---|
< | Less than | 1 > 1 = false |
> | Greater than | 2 > 1 = true |
<= | Less than or equal to | 6 <= 6 = true |
>= | Greater than or equal to | 1 >= 17 = false |
== | Equal to | 6 == 6 = true |
!= | Not equal to | 5 != 89 = true |
These operators should be familiar to you and are covered in elementary mathematics. The output from using these operators is a boolean so it will always output a true or false value.
Lets have a look at these operators in code. As I will be covering the use of these operators in more depth in the next lesson I am not going to go too much into depth here, they are pretty self-explanatory anyway.
Try to guess the output of each line, answers will be provided below. Remember that the output will either be true or false.
System.out.println(1 < 2);
System.out.println(67 > 890 );
System.out.println(56 >= 56);
System.out.println(61 <= 900);
System.out.println(55 == 155);
System.out.println(100 != 34);
The answers above show that the output from using these operators is a true or false value. The usefulness of these operators will become more apparent when we cover if statements and later loops.
Logical Operators
The last set of operators that I wish to cover are logical operators. These operators are usually paired up with rational operators to provide conditional statements. The use of which will become apparent in the next lesson "If statements".
See the table below for a list of Logical Operators
Operator | Description |
---|---|
&& | AND |
OR | |
! | NOT |
Apologies for the OR statement markdown does not provide an escape character for the pipe character.
The OR operator is a double pipe ||.
The AND operator returns true if the statements on both sides of the operator evaluate to true.
The OR operator returns true if the statements on either side of the operator are true.
The NOT operator returns the opposite from what the statement returns.
Here are some examples below. These examples include the rational statements from the section before. Try and work out what the output of these statements could be. I have given the answer for the first 2. Pay close attention to number 3.
System.out.println(1 < 2 && 1 == 1); //true
System.out.println(67 > 890 || 6 != 6 ); //false
System.out.println(!(56 >= 56));
System.out.println(61 <= 900 || 6 == 6);
System.out.println(55 == 155) && 77 > 3;
System.out.println(100 != 34 || 7 > 3);
Challenge
To finish off with I would like you to create some of your own statements like the ones we created in the last section. The more practice you get with these logical expressions the more familiar you will become with them.
Conclusion
In this lesson we covered almost all of the basic operators that are needed to write basic java programs, there are a few that I have deliberately missed and will come back and touch on them at a later date.
If there is anything in this lesson that confuses you or there is anything programming related that you need help with then please comment below and I will try my best to help you.
As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.
The next lesson that I plan to do will be covering If statements and user input. This will allow us to interact with the program and let the computer make decisions based on our input.
Thank you for reading and I hope that someone will get some use out of these tutorials.
Message to readers
Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.
Just found this. It's an interest of mine. I'll have to look up the other two lessons and check em' out. Thanks so much for taking the time to put it together! Much appreciated!
Awesome, if you have any questions then just drop a comment and i'll help you out :)
Thank you for that! Much appreciated!
Congratulations @robertlyon! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Resteemed by @steemvote - send only 0.5 SBD to get your post resteemed to 2K follower and receive Bonus-Upvotes
Resteemed by @resteembot! Good Luck!
Curious?
The @resteembot's introduction post
The @reblogger's introduction post
Get more from @resteembot with the #resteembotsentme initiative
Check out the great posts I already resteemed.
Congratulations @robertlyon! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP