When a variable is declared, the JVM sets aside exactly enough bits to hold a variable of that type, with the value of its “memory box” (a block of contiguous memory) inaccessible to prevent access to any leftover, transient data. When a value is assigned to that variable, it will overwrite this transient value and the memory box will become accessible.
When an object of a class is instantiated, the JVM sets aside exactly enough bits for each instance variable of the class and assigns each a default value (0
or null
).
States that the assignment operator =
will copy the binary value stored in one variable’s memory box to another variable’s memory box.
When this occurs from a global to a function scope, it is known as passing by value. The binary values stored in each of the memory boxes of the arguments are copied to the memory boxes of new variables in the function scope (i.e. their values are passed in)
In Java, there are two types a variable can be of.
Primitive. Variables of this type have memory boxes which store a value of fixed size or a transient piece of data if unassigned.
Reference. Variables of this type have memory boxes which store the 64-bit address of an object in memory or
null
if unassigned. A reference variable is assigned to the address of an object through thenew
keyword, which finds a location in memory that can store the object’s members and returns this location.
Two variables of a different type may store the same binary value in their memory boxes.
int u = 72;
String v = 'H';
u -> 01001000
v -> 01001000
A class which is doesn’t really stand on its own can be nested inside another class. If that nested class is given the private
modifier, only the enclosing class can access it. If it is also given the static
modifier, then it cannot access the outer class’s instance variables.
public class A {
private class B {
// ...
}
// ...
}