Java assignment operator example

This example will demonstrate the use of assignment operator in Java. Assignment operators can be divided in Simple(“=“) and Shorthand assignment operator(“+=“). In this example you will find the use of both type of operator.


public class AssignmentOperator {

	public static void main(String[] args) {

		int i = 0, j = 10; // Simple Assignment operator
		int k = 5;

		JBT jbt = new JBT(); // Here Assignment operator is used to assign the
								// reference of an Object

		int l = i + j; // Addition of two int is assigned to another integer.
						// Use of Simple Assignment

		i = i + j; // Simple Assignment Operator

		System.out.println("Value of (i+j) :" + i);

		i += j; // Shorthand assignment Operator. Same as i=i+j

		System.out.println("Value of i :" + i);


class JBT {
	// Nothing Here

By | 2013-04-13T16:20:44+00:00 April 13th, 2013|Learn By Example|1 Comment

About the Author:

One Comment

  1. Java Developer September 18, 2013 at 9:01 am - Reply

    Also available some more assignment operator as below :
    <>= Right shift AND assignment operator
    &= Bitwise AND assignment operator
    ^= Bitwise exclusive OR and assignment operator
    |= Bitwise inclusive OR and assignment operator

Leave A Comment