Home Previous Next

CSC110AB::Lecture Note::Week 08
Assignments | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates



Code: FutureDay2019.java | Stringer.java | MoreStringExamples.java | DemoStringBuffer.java | ReverseString.java

class String

A string is a sequence of zero or more characters. String objects, once created, cannot be modified (i.e. they are immutable).

Frequently used String methods are:

It is important to remember that when you are testing two String objects for equality, then you must use the equals() method.

   String s1 = new String("hello");
   String s2 = new String();
   String s3 = new String(s1);
   String s4 = "bye";
   s4 = s1;

   if (s1 == s2)   // evaluates to false in this example
      // tests if  s1  and  s2  reference the same String object

   if (s1.equals(s2))   // evaluates to false in this example
      // tests if the content of the object referenced by  s1
      // equals the content of the object referenced by  s2

   s1.equals(s3)   // evaluates to true in this example
   s1 == s3        // evaluates to false in this example
   s1.equals(s4)   // evaluates to true in this example
   s4.equals(s1)   // evaluates to true in this example
   s1 == s4        // evaluates to true in this example

Docs.Oracle.com::String API

Additional String Related Notes

Here are some more notes concerning some of the String instance methods.

The substring() method is used to get a sub-string of an instance of a String object.
   public String substring(begin_i, end_i);

   "Hello".substring(0, 2)

      returns a String object with  He  as its content.
startsWith() and endsWith()
The startsWith() and endsWith() return true or false if the this object starts or ends with a particular String value.
   public boolean startsWith(String prefix);
   public boolean endsWith(String suffix);

   "WebAccount.java".startsWith("Web")   returns  true
   "WebAccount.java".startsWith("web")   returns  false
   "WebAccount.java".endsWith("Web")     returns  false
   "WebAccount.java".endsWith(".java")   returns  true
The compareTo() method is used to test String objects for equality. You use it over the equals() method when you need to determine if one string is less than or greater than another string (e.g. you are sorting strings). If compareTo() returns a value less than 0, then this string is lexicographically less than the string received as a parameter; otherwise, a 0 return from compareTo() implies the strings are equal; else, a positive return value indicates that this string is lexicographically greater than the parameter string.
   public int compareTo(String that);

   "hello".compareTo("bye")     returns positive value
   "hello".compareTo("jello")   returns negative value
   "hello".compareTo("hello")   returns zero
[review Stringer.java]

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

class StringBuffer

The String class is used to create immutable String objects. In some cases, when you are dynamically building a string, it is more efficient to use a StringBuffer object. The StringBuffer class implements a buffer for characters that allows updates and allows the buffer to grow and shrink as needed to accommodate the updates.

Oracle.com::API::class StringBuffer | class String

[ DemoStringBuffer.java]

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)


An array is a aggregate (collection, set, group, list) of individual data values with two characteristics: it is ordered and homogeneous.

The following are some array-related terms:

An array is homogeneous because each element must be of the same type. Example: an array of int values, an array of float values, an array of char values, and array of Object values (handles, references, pointers).

An array is ordered because it has a 1st element, a 2nd element, a 3rd element, and so on. Array elements are probably stored in contiguous memory locations [this is definitely true in C and C++; in Java it is left up to the JVM implementors].

Here is a list of items you should understand when using arrays:

Defining an Array of Primitive Types

The following creates an array of long values:

   long[] ssNbrs;      // an array of social security numbers

      // ssNbrs is an object variable that is going to point to
      // an array of  long  values; at this point and time, it 
      // doesn't point to anything (i.e. it is equal to  null)


   int n = fooA();    // get the array length somehow


   ssNbrs = new long[n];

      // array length is decided at run-time (i.e. we don't know
      // the array length at compile-time; the information is
      // obtained from a user, or from a database file, etc.)
      // two things occur when an array is defined:  memory is
      // allocated from the heap and the memory is initialized
      // to the appropriate default values  [0 for integral and
      // floating-point types,  false  for  boolean  arrays, and
      // null  for arrays of object references]


   for (int i = 0; i < ssNbrs.length; i++)
      ssNbrs[i] = -1L;  // initialize each element to -1

      // use the  <  relational operator is important; a common
      // programming defect is to use the  <=  operator -- what
      // would happen if we did?


   ssNbrs = new long[2 * n];

      // after an array has been created, its length if fixed; if you
      // need more elements, then you need to create a new array;
      // now  ssNbrs  references (i.e. points to) an entirely
      // different array of long values; the original array still
      // exists, but the reference to it has been lost and it are
      // not able to copy the original values into the new array

   long[] orig = ssNbrs;
   ssNbrs = new long[n * 3];

   for (int i = 0; i < orig.length; i++) ssNbrs[i] = orig[i];

      // the handle to the original array is stored in the array variable
      // named  orig.  The new array is allocated and the contents of
      // the original arrray is copied to the new array

   System.arraycopy(orig, 0, ssNbrs, 0, orig.length);

      // arraycopy(Object src, int srcOffset, 
      //           Object destdst, int destOffset, int count);
      // the  arraycopy()  method copies a region of one array, src,
      // beginning at element  srcOffset, to another array, dst,
      // beginning at element  destOffset;  count  elements are copied.
      // note:  the arrays must already be allocated

   Tip:  In the control-EXPR of the loop, I'm better off using
         ssNbrs.length  rather than the variable  'n'  because 
         since 'n' is a variable, it could be modified such that 
         the EXPR  n == ssNbrs.length  is not true.
Defining an Array of Objects

The following code snippet an array of object variables.

   Integer[] iArray;

      // iArray  is a reference (handle, pointer) to an array of 
      // Integer references -- at this point and time, it doesn't
      // "point" to any array


   iArray = new Integer[10];

      // array length is known compile-time; an array to store 
      // 10 Integer references is created; each array element is 
      // initialized to   null   (recall,  null  is a Java keyword)


   for (int i = 0; i < iArray.length; i++)
      iArray[i] = new Integer(i);

         // an  Integer  object is created and a reference to it is
         // stored in  iArray; when the Integer object is created,
         // it is initialized to the value of 'i'...

Arrays are always stored on the heap. The reference to the array will be stored either on the stack or in the data segment depending on where it is defined.

The following are some comments on the efficiency of arrays:

Arrays.java | A.java

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)

Home Previous Next