Assignment statement in Java:

  1. Syntax:

          variableName = expression
    

    =     is the assignment operator in Java
    (It's similar to :=   in Pascal)

  2. Effect of assignment statement - the short story::


    It's a no brainer... unless the type of expression is different from that of variableName...

  3. Effect of assignment statement - the full story:


    NOTE: if the conversion step is not allowed (converstion is not widening), Java compiler will signal error

    NOTE: some programming languages (like C & C++) will allow more flexible convertion rules....
                    C/C++ allow any conversion between integer and float types !!!


  4. Examples assignment statements:

       int i;
       double d;
    
       i = 4;	allowed in Java & C/C++
    		No conversion needed
       d = 4;	allowed in Java & C/C++
    		converts 4 to 4.0 before assigning to d
       i = 4.0;	Not allowed in Java but allowed in C/C++
    		In C/C++, 4.0 is converted into 4 before assigning to i
       d = 4.0;	allowed in Java & C/C++
    		No conversion needed
       d = i;	allowed in Java & C/C++
    		converts the value of i into double before assigning to d
       i = d;	Not allowed in Java but allowed in C/C++
    		In C/C++, value of d is converted into integer before 
    		assigning to i
    

  5. Use the following program to try out what is taught on this page: click here