<< Chapter < Page Chapter >> Page >

Reference types

Once again, what is a primitive type?

Primitive types are types where the name of the variable evaluates to the value stored in the variable.

What is a reference type?

Reference types in Java are types where the name of the variable evaluates to the address of the location in memory where theobject referenced by the variable is stored.

The above statement may not really be true?

However, we can think of it that way. Depending on the particular JVM in use, the reference variable may refer to a table in memory where the address of the object is stored. In that case the second level of indirection is handled behind the scenes and we don't have to worry about it.

Why would a JVM elect to implement another level of indirection? Wouldn't that make programs run more slowly?

One reason has to do with the need to compact memory when it becomes highly fragmented. If the reference variables all refer directly to memory locations containing the objects, there may be many reference variables that refer to the same object. If that object is moved for compaction purposes, then the values stored in every one of those reference variables would have to be modified.

However, if those reference variables all refer to a table that has one entry that specifies where the object is stored, then when the object is moved, only the value of that one entry in the table must be modified.

Fortunately, that all takes place behind the scenes and we as programmers don't need to worry about it.

Primitive vs. reference variables

We will discuss this in more detail in a future module. For now, suffice it to say that in Java, a variable is either a primitive type or a reference type,and cannot be both.

Declaring, instantiating, initializing, and manipulating a reference variable

The fragment of code shown in Listing 7 , (which was taken from the program shown in Listing 6 that deals with wrappers) does the following. It

  • declares,
  • instantiates,
  • initializes, and
  • manipulates a variable of a reference type named myWrappedData .

In Listing 7 , the variable named myWrappedData contains a reference to an object of type Double .

Listing 7 . Aspects of using a wrapper class.
Double myWrappedData = new Double(5.5); //Code deleted for brevity//Note the use of the doubleValue() method to obtain the // value of the double wrapped in the Double object.System.out.println ("My wrapped double is " + myWrappedData.doubleValue() );

Variable names

The rules for naming variables are shown in Figure 3 .

Figure 3 . Rules for naming variables.
  • Must be a legal Java identifier (see below) consisting of a series of Unicode characters. Unicode characters are stored in sixteen bits, allowingfor a very large number of different characters. A subset of the possible character values matches the 127 possible characters in the ASCII characterset, and the extended 8-bit character set, ISO-Latin-1 (The Java Handbook, page 60, by Patrick Naughton).
  • Must not be the same as a Java keyword and must not be true or false.
  • Must not be the same as another variable whose declaration appears in the same scope.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask