SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
Java string , string buffer and wrapper class
Java String class provides a lot of methods to perform operations on string such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring()
etc.
String
The CharSequence interface is used to represent the sequence of characters. String,
StringBuffer and StringBuilder classes implement it. It means, we can create strings in
java by using these three classes.
public class StringExample{
public static void main(String args[]){
String s1="java";//creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}}
Output :
java strings example
Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer
class in java is same as String class except it is mutable i.e. it can be changed.
Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously.
So it is safe and will result in an order.
Java StringBuffer class
Constructor Description
StringBuffer() creates an empty string buffer with the initial capacity of
16.
StringBuffer(String str) creates a string buffer with the specified string.
StringBuffer(int capacity) creates an empty string buffer with the specified capacity
as length.
Important Constructors of StringBuffer class
StringBuffer examples
Some of the methods of stringbuffer class is as follows:
1. StringBuffer append() method
The append() method concatenates the given argument with this string.
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
2) StringBuffer insert() method
The insert() method inserts the given string with this string at the given position.
class StringBufferExample2{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
3) StringBuffer replace() method
The replace() method replaces the given string from the specified beginIndex and endIndex.
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
Conti.
4) StringBuffer delete() method
The delete() method deletes the string from the specified beginIndex to endIndex.
class StringBufferExample4{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
5) StringBuffer reverse() method
The reverse() method of StringBuffer class reverses the current string.
class StringBufferExample5{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}
}
Wrapper class in Java
Wrapper class in java provides the mechanism to convert primitive
into object and object into primitive.
The eight classes of java.lang package are known as wrapper classes
in java. The list of eight wrapper classes are given below:
Primitive type Wrapper class
boolean Boolean
int Integer
byte Byte
short Short
char Charcter
long Long
float Float
double Double
Wrapper class Example: Primitive to Wrapper
public class WrapperExample1{
public static void main(String args[]){
//convert primitive into object type
int a=20;
Integer i=Integer.valueOf(a); //converting int into Integer
System.out.println(“printing as primitive type "+a);
System.out.println(“printing as object: ” + i)
}}
Output:
20 20
Wrapper class Example: Primitive to Wrapper
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue(); //converting Integer to int
System.out.println(“printing as object "+a);
System.out.println(“printing as primitive type ” + i)
}}
Output:
3 3 3
Java string , string buffer and wrapper class

More Related Content

PDF
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
PPTX
Arrays in java
Arzath Areeff
 
PDF
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Kuntal Bhowmick
 
PPTX
Main method in java
Hitesh Kumar
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Method overloading
Lovely Professional University
 
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
Arrays in java
Arzath Areeff
 
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Kuntal Bhowmick
 
Main method in java
Hitesh Kumar
 
Java - Collections framework
Riccardo Cardin
 
9. Input Output in java
Nilesh Dalvi
 
Method overloading
Lovely Professional University
 

What's hot (20)

PPTX
Interface in java
PhD Research Scholar
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPT
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
PPTX
Inheritance in Java
Tamanna Akter
 
PDF
Collections in Java Notes
Shalabh Chaudhary
 
DOCX
Multiple choice questions for Java io,files and inheritance
Abishek Purushothaman
 
PPTX
presentation in html,css,javascript
FaysalAhammed5
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Java threading
Chinh Ngo Nguyen
 
PPTX
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PDF
Managing I/O in c++
Pranali Chaudhari
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Encapsulation C++
Hashim Hashim
 
PPTX
11. java methods
M H Buddhika Ariyaratne
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PDF
Learn Java with Dr. Rifat Shahriyar
Abir Mohammad
 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Interface in java
PhD Research Scholar
 
Inheritance In Java
Darpan Chelani
 
Java Server Pages(jsp)
Manisha Keim
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
Inheritance in Java
Tamanna Akter
 
Collections in Java Notes
Shalabh Chaudhary
 
Multiple choice questions for Java io,files and inheritance
Abishek Purushothaman
 
presentation in html,css,javascript
FaysalAhammed5
 
Methods in Java
Jussi Pohjolainen
 
Java threading
Chinh Ngo Nguyen
 
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
Inheritance C#
Raghuveer Guthikonda
 
Managing I/O in c++
Pranali Chaudhari
 
Inheritance
Sapna Sharma
 
Encapsulation C++
Hashim Hashim
 
11. java methods
M H Buddhika Ariyaratne
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Learn Java with Dr. Rifat Shahriyar
Abir Mohammad
 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Ad

Similar to Java string , string buffer and wrapper class (20)

PPS
String and string buffer
kamal kotecha
 
PPTX
L14 string handling(string buffer class)
teach4uin
 
PPTX
String in java, string constructors and operations
manjeshbngowda
 
PDF
3.7_StringBuilder.pdf
Ananthi68
 
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
PPTX
StringBuffer.pptx
meenakshi pareek
 
