SlideShare a Scribd company logo
Wrapper Classes And String 
Satyam Shrivastav
Wrapper Classes
Primitive Data Type => Wrapper Class Object 
1.Using Constructor 
syntax : 
Integer i1 = new Integer(int); 
Integer i2 = new Integer(String); 
2.Using Method 
syntax : 
Integer i3 = Integer.valueOf(int);
Wrapper Class Object => Primitive Data Type 
1.Using method 
xxxValue() 
syntax : 
Integer io=Integer.valueOf(254) 
int i=io.xxxValue();
Primitive String Object => Wrapper Class Object 
1.Using Constructor 
syntax : 
Integer i1 = new Integer(“primitiveType”); 
2.using method 
syntax : 
Integer i1=Integer.valueOf(“primitiveType”);
Primitive String Object => Primitive Data Type 
//1.Primitive String Object =>Primitive String Object => 
Primitive Data Type 
//2Primitive String Object => Primitive Data Type 
Using method 
parseInt(“primitive type”); 
syntax : 
String s1=Integer.parseInt(“100”)
Wrapper Class Object => String Object 
1.using method 
toString() 
syntax : 
Integer io=new Integer(254); 
String s=io.toString()
Primitive Data Type => Non String Object 
1.using method 
toString(primitive type) 
syntax : 
String s=Integer.toString(10);
String
String 
 String is a sequence of characters placed in double quote(“ ”). 
 Strings are constant , their values cannot be changed in the 
same memory after they are created.
How to create String object 
 There are two ways to create String object: 
1)By string literal. 
2)By new keyword. 
 1)By string literal: 
For Example: String s1=“welcome"; 
String s2=“welcome”; //no new object will be created
2) By new keyword:- 
For Example: String s=new String(“Sachin"); 
String s=new String(“SachinTendulkar");//create two objects 
and one reference variable.
Immutability 
• In java, string objects are immutable. Immutable simply means 
unmodifiable or unchangeable. 
• Once string object is created its data or state can't be changed 
but a new string object is created.
Advantages Of Immutability 
Use less memory: 
String word1 = "Java"; 
String word2 = word1; 
String word1 = “Java"; 
String word2 = new String(word1); 
word1 
OK 
Less efficient: 
wastes memory 
Java 
Java 
Java 
word2 
word1 
word2
Disadvantages of Immutability 
 Less efficient — you need to create a new string and throw 
away the old one even for small changes. 
Example: 
String word = “Java"; 
word= word.concat(“technologies”); 
word Java 
Java 
technologies
Constructors 
 No Argument Constructors: 
No-argument constructor creates an empty String. Rarely used. 
Example: String empty=new String(); 
 Copy Constructors: 
Copy constructor creates a copy of an existing String . Also rarely used. 
Example: String word = new String(“Java”); 
String word2 = new String(word); 
 Other Constructors: 
Most other constructors take an array as a parameter to create a String. 
Example: char[] letters = {‘J’, ‘a’, ‘v’, ‘a’}; 
String word = new String(letters);//”Java”
String Methods 
 substring(int begin): 
 Returns substring from begin index to end of the String. 
 Example: String s=“nacre”; 
System.out.println(s.substring(2));//cre 
 equals(): 
 To perform content comparision where case is important. 
Example: String s=“java”; 
System.out.println(s.equals(“java”));//true 
 concat(): Adding two strings we use this method 
Example: String s=“nacre”; 
s= s.concat(“software”); 
System.out.println(s);// nacresoftware
length() , charAt() 
int length(); 
char charAt(i); 
 Returns the number of characters in the 
string 
 Returns the char at position i. 
Character positions in strings are numbered starting from 
0 – just like arrays. 
Returns: 
“Problem".length(); 7 
“Window”. charAt (2); ’n'
New features of String 
 In 1.5 Version: 
 Wrapper classes are introduced. 
Example. 
String s = Integer.toString(10); 
System.out.println(s);//10 
 In 1.7 Version: 
 Java 7 extended the capability of switch case to use Strings also, 
earlier java versions doesn’t support this. 
If you are implementing conditional flow for Strings, you can use 
if-else conditions and you can use switch case if you are using 
Java 7 or higher versions.
StringBuffer, StringBuilder 
StringTokenizer
 Limitation of String in Java ? 
 What is mutable string? 
A string that can be modified or changed is known as mutable 
string. StringBuffer and StringBuilder classes are used for creating 
mutable string. 
 Differences between String and StringBuffer in 
java? 
Main difference between String and StringBuffer is String is 
immutable while StringBuffer is mutable
Relations between StringBuffer, StringBuilder and 
StringToknizer: 
All These classes are final and subclass of Serializable.
StringBuffer 
 StringBuffer is a synchronized and allows us to mutate the 
string. 
 StringBuffer has many utility methods to manipulate the string. 
 This is more useful when using in a multithreaded 
environment. 
 Always modified in same memory location.
 StringBuffer Constructor: 
 1. public StringBuffer() 
 2. public StringBuffer(int capacity) 
 3. public StringBuffer(String s) 
 4. public StringBuffer(CharSequence cs)
Method Use In StringBuffer 
 1. Append 
 2. Insert 
 3.Delete 
 4.Reverse 
 5.Replacing Character at given index
StringBuilder 
 StringBuilder is the same as the StringBuffer class 
 The StringBuilder class is not synchronized and hence in a 
single threaded environment, the overhead is less than using a 
StringBuffer.
StringTokenizer 
 A token is a portion of a string that is separated from another 
portion of that string by one or more chosen characters (called 
delimiters). 
 The StringTokenizer class contained in the java.util package 
can be used to break a string into separate tokens. This is 
particularly useful in those situations in which we want to read 
and process one token at a time; the BufferedReader class does 
not have a method to read one token at a time
Thank you…

More Related Content

What's hot (20)

PPTX
Operators in java
Then Murugeshwari
 
PPS
Wrapper class
kamal kotecha
 
PPT
Strings
naslin prestilda
 
PPT
String Handling
Bharat17485
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
PDF
ITFT-Constants, variables and data types in java
Atul Sehdev
 
PPTX
Variables in C and C++ Language
Way2itech
 
PPTX
Operators in java
AbhishekMondal42
 
PPT
Java And Multithreading
Shraddha
 
PPT
Python List.ppt
T PRIYA
 
PPTX
L21 io streams
teach4uin
 
PPTX
I/O Streams
Ravi Chythanya
 
PPTX
Queues
Ashim Lamichhane
 
PPTX
String Builder & String Buffer (Java Programming)
Anwar Hasan Shuvo
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPT
Stacks
Malainine Zaid
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PPT
C++ Pointers And References
verisan
 
PPTX
Arrays in java
bhavesh prakash
 
Operators in java
Then Murugeshwari
 
Wrapper class
kamal kotecha
 
String Handling
Bharat17485
 
9. Input Output in java
Nilesh Dalvi
 
Data Types & Variables in JAVA
Ankita Totala
 
ITFT-Constants, variables and data types in java
Atul Sehdev
 
Variables in C and C++ Language
Way2itech
 
Operators in java
AbhishekMondal42
 
Java And Multithreading
Shraddha
 
Python List.ppt
T PRIYA
 
L21 io streams
teach4uin
 
I/O Streams
Ravi Chythanya
 
String Builder & String Buffer (Java Programming)
Anwar Hasan Shuvo
 
Dynamic method dispatch
yugandhar vadlamudi
 
MULTI THREADING IN JAVA
VINOTH R
 
C++ Pointers And References
verisan
 
Arrays in java
bhavesh prakash
 

Similar to Java String (20)

PPTX
Day_5.1.pptx
ishasharma835109
 
PPT
JAVA CONCEPTS
Shivam Singh
 
PPTX
javastringexample problems using string class
fedcoordinator
 
PPTX
Java Unit 2(Part 1)
Dr. SURBHI SAROHA
 
PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPTX
String handling
ssuser20c32b
 
PPTX
Java String Handling
Infoviaan Technologies
 
PPTX
DOC-20240812-WA0000 array string and.pptx
PanjatcharamVg
 
DOCX
Fundamental classes in java
Garuda Trainings
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PDF
Lecture 7
Debasish Pratihari
 
PDF
Java - Strings Concepts
Victer Paul
 
PPTX
package
sweetysweety8
 
PDF
Java Presentation on the topic of string
RajTangadi
 
PPT
String classes and its methods.20
myrajendra
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
DOCX
Autoboxing and unboxing
Geetha Manohar
 
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
PDF
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
Day_5.1.pptx
ishasharma835109
 
JAVA CONCEPTS
Shivam Singh
 
javastringexample problems using string class
fedcoordinator
 
Java Unit 2(Part 1)
Dr. SURBHI SAROHA
 
Java Strings methods and operations.ppt
JyothiAmpally
 
String handling
ssuser20c32b
 
Java String Handling
Infoviaan Technologies
 
DOC-20240812-WA0000 array string and.pptx
PanjatcharamVg
 
Fundamental classes in java
Garuda Trainings
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Java - Strings Concepts
Victer Paul
 
package
sweetysweety8
 
Java Presentation on the topic of string
RajTangadi
 
String classes and its methods.20
myrajendra
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Autoboxing and unboxing
Geetha Manohar
 
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
Ad

Recently uploaded (20)

PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Zero Carbon Building Performance standard
BassemOsman1
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
Ad

Java String

  • 1. Wrapper Classes And String Satyam Shrivastav
  • 3. Primitive Data Type => Wrapper Class Object 1.Using Constructor syntax : Integer i1 = new Integer(int); Integer i2 = new Integer(String); 2.Using Method syntax : Integer i3 = Integer.valueOf(int);
  • 4. Wrapper Class Object => Primitive Data Type 1.Using method xxxValue() syntax : Integer io=Integer.valueOf(254) int i=io.xxxValue();
  • 5. Primitive String Object => Wrapper Class Object 1.Using Constructor syntax : Integer i1 = new Integer(“primitiveType”); 2.using method syntax : Integer i1=Integer.valueOf(“primitiveType”);
  • 6. Primitive String Object => Primitive Data Type //1.Primitive String Object =>Primitive String Object => Primitive Data Type //2Primitive String Object => Primitive Data Type Using method parseInt(“primitive type”); syntax : String s1=Integer.parseInt(“100”)
  • 7. Wrapper Class Object => String Object 1.using method toString() syntax : Integer io=new Integer(254); String s=io.toString()
  • 8. Primitive Data Type => Non String Object 1.using method toString(primitive type) syntax : String s=Integer.toString(10);
  • 10. String  String is a sequence of characters placed in double quote(“ ”).  Strings are constant , their values cannot be changed in the same memory after they are created.
  • 11. How to create String object  There are two ways to create String object: 1)By string literal. 2)By new keyword.  1)By string literal: For Example: String s1=“welcome"; String s2=“welcome”; //no new object will be created
  • 12. 2) By new keyword:- For Example: String s=new String(“Sachin"); String s=new String(“SachinTendulkar");//create two objects and one reference variable.
  • 13. Immutability • In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. • Once string object is created its data or state can't be changed but a new string object is created.
  • 14. Advantages Of Immutability Use less memory: String word1 = "Java"; String word2 = word1; String word1 = “Java"; String word2 = new String(word1); word1 OK Less efficient: wastes memory Java Java Java word2 word1 word2
  • 15. Disadvantages of Immutability  Less efficient — you need to create a new string and throw away the old one even for small changes. Example: String word = “Java"; word= word.concat(“technologies”); word Java Java technologies
  • 16. Constructors  No Argument Constructors: No-argument constructor creates an empty String. Rarely used. Example: String empty=new String();  Copy Constructors: Copy constructor creates a copy of an existing String . Also rarely used. Example: String word = new String(“Java”); String word2 = new String(word);  Other Constructors: Most other constructors take an array as a parameter to create a String. Example: char[] letters = {‘J’, ‘a’, ‘v’, ‘a’}; String word = new String(letters);//”Java”
  • 17. String Methods  substring(int begin):  Returns substring from begin index to end of the String.  Example: String s=“nacre”; System.out.println(s.substring(2));//cre  equals():  To perform content comparision where case is important. Example: String s=“java”; System.out.println(s.equals(“java”));//true  concat(): Adding two strings we use this method Example: String s=“nacre”; s= s.concat(“software”); System.out.println(s);// nacresoftware
  • 18. length() , charAt() int length(); char charAt(i);  Returns the number of characters in the string  Returns the char at position i. Character positions in strings are numbered starting from 0 – just like arrays. Returns: “Problem".length(); 7 “Window”. charAt (2); ’n'
  • 19. New features of String  In 1.5 Version:  Wrapper classes are introduced. Example. String s = Integer.toString(10); System.out.println(s);//10  In 1.7 Version:  Java 7 extended the capability of switch case to use Strings also, earlier java versions doesn’t support this. If you are implementing conditional flow for Strings, you can use if-else conditions and you can use switch case if you are using Java 7 or higher versions.
  • 21.  Limitation of String in Java ?  What is mutable string? A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.  Differences between String and StringBuffer in java? Main difference between String and StringBuffer is String is immutable while StringBuffer is mutable
  • 22. Relations between StringBuffer, StringBuilder and StringToknizer: All These classes are final and subclass of Serializable.
  • 23. StringBuffer  StringBuffer is a synchronized and allows us to mutate the string.  StringBuffer has many utility methods to manipulate the string.  This is more useful when using in a multithreaded environment.  Always modified in same memory location.
  • 24.  StringBuffer Constructor:  1. public StringBuffer()  2. public StringBuffer(int capacity)  3. public StringBuffer(String s)  4. public StringBuffer(CharSequence cs)
  • 25. Method Use In StringBuffer  1. Append  2. Insert  3.Delete  4.Reverse  5.Replacing Character at given index
  • 26. StringBuilder  StringBuilder is the same as the StringBuffer class  The StringBuilder class is not synchronized and hence in a single threaded environment, the overhead is less than using a StringBuffer.
  • 27. StringTokenizer  A token is a portion of a string that is separated from another portion of that string by one or more chosen characters (called delimiters).  The StringTokenizer class contained in the java.util package can be used to break a string into separate tokens. This is particularly useful in those situations in which we want to read and process one token at a time; the BufferedReader class does not have a method to read one token at a time