JAVA Fundamentals
Long
byte short int
Float double
The compiler won’t let you put a value from a large cup into
a small one. But what about the other way—pouring a
small cup into a big one? No problem.
NO!
Don’t do that
Be sure the value can fit into the variable.
what about non-primitive variables?
what about objects?
Person p= new Person();
p.eat();
There is actually no such thing as an object variable.
■ There’s only an object reference variable.
■ An object reference variable holds bits that represent a way to access an object.
■ It doesn’t hold the object itself, but it holds something like a pointer. Or an address. Except, in Java we don’t
really know what is inside a reference variable. We do know that whatever it is, it represents one and only one
object. And the JVM knows how to use the reference to get to the object.
For eg
byte x = 7; Scenario 1:
The bits representing 7 go
into the variable. (00000111). Book A =new Book();
Book B =new Book();
Reference Variable Book C =B;
Dog myDog = new Dog();
The bits representing a way to get to
the Dog object go into the variable. Scenario 2:
The Dog object itself does not go into
the variable Book A =new Book();
Book B =new Book();
A=B;
Brain Strom? B=Null;
1.How big is a reference variable?
2. Does all object references are the same size, regardless of the
size
of the actual objects to which they refer?
3. Can I do arithmetic on a reference variable, increment it?
A reference
variable is like a
remote control.
A primitive A reference Using the dot
Variables come in Variables must variable value is variable value is operator (.) on a
A reference it is not
two flavors: always be declared the bits the bits reference variable
variable has a referencing any
primitive and with a name and a representing the representing a way is like pressing a
value of null when object.
reference. type. value (5, ‘a’, true, to get to an object button on the
3.1416, etc.). on the heap remote control to
access a method
or instance
variable.