Strings and Object References in Java

The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator '+', and about how Strings are immutable.

2. Easy way to Construct Strings


Answer:

  1. A new String object is created, containing the characters between quote marks.
  2. A reference to the object is saved in the variable zeta.

Easy way to Construct Strings

String objects are very useful and are frequently used. To make life easier for programmers Java has a short-cut way of creating a String object:

String zeta = "The last rose of summer" ;

This creates a String object containing the characters between quote marks. A String created in this short-cut way is called a String literal.

Remember that if several statements in a program ask for literals containing the same characters only one object will actually be constructed (see chapter 41). Other classes do not have short-cuts like this. Objects of most classes are constructed by using the new operator.


Question 2:

Can a String reference be a parameter for a method?