advertisement

String Handling

50 %
50 %
advertisement
Information about String Handling
Education

Published on February 23, 2014

Author: Manu___M

Source: slideshare.net

Description

String Handling,String Handling in Java
advertisement

Manu M,manumanjunatha.mm@gmail.com Hi Readers, Let’s have a cup of String, Sorry sorry…cup of coffee, with String. 0

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.

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

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.

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");

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

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".

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

Manu M,manumanjunatha.mm@gmail.com 8

Add a comment

Related presentations

Related pages

C string handling - Wikipedia, the free encyclopedia

Definitions. A string is a contiguous sequence of code units terminated by the first zero code (, corresponding to the null character). In C, there are ...
Read more

Java String - javatpoint - Javatpoint - A Solution of all ...

Java String class with methods such as concat, compareTo, split, join, ... What we will learn in String Handling ? Concept of String; Immutable String;
Read more

String Handling: - Cardiff School of Computer ...

String Handling: Recall from our discussion of arrays (Chapter 6) that strings are defined as an array of characters or a pointer to a portion ...
Read more

String handling - MATLAB strings - MathWorks – Makers of ...

MathWorks Machine Translation. The automated translation of this page is provided by a general purpose third party translator tool. MathWorks does not ...
Read more

String Handling - Yale Center for Medical Informatics

String Handling "String” is computer-ese for text. You can use strings in SQL, as well as in a program written in Access’s programming language ...
Read more

Globalization Step-by-Step: String Handling

Globalization Step-by-Step String Handling On This Page Overview and Description. Avoid Run-Time Composite Strings. When Variables Are Necessary, Use ...
Read more

String (computer science) - Wikipedia, the free encyclopedia

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow ...
Read more

String handling - CodeProject - CodeProject - For those ...

String handling - Free source code and tutorials for Software developers and Architects.; Updated: 31 May 2016
Read more

String Handling - Technische Universität Chemnitz

String Handling. WDM drivers can work with string data in any of four formats: A Unicode string, normally described by a UNICODE_STRING structure, contains ...
Read more

String handling - Free Pascal - Advanced open source ...

Name Description; BinStr: Construct binary representation of integer: Chr: Convert ASCII code to character: Concat: Concatenate two strings: Copy: Copy ...
Read more