0% found this document useful (0 votes)
2 views5 pages

UNIT 2 String

In Java, strings are immutable collections of characters created using string literals, the new keyword, or character arrays. The String class provides various methods for manipulating and comparing strings, such as converting case, trimming whitespace, and finding substrings. Key characteristics include that once a string object is created, its value cannot be changed, and common methods include length(), toLowerCase(), and indexOf().

Uploaded by

PullaReddy L
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)
2 views5 pages

UNIT 2 String

In Java, strings are immutable collections of characters created using string literals, the new keyword, or character arrays. The String class provides various methods for manipulating and comparing strings, such as converting case, trimming whitespace, and finding substrings. Key characteristics include that once a string object is created, its value cannot be changed, and common methods include length(), toLowerCase(), and indexOf().

Uploaded by

PullaReddy L
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

STRINGS

STRINGS:

In Java, string is a collection of characters placed in between double quotes. It


is an object of String class type. Once a String object is created it cannot be
changed. Strings are immutable, meaning their value cannot be changed once
created

Creating strings:
There are 3 ways to create strings in java
1. Using String Literals:
we can create a string by assigning group of characters to a string type
variable
String stringname;
stringname = ”Hello”;
It can be combined and written as String stringname=new
String(”Hello”);

2. Using the new Keyword:


we can create an object to String class by allocating memory using new
operator.
String s=new String(“Hello”);

3. Using character array:


we can create strings by converting the character array into strings
char a[]={‘H’,’E’,’L’,’L’,’O’}; or char[] a={‘H’,’E’,’L’,’L’,’O’};

now create a string object by passing the array name to it, as


String s= new String(a);
Now string contains as “HELLO”

This means all the characters of array are copied into the string s. If we don’t
want all
characters of the array into the array, then we can mention which characters
we need:
String s=new String(arr, indice_location, size);
String s=new String(arr,2,3);

Example:
public class StringEx {
public static void main(String[] args) {

// Creating string using new keyword


By L. Pulla Reddy, Lecturer in Computer Science
pg. 1
String str1 = new String("Hello Java");
[Link]("String using new keyword: " + str1);

// Creating string from character array


char[] c = { 'J', 'A', 'V', 'A' };
String str2 = new String(c);
[Link]("String from char array: " + str2);
}
}

Output
String using new keyword: Hello Java
String from char array: JAVA

Q. Implementation of Strings in Java:


In Java, strings are class objects and implemented by using two classes
[Link] Class
[Link] Class

1. String Class:
In Java, String class represents collection of characters placed in between
double quotes. It provides various methods for creating, comparing, and
manipulating strings. String objects are immutable, means their values cannot
be changed after creation.

Strings may be declared and created as follows:


String stringName;
stringName = new String(“string”); ( or) String strname=new
String(“text”);

Example: String str = new String(“Java”);

It is possible to get the length of string using the length method of the String
class.
int m = [Link]( );

Example:
public class StringClass_Ex {
public static void main(String[] args) {
String str1 = new String("Hello Java");
[Link]("String: " + str1);
}
}

By L. Pulla Reddy, Lecturer in Computer Science


pg. 2
Output: Hello Java

Note:
String are Immutable means that once a String object is created, its value
cannot be changed.
Example:
public class Main {
public static void main(String[] args) {
String text = "hello";
[Link](0) = 'H'; // compile-time error
}
}

Explanation: The line [Link](0) = 'H'; causes a compile-time error because


charAt(0) returns a read-only char, not a variable. String is immutable in Java,
you cannot modify its characters directly.

String Class Methods:


Java String methods are built-in functions provided by the String class to
perform various operations on strings. These methods help in manipulating,
comparing, searching, and formatting text efficiently.
Some Most Commonly Used String class methods are:

Method Operation
s2=[Link]; Converts the string s1 to all lowercase
s2=[Link]; Converts the string s1 to all uppercase
s2=[Link](‘x’,’y’); Replace all appearances of x with y
s2=[Link](); Remove white spaces at the beginning and end
of the string s1
[Link](s2) Returns ‘true’ if s1 is equal to s2
[Link]() Gives the length of s1
[Link](n) Gives nth character of s1
[Link](s2) Returns negative if s1<s2, positive if s1>s2, and
zero if s1=s2
[Link](s2) Concatenates s1 and s2
[Link](n) Gives substring starting from nth character
s1. substring(n,m) Gives substring starting from nth character up to
mth (not including mth)
[Link](‘x’) Gives the position of the first occurrence of ‘x’
in the string s1

By L. Pulla Reddy, Lecturer in Computer Science


pg. 3
s1. indexOf(‘x’,n) Gives the position of ‘x’ that occurs after n th
position in the string s1

By L. Pulla Reddy, Lecturer in Computer Science


pg. 4
By L. Pulla Reddy, Lecturer in Computer Science
pg. 5

You might also like