Defining the String literal

Defining the String literal can be done simply, as follows:

        String a= "hello";

We created a string, which is hello, and stored it in the variable called a. This is how we define a String, which is the same as defining the String literal.

Let's say that you have defined one more string, as follows:

        String a= "hello";
String b= "hello";

Unfortunately, even the b variable has a hello string, and the a variable also has the same string defined. When the Java program compiles this, it creates one String object called a, and assigns hello.

Now, before creating an object for this hello string, the b variable first checks whether there is any hello string instance already defined in the String pool. If it is already defined, it simply refers a to the b object rather than creating one more object separately.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.78.174