0% found this document useful (0 votes)
8 views7 pages

String

The document provides an overview of the String class in Java, detailing its characteristics, methods, and types. It distinguishes between literal (immutable) and non-literal (mutable) strings, explaining their memory allocation and behavior. Additionally, it includes code examples demonstrating various string operations and their outcomes.

Uploaded by

itscynthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

String

The document provides an overview of the String class in Java, detailing its characteristics, methods, and types. It distinguishes between literal (immutable) and non-literal (mutable) strings, explaining their memory allocation and behavior. Additionally, it includes code examples demonstrating various string operations and their outcomes.

Uploaded by

itscynthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

String

 String is a predefined class and it is present in java lang package.


 It stores its value based on index.
 The index starts from 0 and ends in n-1
 Its length starts from 1 and ends in n

Syntax:
String <variable name>= “ “

String Methods
As string is a class it has its own methods used for different purpose. They are

s.no Method name Method Purpose Return type


1 String Length length() To find the length of the Integer
string
2 Find Character charAt() To find particular character Character
3 Check Empty isEmpty() To check given String is Boolean
empty or not
4 Index position indexOf() To find the index value of a Integer
character
5 Last index lastIndexOf() To find the last index value Integer
position of a character
6 Upper case uppercase() To convert all the String
characters to uppercase
7 Lower case lowercase() To convert all the String
characters to lowercase
8 Starting startsWith() To check a string starts Boolean
Character with particular characters
9 Ending endsWith() To check a string ends with Boolean
Character particular characters
10 Word check contains() To check particular word is Boolean
present in the string
11 Replace replace() To replace a particular String
character character in a string
12 Replace String replaceAll() To replace a whole string String
13 Trimming a trim() To remove unwanted space String
string in a string
14 Comparing equals() To check given two strings Boolean
are equal. It is case
sensitive
15 Comparing equalsIgnoreCa To check given two strings String
se() are equal. It is not case
sensitive
16 Joining concat() To join two Strings String

17 Sub String subString(idx To get a new string from String


value) the existing string
18 Sub String Substring(stridx To get a new string from String
, endidx) the existing string using
index values
Types of String:
There are two type of String available in Java. They are

Literal String or Immutable String (unchangeable) (String)

Non Literal String or Mutable String (changeable) (String Buffer/String


Builder)

s.no Literal String Non Literal String


1 Literal String stores data Non Literal string stores data in the heap
inside the heap memory. memory(outer surface of the heap
memory)
2 Declaration/Syntax: Declaration/Syntax:
String s= “java” String s= new String(“java”);
3 In case of duplicate literal In case of duplicate non literal string
string stores it in the same stores in different memory location
memory location

Immutable String:
A string which cannot be changed is known as immutable string. It acts like a
literal string. To join two immutable strings we use concat () method. The input
strings and the concated string shares different memory location.

Mutable String:
A string that is declared using String Buffer or string Builder is known as
mutable string. It acts like non literal string. To join two mutable strings append
() method is used. The first input string and the concated string share the same
memory location.

The String Buffer is a synchronous and thread safe where StringBuilder is


Asynchronous and non-thread safe.

String methods:
import java.util.*;
class strngmths
{
public static void main(String args[])
{
String s= "Java Program";
String s1="";
String s2= "PythonProgram";
String s3="java program";
String s4="Hi Welcome to The Class";
int a= s.length();
System.out.println("Length of the string="+a);
char b= s.charAt(2);
System.out.println("Character at index 2="+b);
boolean c= s1.isEmpty();
System.out.println(c);
boolean d= s.isEmpty();
System.out.println(d);
int e= s.indexOf(a);
System.out.println("Index value of character a is:"+e);
int f= s.lastIndexOf(a);
System.out.println("Last Index value of character a is:"+f);
String g= s.toUpperCase();
System.out.println(g);
String h= s.toLowerCase();
System.out.println(h);
boolean i= s.startsWith("ja");
System.out.println(i);
boolean j= s.endsWith("am");
System.out.println(j);
boolean k= s.contains("gram");
System.out.println(k);
String l= s.replace('a','#');
System.out.println(l);
String m= s2.replaceAll(s2,"Sql Query");
System.out.println(m);
String n= s3.trim();
System.out.println(s3);
System.out.println(n);
boolean o= s.equals(s3);
System.out.println(o);
boolean p= s.equalsIgnoreCase(s3);
System.out.println(p);
String q= s2.concat(s3);
System.out.println(q);
String r= s4.substring(4);
System.out.println(r);
String t= s4.substring(7,20);
System.out.println(t);
}
}
Literal and non literal Strings:
class StringBasic
{
public static void main(String[] args)
{
String s1 = "vengat";
String s2 = "vengat
System.out.println(System.identityHashCode(s1));
System.out.println(System.identityHashCode(s2));
String x1=new String("vengat");
String x2=new String("vengat”);
System.out.println(System.identityHashCode(x1));
System.out.println(System.identityHashCode(x2));
}
}
Output:
31168322 // literal string share the memory if same value
31168322
17225372
5433634 // but non literal won't share

 identityHashcode() is used to print the reference value(storage reference).

2.
class StringBasic
{
public static void main(String[] args)
{
String s1 = "vengat";
String s2 = "prabu";
System.out.println("Immutable string");
System.out.println(System.identityHashCode(s1));
System.out.println(System.identityHashCode(s2));
String r = s1.concat(s2);
System.out.println(r);
System.out.println(System.identityHashCode(r));
StringBuffer x1=new StringBuffer("vengat");
StringBuffer x2=new StringBuffer("prabu");
System.out.println("mutable string");
System.out.println(System.identityHashCode(x1));
System.out.println(System.identityHashCode(x2));
x1.append(x2);
System.out.println(x1);
System.out.println(System.identityHashCode(x1));
}
}

Output:
Immutable string
31168322
17225372
vengatprabu
5433634 // here it takes new memory for concordinattion
mutable string
2430287
17689166
vengatprabu
2430287 // but here it takes x1 memory

You might also like