ROAD TO ANDROID DEVELOPER-Season 1-JAVA-Operators
Hi all, I know I have promised that I will be consistent in uploading articles on this Android series but for the past couple of months I have been occupied so I didn’t find much time to write but now I am back so let’s continue from where we left of. If I am right, we have finished till variables and datatypes(If you haven’t checked them out, I have given the link below), so we will see about Java Operators in this article.
Operators:
Operators are kind of symbols that are used to do certain operations on variables or values. Those symbols, based on the operations they perform, are classified into many types,
- Arithmetic Operators : These operators are used to perform arithmetic operations like those you would have done during your school days like addition, subtraction, multiplication, division and modulo division(Do they ring a bell? lol). Those of you who are new to coding might be wondering what is this modulo operation?. When it comes to programming when you do a division operation, the result you get will be the quotient but when you want to get the reminder, that’s where modulo will come into play. (Confusing? Don’t worry you will understand from the example code below)
class Main {
public static void main(String[] args) {
int x= 3, y= 2;
// addition operator
System.out.println(x+y); //Here + is the operator and on adding x and y the
//result printed will be 5
// subtraction operator
System.out.println(x-y); //Here - is the operator and on subtraction the
//result printed will be 1
// multiplication operator
System.out.println(x*y); //Here * is the operator and on multiplication the
//result printed will be 6
// division operator
System.out.println(x/y); //Here / is the operator and on division the
//result printed will be 1(quotient)
// modulo operator
System.out.println(x%y); //Here %is the operator and on modulo operation the
//result printed will be 1(remainder)
}
}
- Relational Operators : These operators are used to compare and verify two values like whether two particular values are equal or whether one is greater or lesser than the other…something like that…you get the gist right? We will see more clearly in the example code below.
class Main {
public static void main(String[] args) {
int x= 2, y= 5;
// == operator
System.out.println(x == y); //This is the equality operator which checks if
//values of x and y are equal, if they are, then it prints
//true or else false.
// != operator
System.out.println(x != y); // This is notequal operator. It checks if x and y are notequal
//If not equal, it returns true else false.
// > operator
System.out.println(x > y); //This is greater than operator which checks if x is greater
//than y and returns true or false.
// < operator
System.out.println(x < y); //This is lesser than operator which checks if x is lesser
//than y and returns true or false.
// >= operator
System.out.println(x >= y); //This is greater than or equal to operator which checks if x is either greater
//than y or equal to y, It should satisfy any one of the condition to return true
// <= operator
System.out.println(x<=y); //This is lesser than or equal to operator which checks if x is either lesser
//than y or equal to y, It should satisfy any one of the condition to return true
}
}
If it is not clear even now, don’t worry you will get more clarity on it when we see conditional statements.
- Logical Operators : These operators are used to check certain logic and return true or false. These too are used extensively in conditional statements or to be even more precise, usually, logical operators are used in combination with relational operators and then used in conditional statements.
(i) &&(Logical AND) — Returns True only if the results of both sides of the operator are True.
(ii) ||(Logical OR) — Returns True if either one of the results of both sides of the operator is True.
(iii) ! (Logical NOT) — Returns True if the result is False and vice versa.
Let’s see an example to understand clearly.
class Main {
public static void main(String[] args) {
// && operator
System.out.println((8 > 1) && (9 > 3)); // First it checks the results of
//both sides of the AND operator which becomes true && true. Now, since both
//sides of the operator are true the final result is true.
// || operator
System.out.println((5 < 3) || (8 > 5)); // First it checks the results of
//both sides of the OR operator which becomes False && true. Now, since one of
//the sides of the operator is true the final result is true.
System.out.println((5 < 3) || (8 < 5)); // false || false. Since not even
//one of the sides is true, the final result is false.
// ! operator
System.out.println(!(5 == 3)); // First it checks whether 5 is equal to 3.
// Since they are unequal the result of (5==3) will be false but there is a ! symbol
//before(5==3) which means !false which again means true. so the final result is
//True.
}
}
Note : From the above example look carefully at how relational and logical operators are used together in combination, this will be useful while looking into conditional statements.
- Assignment Operator : This Operator is used for assigning values to variables.
class Main {
public static void main(String[] args) {
int x = 2;
int y;
y = x; // Assigning value of x to y using = operator which means value
// of y is now 2
System.out.println(y);//prints 2
y += x; // this operator is equivalent to y=y+x which means y=2+2 so now
//new value of y is 4
System.out.println(y); //prints 4
y*= x; //this is equivalent to y=y*x which means y=4*2 so now new value of
//y is 8
System.out.println(y);//prints 8
}
}
- Bitwise Operators : These operators are used to perform operations on individual bits of a number. i.e. any number can be represented in the form of binary digits aka bits. Bitwise operators are used to perform operations on those bits.
(i)Bitwise AND(&) : If both bits are 1 it gives 1 else 0. For example lets consider bitwise AND of 1 and 3.
binary of 1 is 0001, binary of 3 is 0011
0001
0011
-----
0001 --- which gives decimal number 1
I know many of you are so utterly confused and have no freaking idea of what I am talking about. Don’t worry bitwise operators are not of much use atleast in Android development but they are useful when it comes to embedded programming(which involves programming the hardware), network programming, working on queries in the backend etc.. Just know that there is an operator like this.
(ii)Bitwise OR(|) : When any one of the bits of both sides is 1 it gives 1 else 0.
// lets take bitwise OR of same 3 and 1
0011
0001
----
0011----which gives result as 3
(iii)Bitwise XOR(^) : When two bits are different it gives 1 else 0.
// lets take bitwise XOR of same 3 and 1
0011
0001
----
0010----which gives result as 2
(iv)Bitwise Complement(~) : This operator inverts all the bits that is if a bit is 1, it changes it to 0 and vice versa.
// lets take bitwise complement of same 3
0011
----
1100----which gives result as 12
Let’s see the code
public class bitwiseoperator {
public static void main(String[] args)
{
int x= 3;
int y= 1;
// bitwise and
// 0011 & 0001=0001 = 1
System.out.println(x&y);//prints 1
// bitwise or
// 0011 & 0001=0011 = 3
System.out.println(x|y);//prints 3
// bitwise xor
// 0011 & 0001=0010 = 2
System.out.println(x^y);//prints 2
// bitwise complement
// 0011=1100=12
System.out.println(~x);//prints 12
}
}
Just know that there is an operator like this. If you want to know more about how decimal number is converted to binary and more about how the bitwise operator works, comment down below or reach out to me on my social media channels which I have given below, I will make a separate article on them :)
- Unary Operators : This operator performs operation on only one number or a variable.
(i)Increment and Decrement Operators : These operators either increases the value by 1 or decreases the value by 1 respectively.
-> ++ is the increment operator.
-> — — is the decrement operator.
Let’s see an example
class unary{
public static void main(String[] args) {
int z=2;
int x,y;
// original value
System.out.println(z); //prints 2
// increment operator
x= ++z;
System.out.println(x); //prints 3. now z is also increased to 3 i.e. new
// value of z is now 3
// decrement operator
y = --z;
System.out.println(y); //prints 2. Now new value of z is 2
}
}
In increment and decrement operators, there is a concept known as prefix and postfix.
-> Prefix operator is written as ++num or — num(notice the + sign and — sign is before the vatriable). During prefix operation, first the value is incremented or decremented and then returned.
-> Postfix operator is written as num++ or num — (notice the + and — sign is after the variable). During the postfix operation, the value is returned and then it is incremented or decremented.
Not clear? Don’t worry we will see an example,
class prefixandpostfix{
public static void main(String[] args) {
int x=3,y=3;
//postfix
System.out.println(x++);//3 will be displayed(not 4)only after displaying
//3, x will get increased to 4
System.out.println(x); //Now 4 will be printed.
//prefix
System.out.println(++y);//4 will be printed as this is prefix where
//first the increment is done and then the value is returned
}
}
In similar manner, prefix and postfix decrement can be done(try it yourself ;) )
If you have concentrated carefully so far you would have got a doubt by now, If Unary operator works on single number or variable, then what about logical NOT operator we saw above?
If you have got this doubt BINGO!! Yup that’s right NOT operator too comes under Unary operator.
(ii) Logical Complement and Bitwise Complement : Both of these operators which we have already seen above comes under Unary. Logical Complement which is represented by ! is used to invert the result of a boolean and bitwise complement which is represented by ~ is used to invert the bits (i.e. 1 to 0 and vice versa).
Examples of these two operators are already there above so kindly refer them.
All these operators are the commonly used operators, I hope you guys would have found this useful. In my next article, we will be talking about Conditional Statements….so until then CIAO!! :)
In any case, If you missed my previous article on variables and datatypes, check it out here,
https://medium.com/@TechTiger/road-to-android-developer-season-1-java-variables-and-datatypes-f3db8624589
If you have any queries or if I have made any mistakes in my article (as I am merely a human lol), feel free to reach out to me on
Email : sktharanibalan@gmail.com
Instagram : name_is_tharani_
Disclaimer : The technical stuffs I am mentioning in this series might be factually wrong at some places or it might be short on content so I would recommend to read this stuff just as a refresher or for getting started as a beginner. For more detailed stuff follow other sites out there on the internet. Thank you