Your SlideShare is downloading. ×
String Handling
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

String Handling

265
views

Published on

String Handling,String Handling in Java

String Handling,String Handling in Java

Published in: Education

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
265
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Manu M,manumanjunatha.mm@gmail.com Hi Readers, Let’s have a cup of String, Sorry sorry…cup of coffee, with String. 0
  • 2. Manu M,manumanjunatha.mm@gmail.com 1 String Handling Creating String Object There are two ways to create String object: 1. By string literal 2. By new keyword By string literal By new keyword String literal is created by double quote 1. String literal is created by using new key word String s="Hello"; String s=new String("Welcome"); Each time you create a string literal, the JVM JVM will create a new String object in normal checks the string constant pool first. If the string Heap memory. .The variable will refer to the already exists in the pool, a reference to the object in Heap. pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. 2. Applicable only for String class. (Not applicable for,StringBuilder, StringBuffer classes.) Applicable for String ,StringBuilder, StringBuffer classes. /** * @author AdiTemp * */ package com.aditemp.adi; public class StringHandling { public static void main(String[] args) { String s1 = "Adi"; String s2 = "Adi String s3=new String("Adi"); StringBuilder s4=new StringBuilder("Adi"); } } String s1 = "Adi"; String s2 = "Adi String s3=new String("Adi"); StringBuilder s4=new StringBuilder("Adi"); New String Constant created in string constant pool, s1 is returned to this . No new object is created, s2 is returned with the same above string present in string constant pool. JVM will create a new String object in Heap memory and the literal "Adi" will be placed in the string constant pool. The variable s3 will refer to the object in Heap. JVM will create a new String object in Heap memory and the literal "Adi" will be placed in the string constant pool. The variable s4 will refer to the object in Heap.
  • 3. Manu M,manumanjunatha.mm@gmail.com 2 Mutability concept   String class is immutable. StringBuffer and StringBuilder classes are mutable (modifiable) strings. Let's try to understand the immutability concept by the example given below: /** * @author Manu_M * */ public class StringHandling { public static void main(String[] args) { String s1=new String("Adi"); String s2=s1.concat("Temp"); String is immutable, hence s1 is unaltered System.out.println("s1 is "+s1); System.out.println("s2 is "+s2); StringBuilder s3=new StringBuilder("Adi"); StringBuilder is StringBuilder s4=s3.append("Temp"); mutable , hence s3 is System.out.println("s3 is "+s3); altered System.out.println("s4 is "+s4); } } Output: s1 s2 s3 s4 is is is is Adi AdiTemp AdiTemp AdiTemp
  • 4. Manu M,manumanjunatha.mm@gmail.com 3 String Comparison There are two different ways to compare objects in Java. The == operator compares whether two references point to the same object. The equals() method compares whether two objects contain the same data. One of the first lessons you learn in Java is that you should usually use equals(), not ==, to compare two strings. There are three ways to compare String objects: 1. By equals() method 2. By = = operator 3. By compareTo() method used in authentication used in sorting used in reference matching 1) By equals() method  This equals() method overrides equals() in class Object  equals() method compares the original content of the string present in string constant pool, whatever may be the objects and references are ignored. It compares values of string for equality. 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. 2) By == operator The = = operator compares references not values.
  • 5. Manu M,manumanjunatha.mm@gmail.com 4 3) compareTo() method: compareTo() method compares values and returns an int which tells if the values compare less than, equal, or greater than. The comparison is based on the Unicode value of each character in the strings Suppose s1 and s2 are two string variables.If:  s1 == s2 :0  s1 > s2 :positive value  s1 < s2 :negative value class StringHandling{ public static void main(String args[]){ String s1="AdiTemp"; String s2="AdiTemp"; String s3=new String("AdiTemp"); String s4="Manu M"; String s5="aditemp"; System.out.println(s1.equals(s2));//true System.out.println(s1.equals(s3));//true System.out.println(s1.equals(s4));//false System.out.println(s1.equalsIgnoreCase(s5));//true because case is ignored System.out.println(s1==s2);//true,because both refer to same instance System.out.println(s1==s3);//false,because s3 refers to instance created in nonpool System.out.println(s1.compareTo(s2));//0 System.out.println(s1.compareTo(s4));//-12,because s1<s4 System.out.println(s4.compareTo(s1));//12(because s4>s1 ) } } Output: true true false true true false 0 -12 12 String Concatenation in Java String StringBuilder, StringBuffer By + (string concatenation) operator By concat() method By append() method There are various overloaded append() methods present in StringBuilder, StringBuffer classes .Refer Java API. We can concatenate only matching types. We cannot concatenate String and StringBuilder/StringBuffer We cannot concatenate StringBuilder and StringBuffer class StringHandling{ public static void main(String args[]){ String s1="Adi"; String s2="Temp"; StringBuilder s3 =new StringBuilder("Adi");
  • 6. Manu M,manumanjunatha.mm@gmail.com 5 String s5="Adi"+"Temp"+100+50; System.out.println(s5);//AdiTemp10050 String s6=s1.concat(s2); System.out.println(s6);//AdiTemp A variable or direct String constant StringBuilder s7=s3.append("Temp"); System.out.println(s7);//AdiTemp } } OutPut: AdiTemp10050 AdiTemp AdiTemp Substring in Java A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex starts from 0 and endIndex starts from 1 or startIndex is inclusive and endIndex is exclusive. You can get substring from the given String object by one of the two methods: 1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive). 2. public String substring(int startIndex,int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex. In case of string:  startIndex:starts from index 0(inclusive).  endIndex:starts from index 1(exclusive). class StringHandling{ public static void main(String args[]){ String s="AdiTemp"; System.out.println(s.substring(3));// System.out.println(s.substring(0,0));// System.out.println(s.substring(0,1));// System.out.println(s.substring(0,3));// } } Output: Temp A Adi
  • 7. Manu M,manumanjunatha.mm@gmail.com 6 toString() method    If you want to represent any object as a string, toString() method comes into existence. The toString() method returns the string representation of the object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation. Consider a class Demo Demo obj= new Demo(); System.out.println(obj); Same Demo obj= new Demo(); System.out.println(obj.toString()); class A{ void meth(){ System.out.println(" class A"); } } class StringHandling{ public static void main(String args[]){ A a=new A(); System.out.println(a.toString()); StringBuffer s=new StringBuffer("AdiTemp"); String s1=s.toString(); System.out.println(s1); } } Output: A@6e818805 AdiTemp intern()    Intern() method is present in String class, but not in StringBuilder and StringBuffer.If you want to use intern() method for StringBuilder and StringBuffer , then you have to convert StringBuilder and StringBuffer to String. toString() method present in Object class returns a string representation of the object, can be used to convert StringBuilder and StringBuffer objects to String object. return type of intern() is String. It always returns any one of the String from string constant pool. String s1 = new String("Hello Java"); String s2= s1.intern(); Here JVM checks whether "Hello Java" is present in string constant pool. If present then s2 is returned to existing one, otherwise new string constant "Hello Java" is created in string constant pool and s2 is returned to this new string constant "Hello Java".
  • 8. Manu M,manumanjunatha.mm@gmail.com 7 Here “AdiTemp” is not present in string constant pool, So JVM creates new string constant “AdiTemp” in string constant pool. S3 is returned to this new string constant. Here “AdiTemp” is present in string constant pool, So JVM will not creates new string constant “AdiTemp” in string constant pool. S3 is returned to existing string constant. public class Demo { public static void main(String[] args) { public class Demo { public static void main(String[] args) { String str1 = "AdiTemp"; String str2 = new String("AdiTemp "); String str3 = str2.intern(); System.out.println((str1 == str2)); System.out.println((str1 == str3)); String str1 = "Adi"; String str2 = new String("AdiTemp"); String str3 = str2.intern(); System.out.println((str1 == str2)); System.out.println((str1 == str3)); } } Output: false false } } Output: false true
  • 9. Manu M,manumanjunatha.mm@gmail.com 8