The null literal indicates the absence of any assignment to a reference type variable. Let's look at the following code snippet:
SomeClass someClass = new SomeClass();
someClass.someMethod();
someClass = null;
someClass.someMethod(); // throws NullPointerException
The first statement declares the someClass variable and assigns to it a reference to the object of the SomeClass class. Then a method of this class is called using its reference. The next line assigns the null literal to the someClass variable. It removes the reference value from the variable. So, when in the next line, we try to call the same method again, we get back NullPointerException, which happens only when a reference used is assigned the null value.
The String type is a reference type, too. This means that the default value of a String variable is null. The String class inherits all the methods from the java.lang.Object class as any other reference type does, too.
But in some respects, objects of the String class behave as if they are of a primitive type. We will discuss one such case—when the String object is used as a method parameter—in the Passing reference type values as method parameters section. We will discuss other cases of String behaving like a primitive type now.
Another feature of the String type that makes it look like a primitive type is that it is the only reference type that has more literals than just null. The type String can also have a literal of zero or more characters enclosed in double quotes—"", "$", "abc", and "12-34". The characters of the String literal may include escape sequences, too. Here are a few examples:
System.out.println("\nFirst line.\nSecond line.");
System.out.println("Tab space\tin the line");
System.out.println("It is called a \"String literal\".");
System.out.println("Latin Capital Letter Y with diaeresis: \u0178");
If you execute the preceding code snippet, the output will be as follows:
![](assets/c4ff739c-3cd1-4b9a-8900-8273a4536a18.png)
But, in contrast with the char type literals, String literals do not behave like numbers in an arithmetic operation. The only arithmetic operation applicable to the String type is an addition, and it behaves like a concatenation:
System.out.println("s1" + "s2");
String s1 = "s1";
System.out.println(s1 + "s2");
String s2 = "s1";
System.out.println(s1 + s2);
Run the preceding code and you will see the following:
![](assets/7d0600b2-bac7-4769-a95f-4e7596b5f802.png)
Another particular characteristic of String is that an object of the String type is immutable.