String Class Part 1 Tutorial

When learning Java, there is a common misperception that a string is a primitive data type. There are two data types that a variable can refer to: primitive data types and reference types. The most common initialization statement for a String variable is more like a primitive initialization statement than an object initialization statement.
     String s = "ABC"; // looks just like a primitive initialization statement - somewhat confusing
     String s = new String("ABC"); // obviously s is a reference variable pointing to a String object with the value of "ABC"
In both of the examples above, s is a reference variable and "ABC" is the value contained in an object instance on the heap memory. A string literal is a series of characters in your code that is enclosed in double quotes. Whenever the java compiler encounters a string literal in your code, the compiler creates a new String object with its value. Based on this explanation, it should be crystal clear that the string literal "ABC" is equivalent to new String("ABC") in the compiled code.

The most important thing to understand about the String class is that once a string object has been created, it cannot be changed. When learning Java, it is easy to confuse the actual string object with the reference variable that points to the object. For example, suppose we say String s = "ABC"; then right after that we execute s = "DEF";. We are not changing the value of the object that contains "ABC", we are creating a new String object "DEF" and pointing the reference variable s to the new object.

CAN Change                      CANNOT Change   
Reference Variable Value        String Object Value   
s                               ABC
s                               DEF

This tutorial draws on concepts from my Garbage Collection Tutorial and The Object Class Tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringOne
C:\Java>cd StringOne
C:\Java\StringOne>Notepad StringOne.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StringOne {
    public static void main(String args[]) {
        StringOne si = new StringOne();
        System.out.println(si); // display heap memory location that si is refering to
        System.out.println("------\n");

        String one = "ABC";
        String two = one;
        String three = new String("DEF");
        

        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
        System.out.println("one refers to: " + one.getClass().getName() + '@' + Integer.toHexString(one.hashCode()));
        System.out.println("two refers to: " + two.getClass().getName() + '@' + Integer.toHexString(two.hashCode()));
        System.out.println("three refers to: " + three.getClass().getName() + '@' + Integer.toHexString(three.hashCode()));

        System.out.println("\n-------");
        one = three;
        
        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
        System.out.println("one refers to: " + one.getClass().getName() + '@' + Integer.toHexString(one.hashCode()));
        System.out.println("two refers to: " + two.getClass().getName() + '@' + Integer.toHexString(two.hashCode()));
        System.out.println("three refers to: " + three.getClass().getName() + '@' + Integer.toHexString(three.hashCode()));    

        System.out.println("\n-------");
        one = new String("GHI");
        
        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
        System.out.println("one refers to: " + one.getClass().getName() + '@' + Integer.toHexString(one.hashCode()));
        System.out.println("two refers to: " + two.getClass().getName() + '@' + Integer.toHexString(two.hashCode()));
        System.out.println("three refers to: " + three.getClass().getName() + '@' + Integer.toHexString(three.hashCode()));   
    }
}

Now switch back to the command prompt (CMD) and type in javac StringOne.java and press Enter.
Now type in java StringOne and press Enter.


C:\Java\StringOne>javac StringOne.java
C:\Java\StringOne>java StringOne
StringOne@659e0bfd
------

ABC
ABC
DEF
one refers to: java.lang.String@fc42
two refers to: java.lang.String@fc42
three refers to: java.lang.String@107e5

-------
DEF
ABC
DEF
one refers to: java.lang.String@107e5
two refers to: java.lang.String@fc42
three refers to: java.lang.String@107e5

-------
GHI
ABC
DEF
one refers to: java.lang.String@11388
two refers to: java.lang.String@fc42
three refers to: java.lang.String@107e5


Final thoughts

A string literal is a series of characters in your code that is enclosed in double quotes. Whenever the java compiler encounters a string literal in your code, the compiler creates a new String object with its value.


Tutorials