SlideShare a Scribd company logo
IDEAL EYES BUSINESS COLLEGE
A
PRESENTATION
STRING IN
JAVA LANGUAGE
1
PERSENTED BY:- VIVEK KUMARPERSENTED TO:- PANKAJ SIR
String
��� Generally string is a sequence of characters. But in java,
string is an object.
 String class is used to create string object.
 string is a 16-bit Unicode character .
There are two ways to create String object:
1. By string literal 2. By new keyword
string literal
For example:-
String s1="Welcome";
String s2="Welcome"; //no new object will be created
Note:- String objects are stored in a special memory area known as
string constant pool inside the Heap memory.
String literal is create by double quote.
Each time you create a string literal, the JVM checks the string
constant 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
In the above example only one object will be created . First time JVM will find no string object with
the name "Welcome" in string constant pool , so it will create a new object . Second time it will find
the string with the name "Welcome" in string constant pool , so it will not create new object
whether will return the reference to the same instance.
By string literal
class String _literal
{
public static void main(String args[])
{
String x ="java";
String x1="java";
System.out.println( "x="+x);
}
}
By new keyword
In such case, JVM will create a new String object in normal (
nonpool ) Heap memory and the literal "Welcome" will be
placed in the string constant pool . The variable s will refer to
the object in Heap( nonpool ).
String s=new String("Welcome");
//creates two objects and one reference variale
For example:-
By new keyword
1. class new_keyword
2. {
3. public static void main(String args[])
4. {
5. String x ="java";
6. String s=new String("Welcome");
7. System.out.println( "x="+s);
8. System.out.println( "x="+x);
9. }
10. }
Methods of String class
java.lang.String class provides a lot of methods to work on string. Let's see the
commonly used methods of String class.
1. charAt() Returns the character located at the specified index
2. concat() Appends one String to the end of another ( "+" also works)
3. equalsIgnoreCase() Determines the equality of two Strings, ignoring case
4. length() Returns the number of characters in a String
5. replace() Replaces occurrences of a character with a new character
6. substring() Returns a part of a String
7. toLowerCase() Returns a String with uppercase characters converted
8. toString() Returns the value of a String
9. toUpperCase() Returns a String with lowercase characters converted
10.trim() Removes whitespace from the ends of a String
IMPORTANT METHODS IN THE STRING CLASS
charAt() method
class charAt
{
public static void main(String args[])
{
String s="Sachin";
System.out.println(s.charAt(5));
System.out.println(s.charAt(3));
}
}
toUpperCase() and toLowerCase() method
class low_upper
{
public static void main(String args[])
{
String s="Sachin";
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s);
}
}
JAVA IS EVERYWHERE

More Related Content

String in java

  • 1. IDEAL EYES BUSINESS COLLEGE A PRESENTATION STRING IN JAVA LANGUAGE 1 PERSENTED BY:- VIVEK KUMARPERSENTED TO:- PANKAJ SIR
  • 2. String  Generally string is a sequence of characters. But in java, string is an object.  String class is used to create string object.  string is a 16-bit Unicode character . There are two ways to create String object: 1. By string literal 2. By new keyword
  • 3. string literal For example:- String s1="Welcome"; String s2="Welcome"; //no new object will be created Note:- String objects are stored in a special memory area known as string constant pool inside the Heap memory. String literal is create by double quote. Each time you create a string literal, the JVM checks the string constant 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
  • 4. In the above example only one object will be created . First time JVM will find no string object with the name "Welcome" in string constant pool , so it will create a new object . Second time it will find the string with the name "Welcome" in string constant pool , so it will not create new object whether will return the reference to the same instance.
  • 5. By string literal class String _literal { public static void main(String args[]) { String x ="java"; String x1="java"; System.out.println( "x="+x); } }
  • 6. By new keyword In such case, JVM will create a new String object in normal ( nonpool ) Heap memory and the literal "Welcome" will be placed in the string constant pool . The variable s will refer to the object in Heap( nonpool ). String s=new String("Welcome"); //creates two objects and one reference variale For example:-
  • 7. By new keyword 1. class new_keyword 2. { 3. public static void main(String args[]) 4. { 5. String x ="java"; 6. String s=new String("Welcome"); 7. System.out.println( "x="+s); 8. System.out.println( "x="+x); 9. } 10. }
  • 8. Methods of String class java.lang.String class provides a lot of methods to work on string. Let's see the commonly used methods of String class. 1. charAt() Returns the character located at the specified index 2. concat() Appends one String to the end of another ( "+" also works) 3. equalsIgnoreCase() Determines the equality of two Strings, ignoring case 4. length() Returns the number of characters in a String 5. replace() Replaces occurrences of a character with a new character 6. substring() Returns a part of a String 7. toLowerCase() Returns a String with uppercase characters converted 8. toString() Returns the value of a String 9. toUpperCase() Returns a String with lowercase characters converted 10.trim() Removes whitespace from the ends of a String IMPORTANT METHODS IN THE STRING CLASS
  • 9. charAt() method class charAt { public static void main(String args[]) { String s="Sachin"; System.out.println(s.charAt(5)); System.out.println(s.charAt(3)); } }
  • 10. toUpperCase() and toLowerCase() method class low_upper { public static void main(String args[]) { String s="Sachin"; System.out.println(s.toUpperCase()); System.out.println(s.toLowerCase()); System.out.println(s); } }