The document discusses Java strings and wrapper classes. It provides examples of creating and manipulating strings using the String, StringBuffer, and StringBuilder classes. It also discusses the eight primitive wrapper classes in Java and examples converting between primitive types and their corresponding wrapper classes.
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