SlideShare una empresa de Scribd logo
1 de 10
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

Más contenido relacionado

La actualidad más candente

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 

La actualidad más candente (20)

Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java exception
Java exception Java exception
Java exception
 
Java annotations
Java annotationsJava annotations
Java annotations
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Io streams
Io streamsIo streams
Io streams
 

Similar a Java string , string buffer and wrapper class

String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
adityaraj7711
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
Ravi_Kant_Sahu
 

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

String and string buffer
String and string bufferString and string buffer
String and string buffer
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
 
StringBuffer.pptx
StringBuffer.pptxStringBuffer.pptx
StringBuffer.pptx
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Vision academy classes_bcs_bca_bba_java part_2
Vision academy classes_bcs_bca_bba_java part_2Vision academy classes_bcs_bca_bba_java part_2
Vision academy classes_bcs_bca_bba_java part_2
 
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdfvision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
Java string handling
Java string handlingJava string handling
Java string handling
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
07slide
07slide07slide
07slide
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Strings
StringsStrings
Strings
 
package
packagepackage
package
 
11.ppt
11.ppt11.ppt
11.ppt
 
strings.ppt
strings.pptstrings.ppt
strings.ppt
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 

Más de SimoniShah6 (6)

Mergesort
MergesortMergesort
Mergesort
 
JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
 
Modulation Techniques
Modulation TechniquesModulation Techniques
Modulation Techniques
 
Top 10 Hadoop Shell Commands
Top 10 Hadoop Shell Commands Top 10 Hadoop Shell Commands
Top 10 Hadoop Shell Commands
 
Arrays and Pointers
Arrays and PointersArrays and Pointers
Arrays and Pointers
 
Phyics - introduction of biomaterials
Phyics - introduction of biomaterialsPhyics - introduction of biomaterials
Phyics - introduction of biomaterials
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

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
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 

Java string , string buffer and wrapper class

  • 1.
  • 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