PPTX
Programing with java for begniers .pptx
adityaraj7711
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PDF
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
bhagyashri686896
 
PDF
Vision academy classes_bcs_bca_bba_java part_2
NayanTapare1
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PPT
07slide
Aboudi Sabbah
 
PPT
Strings.ppt
SanthiyaAK
 
PPTX
Java String Handling
Infoviaan Technologies
 
PPTX
Strings in Java
Abhilash Nair
 
PPT
Strings
naslin prestilda
 
PPTX
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
PPTX
package
sweetysweety8
 
String and string buffer
kamal kotecha
 
L14 string handling(string buffer class)
teach4uin
 
String in java, string constructors and operations
manjeshbngowda
 
3.7_StringBuilder.pdf
Ananthi68
 
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
StringBuffer.pptx
meenakshi pareek
 
Programing with java for begniers .pptx
adityaraj7711
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
bhagyashri686896
 
Vision academy classes_bcs_bca_bba_java part_2
NayanTapare1
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
Java string handling
GaneshKumarKanthiah
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
07slide
Aboudi Sabbah
 
Strings.ppt
SanthiyaAK
 
Java String Handling
Infoviaan Technologies
 
Strings in Java
Abhilash Nair
 
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
package
sweetysweety8
 
Ad

More from SimoniShah6 (6)

PPTX
Mergesort
SimoniShah6
 
PPT
JDBC Architecture and Drivers
SimoniShah6
 
PPTX
Modulation Techniques
SimoniShah6
 
PPTX
Top 10 Hadoop Shell Commands
SimoniShah6
 
PPT
Arrays and Pointers
SimoniShah6
 
PPT
Phyics - introduction of biomaterials
SimoniShah6
 
Mergesort
SimoniShah6
 
JDBC Architecture and Drivers
SimoniShah6
 
Modulation Techniques
SimoniShah6
 
Top 10 Hadoop Shell Commands
SimoniShah6
 
Arrays and Pointers
SimoniShah6
 
Phyics - introduction of biomaterials
SimoniShah6
 

Recently uploaded (20)

PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PPTX
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Zero Carbon Building Performance standard
BassemOsman1
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 

Java string , string buffer and wrapper class

  • 2. Java String class provides a lot of methods to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. String The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in java by using these three classes. public class StringExample{ public static void main(String args[]){ String s1="java";//creating string by java string literal char ch[]={'s','t','r','i','n','g','s'}; String s2=new String(ch);//converting char array to string String s3=new String("example");//creating java string by new keyword System.out.println(s1); System.out.println(s2); System.out.println(s3); }} Output : java strings example
  • 3. Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order. Java StringBuffer class Constructor Description StringBuffer() creates an empty string buffer with the initial capacity of 16. StringBuffer(String str) creates a string buffer with the specified string. StringBuffer(int capacity) creates an empty string buffer with the specified capacity as length. Important Constructors of StringBuffer class
  • 4. StringBuffer examples Some of the methods of stringbuffer class is as follows: 1. StringBuffer append() method The append() method concatenates the given argument with this string. class StringBufferExample{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } }
  • 5. 2) StringBuffer insert() method The insert() method inserts the given string with this string at the given position. class StringBufferExample2{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello "); sb.insert(1,"Java");//now original string is changed System.out.println(sb);//prints HJavaello } } 3) StringBuffer replace() method The replace() method replaces the given string from the specified beginIndex and endIndex. class StringBufferExample3{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.replace(1,3,"Java"); System.out.println(sb);//prints HJavalo } } Conti.
  • 6. 4) StringBuffer delete() method The delete() method deletes the string from the specified beginIndex to endIndex. class StringBufferExample4{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.delete(1,3); System.out.println(sb);//prints Hlo } } 5) StringBuffer reverse() method The reverse() method of StringBuffer class reverses the current string. class StringBufferExample5{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello"); sb.reverse(); System.out.println(sb);//prints olleH } }
  • 7. Wrapper class in Java Wrapper class in java provides the mechanism to convert primitive into object and object into primitive. The eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are given below: Primitive type Wrapper class boolean Boolean int Integer byte Byte short Short char Charcter long Long float Float double Double
  • 8. Wrapper class Example: Primitive to Wrapper public class WrapperExample1{ public static void main(String args[]){ //convert primitive into object type int a=20; Integer i=Integer.valueOf(a); //converting int into Integer System.out.println(“printing as primitive type "+a); System.out.println(“printing as object: ” + i) }} Output: 20 20
  • 9. Wrapper class Example: Primitive to Wrapper public class WrapperExample2{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(3); int i=a.intValue(); //converting Integer to int System.out.println(“printing as object "+a); System.out.println(“printing as primitive type ” + i) }} Output: 3 3 3