String

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

String

String
 Strings, are a sequence of characters.

 In Java programming language, strings are treated as objects.

 The java.lang.String class is used to create a string object

How to create a string object?


There are two ways to create String object:
 By string literal

 By new keyword

 By char to string
By string literal
 By declaring a string variable and assigning a group of

characters to the string variable.

 Java String is created by using double quotes.

Syntax:

String <variable_name>=value;

Ex:

String s="welcome";
Ex:1
public class String1
{
public static void main(String[] args) {
String s="Welcome java";
System.out.println(s);
}
}
By new keyword

 Using the new operator, create a String object by passing the

string value as an argument to the String.

 Java String is created by using new keyword.

Syntax:

String <object> = new String(<value>);

Ex:

String s=new String("welcome to java“);


Ex:2

public class String1 {

public static void main(String[] args)

String s= new String("Welcome java");

System.out.println(s);

}
By char to string
 Converting the character array into strings.

Syntax:

String <object> = new String(<Char_Array>);

Ex:

char ch[]={'s','t','r','i','n','g','s'};

String name = new String(ch);


Ex:3
public class StringExample
{
public static void main(String args[])
{
char ch[]={'s','t','r','i','n','g','s'};
String s1=new String(ch);//converting char array to string
System.out.println(s1);
}
}
Strings Are Immutable
 Strings are immutabe which means, their value cannot be
changed once they are created.

Is String A Class Or A Datatype?


 String is a class defined in java.lang package.

 In Java, every class is considered as user defined data type.

 So we can take string as a data type.


String are immutable example
classTestimmutablestring

public static void main(String args[])

String s="Sachin";

s.concat("Tendulkar");

System.out.println(s);

}
Java String Buffer class
 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.


Creating Stringbuffer Objects
 String Buffer can be defined simply by the use of new operator

and by passing the string value inside the String Buffer.

Syntax:

 StringBuffer <object> = new StringBuffer(String _ value);

Ex:

 StringBuffer sb = new StringBuffer(“Hello”);


Ex:
public class StringBuf
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("Hello ");
System.out.println(sb);
}
}
Java StringBuilder class
 java StringBuilder class is used to create mutable

(modifiable) string.
 The Java StringBuilder class is same as StringBuffer

class except that it is non-synchronized.


 It is available since JDK 1.5.
Creating Stringbuilder Objects
 StringBuilder can be defined simply by the use of new operator
and by passing the string value inside the StringBuilder
constructor.
Syntax:
 StringBuilder <object> = new StringBuilder(String_value);

Ex:
StringBuilder sb = new StringBuilder(“Hello”);
Ex:
public class StringBuild
{
public static void main(String[] args)
{
StringBuilder sb=new StringBuilder("Hello ");
System.out.println(sb);
}
}
Difference between String and StringBuffer
Difference between StringBuffer and
StringBuilder

You might also like