(Review:) What two things does the following statement do?
String zeta = new String("The last rose of summer." );

A good answer might be:

  1. A new String object is created, containing the indicated characters.
  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, as follows:

String zeta = "The last rose of summer." ;

This creates a String object containing the characters between quote marks, just as before. Java also does something to optimize performance, but ignore this for now. A String created in this short-cut way is called a String literal. Only Strings have a short-cut like this. All other objects are constructed by using the new operator.

Some methods require a parameter that is a reference to a String object. For example,

String stringA = "Random Jottings";
String stringB = "Lyrical Ballads";

if ( stringA.equals( stringB ) ) 
  System.out.println("They are equal.");
else
  System.out.println("They are different.");

The String refered to by stringA has an equals() method. That method is called with a parameter, a reference to stringB. The method checks if both strings contain identical characters, and if so, evaluates to true ("returns true").

Careful:   The previous paragraph is accurate, but awkward. Almost always people say "The equals method of stringA is called with stringB". This is fine, as long as you remember that a variable like stringA is not the object, but only a reference to an object. This may seem picky, but there is nothing quite as picky as a computer. To avoid future confusion, make sure you understand variables, objects, and references.

QUESTION 2:

(Review:) Examine the following snippet of code:

String a;
Point  b;
  • What is the data type of the variable a?
  • What is the data type of the variable b?
  • How many objects are there (so far)?
Click Here after you have answered the question