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

What's hot (20)

PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Strings in Java
Abhilash Nair
 
PPT
Arrays in JAVA.ppt
SeethaDinesh
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Data types in php
ilakkiya
 
PDF
Collections In Java
Binoj T E
 
PDF
Threads concept in java
Muthukumaran Subramanian
 
PDF
Java Collection framework
ankitgarg_er
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Java Data Types
Spotle.ai
 
PDF
Basic i/o & file handling in java
JayasankarPR2
 
PPSX
Java String class
DrRajeshreeKhande
 
PDF
Exception handling in plsql
Arun Sial
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Java Methods
OXUS 20
 
PPSX
python Function
Ronak Rathi
 
Java(Polymorphism)
harsh kothari
 
Super keyword in java
Hitesh Kumar
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Exception Handling in JAVA
SURIT DATTA
 
Arrays in Java
Naz Abdalla
 
Strings in Java
Abhilash Nair
 
Arrays in JAVA.ppt
SeethaDinesh
 
Java String
SATYAM SHRIVASTAV
 
Data types in php
ilakkiya
 
Collections In Java
Binoj T E
 
Threads concept in java
Muthukumaran Subramanian
 
Java Collection framework
ankitgarg_er
 
Constructor in java
Pavith Gunasekara
 
Java Data Types
Spotle.ai
 
Basic i/o & file handling in java
JayasankarPR2
 
Java String class
DrRajeshreeKhande
 
Exception handling in plsql
Arun Sial
 
String and string buffer
kamal kotecha
 
Java Methods
OXUS 20
 
python Function
Ronak Rathi
 

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

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
 
PPT
Strings
naslin prestilda
 
PPTX
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
PPTX
package
sweetysweety8
 
PPT
strings.ppt
BhumaNagaPavan
 
PPT
11.ppt
kavitamittal18
 
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
 
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
package
sweetysweety8
 
strings.ppt
BhumaNagaPavan
 
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
 
Ad

Recently uploaded (20)

PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPTX
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 

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