<< 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

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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