<< Chapter < Page Chapter >> Page >

Alternative string instantiation constructs

The first thing to notice is that a String object can be created using either of the following constructs:

Alternative String instantiation constructs String str1 = new String("String named str2");String str2 = "String named str1";

The first approach uses the new operator to instantiate an object while the shorter version doesn't use the new operator.

Later I will discuss the fact that

  • the second approach is not simply a shorthand version of the first construct, but that
  • they involve two different compilation scenarios with the second construct being more efficientthan the first.

Instantiating stringbuffer objects

The next thing to notice is that a similar alternative strategy does not hold for the StringBuffer class.

For example, it is not possible to create a StringBuffer object without use of the new operator. (It is possible to create a reference to a StringBuffer object but it is later necessary to use the new operator to actually instantiate an object.)

Note the following code fragments that illustrate allowable and non-allowable instantiation scenarios for StringBuffer objects.

Instantiating StringBuffer objects //allowed StringBuffer str3 = new StringBuffer("StringBuffer named str3"); //not allowed//StringBuffer str4 = "StringBuffer named str4"; o.d("Try to create and append to StringBuffer " +"without using new -- not allowed"); //StringBuffer str7;//str7.append("StringBuffer named str7");

Declaration, memory allocation, and initialization

To review what you learned in an earlier module, three steps are normally involved in creating an object (but the third step may be omitted) .

  • declaration
  • memory allocation
  • initialization

The following code fragment performs all three steps:

Declaration, memory allocation, and initialization StringBuffer str3 = new StringBuffer("StringBuffer named str3");

The code

StringBuffer str3

declares the type and name of a reference variable of the correct type for the benefit of the compiler.

The new operator allocates memory for the new object.

The constructor call

StringBuffer("StringBuffer named str3")

constructs and initializes the object.

Instantiating an empty stringbuffer object

The instantiation of the StringBuffer object shown above uses a version of the constructor that accepts a String object and initializes the StringBuffer object when it is created.

The following code fragment instantiates an empty StringBuffer object of a default capacity and then uses a version of the append method to put some data into the object. (Note that the data is actually a String object -- a sequence of characters surrounded by quotation marks.)

Instantiating an empty StringBuffer object //default initial length StringBuffer str5 = new StringBuffer();//modify length as needed str5.append("StringBuffer named str5");

It is also possible to specify the capacity when you instantiate a StringBuffer object.

Some authors suggest that if you know the final length of such an object, it is more efficient to specify that length when the object is instantiated than tostart with the default length and then require the system to increase the length "on the fly" as you manipulate the object.

Questions & Answers

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
cell is the building block of life.
Condoleezza Reply
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