String Concatenation Operator(+)

The only overloaded operator in java is ‘+’ operator.Some times it acts as arithmetic addition operator some times it acts as “String concatenation Operator”.

Example:

public class Test {
	public static void main(String[] args) {
		String s1="Wel";
		String s2="Come";
		String s3=s1+s2;
		System.out.println(s3);
	}
}
/*
Output:
WelCome
*/
  1. If at least one operand is String type then ‘+’ operator acts as concatenation otherwise it acts as arithmetic addition operator.
    Example:

    public class Test {
    	public static void main(String[] args) {
    		  String a="java2learn";
    		  int b=10;
    		  int c=20;
    		  int d=30;
    		  System.out.println(a+b+c+d);
    		  System.out.println(b+c+d+a);
    		  System.out.println(b+c+a+d);
    		  System.out.println(b+a+c+d);
    	}
    }
    /*
    Output:
    java2learn102030
    60java2learn
    30java2learn30
    10java2learn2030
    */
    

Assignment Operators(=)

This operator is used to store some value into a variable.
It is used in 3 ways:

  1. It is used to store a value into a variable
    Example:int x=5;
  2. It is used to store the value of a variable into another variable.
    Example:int x=y;//Here the value of y is stored into x.
  3. It is use to store the value of an expression into a variable.

    Example:

    public class Test {
    	public static void main(String[] args) {
    		int y=10;
    		int z=30;
    		int x=y+z-4;
    		System.out.println(x);
    	}
    }
    /*
    Output:
    36
    */
    

Compound assignment operator (OR)compact Notation

Some times assignment operator will mix with some other operator to form compound assignment operator.

Expanded Notation compact Notation Name Operator
x=x+10 x+=10 += is called addition Assignment Operator
sal=sal*10.5 sal*=10.5 *= is called Multiplication Assignment Operator
value=value-discount value-=discount -= is called Substraction Assignment Operator
p=p/100 p/=100 /= is called Division Assignment Operator
num=num%5.5 num%=5.5 %= is called modules Assignment Operator

In the case of compound assignment operators compile will perform the required implicit type Casting
that is b+=1=>b=(byte)(b+1);
Example:

public class Test {
	public static void main(String[] args) {
		byte b=10;
		b=b+1;
		System.out.println(b);
	}
}
/*
Compile time error:
possible loss of precision
required:byte
found:int
*/

Example:


public class Test {
	public static void main(String[] args) {
		 byte b=10;
		 b+=1;
		 System.out.println(b);
	}
}
/*
Output:
11
*/