Skip to main content

13. Assignment Operators

 

Variables

  • Variables are just bit holders, with a designated type. Within that holder is a bunch of bits representing the value.
  • For primitives, the bits represent a numeric value.
  • A reference variable bit holder contains bits representing a way to get to the object.
  • The way in which object references are stored is virtual-machine specific.
  • Instance variables are always initialized with a default value.
  • Local/automatic/method variables are never given a default value. If we attempt to use one before initializing it, we'll get a compiler error.

Primitive Assignments

  • The equal (=) sign is used for assigning a value to a variable, and it's cleverly named the assignment operator.
  • There are total 12 assignment operators.
  • The result of an expression involving anything int-sized or smaller is always an int.
  • class ShellClass {
        public static void main(String[] args) {
            byte a = 60;
            byte b = 9;
            byte c = b + a;
        }
    }

  • The compound assignment operators will all put in an implicit cast.
  • class ShellClass {
        public static void main(String[] args) {
            byte c = 60;
            c += 9;
        }
    }

Primitive Casting

  • Casting lets us convert primitive values from one type to another. Casts can be implicit or explicit.
  • An implicit cast means we don't have to write code for the cast; the conversion happens automatically.
  • An implicit cast happens when we're doing a widening conversion i.e., putting a smaller thing into a bigger container.
  • class ShellClass {
        public static void main(String[] args) {
            int a = 10;
            double d = a; // Implicit Cast
            System.out.println(d);
        }
    }

  • The large-value-into-small-container conversion is referred to as narrowing and requires an explicit cast, where we tell the compiler that we're aware of the danger and accept full responsibility.
  • When we cast a floating-point number to an integer type, the value loses all the digits after the decimal.
  • class ShellClass {
        public static void main(String[] args) {
            double a = 10.50;
            int d = (int) a; // Explicit Cast
            System.out.println(d);
        }
    }

Assigning Numeric Values

  • A literal integer is always implicitly an int.
  • If we're assigning to a byte variable, then the compiler automatically narrows the literal value to a byte.
  • class ShellClass {
        public static void main(String[] args) {
            byte a = 10;
        }
    }
  • Every floating-point literal is implicitly a double.
  • If we try to assign a double to a float, the compiler knows we don't have enough room in a 32-bit float container to hold the precision of a 64-bit double.
  • class ShellClass {
        public static void main(String[] args) {
            float a = 10.50;
        }
    }

  • To assign a floating-point literal to a float variable, we must either cast the value or append an f or F to the end of the literal.
  • class ShellClass {
        public static void main(String[] args) {
            float a = (float) 10.50;
            float b = 10.50f;
            float c = 10.50F;
        }
    }
  • We'll also get a compiler error if we try to assign a literal value that the compiler knows is too big to fit into the variable.
  • class ShellClass {
        public static void main(String[] args) {
            byte a = 128;
        }
    }

  • When we narrow a primitive, Java simply truncates the higher-order bits that won't fit. It loses all the bits to the left of the bits we're narrowing to.
  • class ShellClass {
        public static void main(String[] args) {
            byte a = (byte) 128;
            System.out.println(a);
        }
    }

Assigning Primitive Variable to Primitive Variable

  • When we assign one primitive variable to another, it means copy the bit pattern in right-side variable, and place the copy into left-side variable.
  • At this point, both the variables have identical contents, but if we change the contents of either any variable, the other variable won't be affected.
  • class ShellClass {
        public static void main(String[] args) {
            int a = 69;
            int b = a;
            System.out.println("a: " + a + "\tb:" + b);
            a = 100;
            System.out.println("a: " + a + "\tb:" + b);
        }
    }

Reference Variable Assignments

  • We can assign a newly created object to an object reference variable.
  • we can also assign null to an object reference variable, which simply means the variable is not referring to any object.
  • class ShellClass {
        public static void main(String[] args) {
            Object refVar = new ShellClass();
            refVar = null;
        }
    }
  • we can assign a subclass of the declared type, but not a superclass of the declared type.
  • class ShellClass {
        public static void main(String[] args) {
            ShellClass refVar = new Object();
        }
    }

  • If we assign an existing instance of an object to a new reference variable, then two reference variables will hold the same bit pattern - a bit pattern referring to a specific object on the heap.
  • For any object type (other than String), where two references refer to the same object, if either reference is used to modify the object, both references will see the changes because there is still only a single object.
  • import java.awt.Dimension;

    class ShellClass {
        public static void main(String[] args) {
            Dimension a = new Dimension(69, 69);
            Dimension b = a;

            System.out.println("a's Height: " + a.getHeight() + "\tb's Height: " + b.getHeight());

            b.height = 10;
            System.out.println("a's Height: " + a.getHeight() + "\tb's Height: " + b.getHeight());
        }
    }

  • But any time we make any changes at all to a String, the VM will update the reference variable to refer to a different object.
  • class ShellClass {
        public static void main(String[] args) {
            String a = new String("Deepak");
            String b = a;

            System.out.println("a: " + a + "\tb: " + b);

            b = "Sharma";
            System.out.println("a: " + a + "\tb: " + b);
        }
    }

Prev Next

Comments