Do you thing that all objects of the same type are the same size?
No. Objects are bigger and more complicated than primitive data types. It would be too restrictive if all objects of the same type had to be the same size.
String
ObjectObjects are big, complicated, and vary in size. You DO NOT automatically get an object when you declare an object reference variable. All you get is a name for a future object. Examine the following:
class EgString { public static void main ( String[] args ) { String str; str = new String( "The Gingham Dog" ); System.out.println( str ); } }
An object contains data and methods (state and behavior).
You can visualize the String
object in the above program like this:
The data section of the object contains the characters. The methods section of the object contains many methods. (Actually, this picture is a simplification. The Java system does something more efficient, but logically equivalent.) Future diagrams sometimes will not show the methods of the object.
(Review:) What does the following new
operator do?
str = new String( "The Gingham Dog" );