0% found this document useful (0 votes)
2 views

String

The document provides an overview of Strings in Java, explaining that they are immutable objects created using the java.lang.String class. It details methods for creating strings, comparing them, and manipulating their content, including length, character access, concatenation, and case conversion. Additionally, it introduces mutable strings using StringBuffer and StringBuilder, along with the Scanner class for input handling.

Uploaded by

applepro1682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

String

The document provides an overview of Strings in Java, explaining that they are immutable objects created using the java.lang.String class. It details methods for creating strings, comparing them, and manipulating their content, including length, character access, concatenation, and case conversion. Additionally, it introduces mutable strings using StringBuffer and StringBuilder, along with the Scanner class for input handling.

Uploaded by

applepro1682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

What is String in Java?

• String is a sequence of characters.


• But in Java, string is an object that represents a sequence of characters.
• The java.lang.String class is used to create a string object.
• Strings are immutable class in java. [Immutable means we can’t changed]

How to create a string object?

There are two ways to create String object:


1. By string literal
2. By new keyword
String Literal

• Java String literal is created by using double quotes.


String s=“Edso Services";
• Each time you create a string literal, the JVM checks the "string constant pool"
first.
• If the string already exists in the pool, a reference to the pooled instance is
returned.
• If the string doesn't exist in the pool, a new string instance is created and placed in
the pool.
• String s1=“Edso Services";
• String s2=" Edso Services ";//It doesn't create a new instance
String Constant Pool
By new keyword

• String s=new String(“Edso Services");


• In such case, JVM will create a new string object in normal (non-pool) heap
memory, and the literal " Edso Services " will be placed in the string constant
pool.
• The variables will refer to the object in a heap (non-pool).
Q. What is difference between == operator & equal() method.

• Ans. == operator for refences comparison (Address Comparison) & .equals()


method for contain comparison.

• String s1 = new String(“Ganesh”);


• String s2 = new String(“Ganesh”);
• sop(s1 == s2) -> false, sop(s1.equals(s2)); -> true
Java String length()

• The Java String class length() method finds the length of a string.
• The length of the Java string is the same as the Unicode code units of the string.

Signature
• public int length()

Returns
• Length of characters. In other words, the total number of characters present in the
string.
Java String charAt()

• The Java String class charAt() method returns a char value at the given index number.
• The index number starts from 0 and goes to n-1, where n is the length of the string.
• It returns StringIndexOutOfBoundsException, if the given index number is greater
than or equal to this string length or a negative number.

Syntax
• public char charAt(int index)
Java String compareTo()

• The Java String class compareTo() method compares the given string with the
current string. It returns a positive number, negative number, or 0.
• It compares strings based on the Unicode value of each character in the strings.
• It returns a negative number, and if the first string is lexicographically equal to the
second string, it returns 0.
• if s1 > s2, it returns positive number
• if s1 < s2, it returns negative number
• if s1 == s2, it returns 0
Syntax
• public int compareTo(String another String)
Java String concat

• The Java String class concat() method combines specified string at the end of this
string.
• It returns a combined string.
• It is like appending another string.

Signature
• public String concat(String another String)
Java String split()

• The java string split() method splits this string against given regular expression
and returns a char array.

Signature
• There are two signature for split() method in java string.
1. public String split(String regex)
2. public String split(String regex, int limit)
Java String toUpperCase()

• The java string toUpperCase() method returns the string in uppercase letter.
• In other words, it converts all characters of the string into upper case letter.
• public String toUpperCase()

Java String toLowerCase()


• The java string toLowerCase() method returns the string in lowercase letter.
• In other words, it converts all characters of the string into lower case letter.
• public String toLowerCase()
Java String trim()

• The Java String class trim() method eliminates leading and trailing spaces.
• public String trim()

Returns
• string with omitted leading and trailing spaces
What is a mutable String?

• A String that can be modified or changed is known as mutable String.


• StringBuffer and StringBuilder classes are used for creating mutable strings.
StringBuffer class in Java

• StringBuffer is a peer class of String that provides much of the functionality of


strings.
• The string represents fixed-length, immutable character sequences while
StringBuffer represents growable and writable character sequences.
• StringBuffer may have characters and substrings inserted in the middle or
appended to the end.
• It will automatically grow to make room for such additions and often has more
characters preallocated than are needed, to allow room for growth.
1. StringBuffer()
• It reserves room for 16 characters without reallocation.
• StringBuffer s = new StringBuffer();

2. StringBuffer(String str):
• It accepts a string argument that sets the initial contents of the StringBuffer object
and reserves room for 16 more characters without reallocation.
• StringBuffer s = new StringBuffer(“EdsoServices");
Modifier and Type Method Description

public synchronized append(String s) It is used to append the specified string with


StringBuffer this string.
public synchronized insert(int offset, String s) It is used to insert the specified string with this
StringBuffer string at the specified position.
public synchronized replace(int startIndex, int It is used to replace the string from specified
StringBuffer endIndex, String str) startIndex and endIndex.

Public synchronized delete(int startIndex, int It is used to delete the string from specified
StringBuffer endIndex) startIndex and endIndex.

public synchronized reverse() is used to reverse the string.


StringBuffer
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.
Modifier and Type Method Description

public ansyncronise append(String s) It is used to append the specified string with


StringBuilder this string.
public ansyncronise insert(int offset, String s) It is used to insert the specified string with this
StringBuilder string at the specified position.
public ansyncronise replace(int startIndex, int It is used to replace the string from specified
StringBuilder endIndex, String str) startIndex and endIndex.

Public ansyncronise delete(int startIndex, int It is used to delete the string from specified
StringBuilder endIndex) startIndex and endIndex.

public ansyncronise reverse() is used to reverse the string.


StringBuilder
Scanner Class in Java

• Scanner is a class in java.util package used for obtaining the input of the
primitive types like int, double, etc. and strings.
• To create an object of Scanner class, we usually pass the predefined object
System.in, which represents the standard input stream.
• Scanner in = new Scanner(System.in);
Method Description

next() It is used to get the next complete token from the


scanner which is in use.

nextInt() It scans the next token of the input as an Int.

nextLine() It is used to get the input string that was skipped of


the Scanner object.

You might also like