0% found this document useful (0 votes)
18 views11 pages

Java String Basics and Methods Guide

The document provides an overview of the Java String class, explaining that a String is an object representing a sequence of characters. It details how to create String objects using string literals and the new keyword, along with the implications of each method. Additionally, it lists various String class methods and provides examples of their usage.

Uploaded by

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

Java String Basics and Methods Guide

The document provides an overview of the Java String class, explaining that a String is an object representing a sequence of characters. It details how to create String objects using string literals and the new keyword, along with the implications of each method. Additionally, it lists various String class methods and provides examples of their usage.

Uploaded by

Amgd Talal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String in Java Lect.

#4
By: Dr. Mohammad Hashim
2 Java String

• In Java, String is basically an object that represents sequence of char values.


• The [Link] class is used to create a String object.
• An array of characters works same as Java String. For example:
char[] ch={'j','a','v','a',’4',’0',’1',’C',’S'};
String s=new String(ch);
is the same as
String s="java401CS";
3 HOW TO CREATE A STRING OBJECT?

There are two ways to create String object:


• By String literal
• By new keyword
4 STRING LITERAL

• Java String literal is created by using double quotes. For Example:


String s = “Java”;
• 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.

• For example:
String s1 = “Welcome”;
String s2 = “Welcome”; // it doesn’t create a new object
In this case (s1 == s2) returns true.
5 BY NEW KEYWORD

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


• In this case if we create another String object containing the same value, then we
will get to different reference objects pointing to two different places.
• For example:
s3 Welcome
String s3 = new String (“Welcome”);
String s4 = new String (“Welcome”); Welcome
s4
In this case (s3 == s4) returns false.
6 STRING CONSTRUCTORS

String class has many constructors to create a new object, some of them are:
• String ()
• Creates an empty String object;

• String (char [ ] )
• Creates a new String object referring to the char array (“String literal”).

• String (String p)
• Creates a new String object referring to a new place have the same value as p.
7 JAVA STRING CLASS METHODS

The String class provides many useful methods to perform operations on sequence of char values.
8 JAVA STRING CLASS METHODS (CONT.)

The String class provides many useful methods to perform operations on sequence of char values.

int indexOf(char ch)

boolean equalsIgnoreCase(String another)

int lastIndexOf (char ch) return the specified char value last index.
9 EXAMPLES:

String name="javaLectures";
• int len = [Link](); // returns the length of the string which is 12
• char ch=[Link](4); //returns the char value at the 4th index which is ‘L’
• ch=[Link](-4); //throws a “StringIndexOutOfBoundsException”
• String s2 = [Link](4); // returns a new String containing “Lectures”
• String s3 = [Link](4,8); // returns a new String containing “Lect” (index 4 in but index 8 out)
• s3 = [Link](4,15); //throws a “StringIndexOutOfBoundsException”
• [Link](“Java”) // returns “false” (case-sensetaive)
10 EXAMPLES:

String name="javaLectures";
• [Link](“JAVALECTURES”) // returns “false” (case-sensetaive)
• [Link](“JAVALECTURES”) // returns “true”
• String s3 = [Link](); // returns a new String containing “JAVALECTURES”
• String s4 = [Link](); // returns a new String containing “javalectures”
• int a = [Link](‘v’); // returns first occurrence of letter ‘v’ which is 2
• a = [Link](‘a’); // returns first occurrence of letter ‘a’ which is 1
• a = [Link](‘z’); // returns -1 (does not exist)
• a = [Link](‘a’); // returns last occurrence of letter ‘a’ which is 3
• int a = [Link](“tur”); // returns first occurrence of String “tur” which is 7
Thank You

11

You might also like