Java Unit 2 Notes 1
Java Unit 2 Notes 1
String in Java
Strings Creation in JAVA
String Concatenating
Java String Pool
Java String Pool & Heap
String in Java
The Java platform provides the String class to create and manipulate strings.
String can be created in many ways:
1. Using a String literal
The most direct way to create a string is
String myString = “Work is worship";
In above " Work is worship ", is a string literal, a series of characters that is
enclosed in double quotes. Whenever it encounters a string literal in your
code, the compiler creates a String object with its value-in the above case,
Work is worship
2. Using new Keyword
Like any other objects you need to use the new operator to create a new Java
String object.
String myString = new String(" Work is worship ");
Now the variable myString will conatain " Work is worship ", the text
between the double quotes.
Strings Creation in JAVA
char[] charArray = { 'c', 'o', 'r', 'e', ' ', 'j', 'a', 'v', 'a'};
String myString3 = new String(charArray);
System.out.println(myString2);
System.out.println(myString3);
4. Using + operator
Strings are more commonly concatenated with the + operator and using
this operator we can create a string like below:
System.out.println(myFinalString);
Output : CoreJava.
String Concatenating
String Concatenating
String allocation, like all object allocation, is costly in both time and
memory.
The JVM performs some tricky background work while instantiating string
literals to increase performance and decrease memory overhead. To cut
down the number of String objects created in the JVM, the String class
keeps a pool of strings.
Each time your code create a string literal, the JVM checks the string
literal pool first.
If the string already exists in the pool, a reference to the pooled instance
returns.
If the string does not exist in the pool, a new String object instantiates,
then is placed in the pool.
Java String Pool
Java can make this optimization since strings
are immutable and can be shared without
fear of data corruption. For example:
When we create a new string object using
string literal, JVM checks whether that
string literal is present in string pool or
not. If it is not present then it adds it to
the pool.
String myString = "Core Java” ;
Now, when you create another string
object with same string literal, then a
reference of the string literal already
present in string pool is returned.
String myString2 = myString;
Java String Pool
And now if you change the literal in the new string, its reference gets modified.
Remember String is a immutable class, means if you change the value,
internally it is creating a new object and returning the reference.
Java String Pool & Heap
If you create a new string using 'new' keyword, it creates object in heap rather
than in string pool even if the same string literal is present in the pool.
String myString3 = new String()
String Comparison
String Comparison
By equals() method
By = = operator
By compareTo() method
String Comparison using equals() method
By equals() method : compares two strings for equality. It compares the content of
the strings. It will return true if string matches, else returns false. String class
provides two methods:
public boolean equals(Object another) compares this string to the specified object.
public boolean equalsIgnoreCase(String another) compares this String to another
string, ignoring case.
class Stringcomparison{
public static void main(String args[]){
String myString1="Java"; String myString2="Java";
String myString3=new String("Java"); String myString4="Core";
String myString5="JAVA";
System.out.println(myString1.equals(myString2)); //true
System.out.println(myString1.equals(myString3)); //true
System.out.println(myString1.equals(myString4)); //false
System.out.println(myString1.equalsIgnoreCase(myString5)); //true
} }
String Comparison using By == operator
The == operator compares references not values. The == operator compares two
object references to check whether they refer to same instance and it returns true
if refer to same instance else false.
class Stringcomparison{
public static void main(String args[]){
String myString1="Java";
String myString2="Java";
String myString3=new String("Java");
System.out.println(myString1==myString2); //true
System.out.println(myString1==myString3); //false
} }
The code myString1==myString2 returns true because both refer to same
instance in the string pool. The code myString1==myString3 returns false
because myString3 refers to instance created in heap rather than string pool.
String Comparison using compareTo() method
string1==string2 :returns 0
string1>string2 :returns +ve value
string1<string2 :returns -ve value
Note: To use compareTo() function you have to implement the Comparable
Interface.
String Comparison using compareTo() method
class Stringcomparison{
public static void main(String args[]){
String myString1="Java";
String myString2="Java";
String myString3="Core";
System.out.println(myString1.compareTo(myString2)); //0
System.out.println(myString1.compareTo(myString3)); //1
System.out.println(myString3.compareTo(myString1)); //-1
} }
The code myString1.compareTo(myString2) returns '0'
because both are equal,
while the code myString1.compareTo(myString3) returns '1' because myString1>myString3
and the code myString3.compareTo(myString1) returns '-1' because myString3<myString1.
Escape Characters
Console output: