SlideShare a Scribd company logo
1 of 28
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

Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programmingRiccardo Cardin
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)Anwar Hasan Shuvo
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysMartin Chapman
 

What's hot (20)

Generics in java
Generics in javaGenerics in java
Generics in java
 
07 java collection
07 java collection07 java collection
07 java collection
 
String in java
String in javaString in java
String in java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Java operators
Java operatorsJava operators
Java operators
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 

Similar to Java String

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string classfedcoordinator
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in javaGaruda Trainings
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesPrabu U
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings ConceptsVicter Paul
 
Java Presentation on the topic of string
Java Presentation  on the topic of stringJava Presentation  on the topic of string
Java Presentation on the topic of stringRajTangadi
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...Indu32
 
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.docxJava R20 - UNIT-5.docx
Java R20 - UNIT-5.docxPamarthi Kumar
 

Similar to Java String (20)

Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
String handling
String handlingString handling
String handling
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
package
packagepackage
package
 
Java Presentation on the topic of string
Java Presentation  on the topic of stringJava Presentation  on the topic of string
Java Presentation on the topic of string
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
Java R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdfJava R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdf
 
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.docxJava R20 - UNIT-5.docx
Java R20 - UNIT-5.docx
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 

Recently uploaded

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 

Recently uploaded (20)

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 

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