0

I just wanted to know what is the difference between a = a+b; and a += b;

consider,

int a = 10;
long b = 20;

a = a+b; // is not possible 

but

a += b; // is possible.

Thank you!

package com.test;


public class ClassCast {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int a = 1;
        long b = 2;

        /*Not possible. Compile time error.
        a = a+b;
        */

        //Possible. Why?
        //a += b;

        System.out.println(a += b);

    }

}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51

1 Answers1

0

So a += b is equivalent to a = (int) (a + b).

implicit conversion done by compiler

rackdevelop247
  • 86
  • 1
  • 10