Operators in java
Operators can be categorized in the following types:
Concatenation:
In Java, + operator is very flexible which means that it can be used to perform arithmetic operation as well as used to concatenate strings. Therefore it is also called as String Arithmetic Operator and String Concatenation Operator. For example,
System.out.println(2+3); System.out.println("Mark"+" Zuckerburg"); System.out.println("Mark"+42);
Output of the above code snippet will be:
5 Mark Zuckerburg Mark42
As you can analyze that when the + operator is given 2 numeral values, it will give you numeral output, while when you give 2 string values then it concatenates them. Moreover, when you give 1 numeral value and 1 string value then it concatenates them. But what to do when we want to add the numeral values instead of concatenating them with the string. Analyze the following line of code:
System.out.println("Sum is "+2+3); //output will be //Sum is 23 System.out.println("Sum is "+(2+3)); //output will be //Sum is 5
Note- To use the + operator as arithmetic rather than concatenation operator then paranthesis should be used.
To assign the value of + operator there are some restrictions, which says that results of arithmetic operations can only be stored in the datatype compatible with arithmetic data types like (int, double, float etc) and results of concatenation can only be stored in String type variable.
int x=10, y=20; String z="John"; x=y+z; //invalid, incompatible types z=x+z; //valid y=x+y; //valid z=x+y; //invalid, incompatible types
Relational Operator:
These operators are used for comparison purposes. It requires 2 variables or constant values for operation, on giving more than 2 variables then the compiler will give syntax error. Their resultant value is in Boolean data type i.e. either true or false. Relational operator can only be used with primitive data types and not with non-primitive data types like object of classes. These operators are > (greater than), < (less than), <= (less than or equal to), >= (greater than or equal to).
System.out.println(10<20); //valid, true System.out.println('a'<'b'); //valid, true System.out.println(10==10.0); //valid, true System.out.println('a'<100); //valid, true System.out.println("Hi"<"Hi"); //invalid System.out.println(4<5<6); //invalid System.out.println(false<true); //invalid
Note- < (less than) operator can not be applied to String and boolean data types.
Equality Operator
As its name indicates, equality operator checks that the given input variables are either equal in value or not. Relational operator and equality operator are considered to be the same but actually they are not. These operator also takes only 2 variables or constants and return boolean values. These operators are == (equal to), != (not equal to).
'a'=='A' //valid, false 5==5.0 //valid, true 'a'==97 //valid, true true==false //valid, false "Hello"=="Hello" //invalid
Note- Strings cannot be compared through == operator. It is because == operator compares the reference. It only gives true when the 2 references are pointing to the same object.
Thread a = new Thread(); Thread b = new Thread(); Thread c = a; //new reference c is now pointing to object a System.out.println(a==b); //output will be false System.out.println(a==c); //output will be true
Note- References of different classes can not be compared through equality operator. And on doing so compile time error will be given as Incomparable Types. But all the references of any class can be compared with the references of their of parent or child class.
String s = new String("Hello"); Object o = new Object(); Thread t = new Thread(); System.out.println(s==o); //invalid, compile time error System.out.println(o==t); //valid, false System.out.println(s==t); //valid, false
Some special cases that occurs with the usage of equality operators are:
//Case-1 //a is the reference of any class System.out.println(a==null); //valid, false //Case-2 System.out.println(null==null); //valid, true