Lecture 16 OOP
Lecture 16 OOP
Credit Hour: 3
Lec - 16
Course Teacher
Md Istakiak Adnan Palash
Lec - 16 1
Java String
• In Java, string is basically an object that represents sequence of char
values. An array of characters works same as Java string. For example:
char[] ch={'j','a','v','a','t','p','o','i','n','t'};
String s=new String(ch);
is same as:
String s="javatpoint";
Lec - 16 2
• Java String class provides a lot of methods to perform operations
on string such as compare(), concat(), equals(), split(), length(),
replace(), compareTo(), intern(), substring() etc.
• By new keyword
Lec - 16 3
1) String Literal
• Java String literal is created by using double quotes. For Example:
String s="welcome";
• 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:
Lec - 16 4
2) By new keyword
• In such case, JVM will create a new string object in normal (non-
pool) heap memory, and the literal "Welcome" will be placed in
the string constant pool. The variable s will refer to the object in a
heap (non-pool).
Lec - 16 5
Java String Example
1.public class StringExample{
2.public static void main(String args[]){
3.String s1="java";//creating string by java string literal
4.char ch[]={'s','t','r','i','n','g','s'};
5.String s2=new String(ch);//converting char array to string
6.String s3=new String("example");//creating java string by ne
w keyword
7.System.out.println(s1);
8.System.out.println(s2);
9.System.out.println(s3);
10.}}
Output:
java
strings
example
Lec - 16 6
Java String class methods
• The java.lang.String class provides many useful methods
to perform operations on sequence of char values.
Lec - 16 7
Immutable String in Java
• In java, string objects are immutable. Immutable simply means
unmodifiable or unchangeable.
• Once string object is created its data or state can't be changed
but a new string object is created.
1.class Testimmutablestring{
2. public static void main(String args[]){
3. String s=“Saif";
4. s.concat(" Shams");//concat() method appends the
Output: Saif
string at the end
5. System.out.println(s);//will print Saif because string
s are immutable objects
6. }
7.}
Lec - 16 8
Immutable String in Java
• if we explicitely assign it to the reference variable, it
will refer to "Saif Shams" object.For example:
1.class Testimmutablestring1{
2. public static void main(String args[]){
3. String s="Saif";
4. s=s.concat(" Shams");
5. System.out.println(s);
6. }
7.}
Output:Saif Shams
Lec - 16 9
Java String compare
• We can compare string in java on the basis of content and
reference.
• It is used in authentication (by equals()
method), sorting (by compareTo() method), reference
matching (by == operator) etc.
• There are three ways to compare string in java:
• By equals() method
• By = = operator
• By compareTo() method
Lec - 16 10
1) String compare by equals() method
• The String equals() method compares the original content of the string. It
compares values of string for equality. String class provides two methods:
Lec - 16 11
1) String compare by equals() method
1.class Teststringcomparison1{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin"); Output: true
true
6. String s4="Saurav";
false
7. System.out.println(s1.equals(s2));//true
8. System.out.println(s1.equals(s3));//true
9. System.out.println(s1.equals(s4));//false
10. }
11.}
Lec - 16 12
1) String compare by equals() method
1.class Teststringcomparison2{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="SACHIN";
5.
6. System.out.println(s1.equals(s2));//false
7. System.out.println(s1.equalsIgnoreCase(s2));//true
8. }
9.}
Output:
false
true
Lec - 16 13
2) String compare by == operator
• The = = operator compares references not values.
1.class Teststringcomparison3{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. System.out.println(s1==s2);//true (because both refe Output: true
r to same instance) false
7. System.out.println(s1==s3);//false(because s3 refers
to instance created in nonpool)
8. }
9.}
Lec - 16 14
3) String compare by compareTo() method
• The String compareTo() method compares values lexicographically
and returns an integer value that describes if first string is less than,
equal to or greater than second string.
• Suppose s1 and s2 are two string variables. If:
• s1 == s2 :0
• s1 > s2 :positive value
• s1 < s2 :negative value
Lec - 16 15
3) String compare by compareTo() method
1.class Teststringcomparison4{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3="Ratan"; Output: 0
6. System.out.println(s1.compareTo(s2));//0 1
7. System.out.println(s1.compareTo(s3));//1(because s -1
1>s3)
8. System.out.println(s3.compareTo(s1));//-1
(because s3 < s1 )
9. }
10.}
Lec - 16 16
Write a java program to compare
two strings lexicographically.
Lec - 16 17
String 1: This is exercise 1
String 2: This is Exercise 1
"This is exercise 1" is equal to "This is
Exercise 1"
Lec - 16 18
Write a Java program to compare a given string
to the specified character sequence.
Lec - 16 19
Lec - 16 20
String Concatenation in Java
Lec - 16 21
2) String Concatenation by concat() method
1.class TestStringConcatenation3{
2. public static void main(String args[]){
3. String s1="Sachin ";
4. String s2="Tendulkar";
5. String s3=s1.concat(s2);
6. System.out.println(s3);//Sachin Tendulkar
7. }
8.}
Lec - 16 22
Substring in Java
• A part of string is called substring. In other words, substring is a subset of
another string. In case of substring startIndex is inclusive and endIndex is
exclusive.
• We can get substring from the given string object by one of the two
methods:
• public String substring(int startIndex): This method returns new
String object containing the substring of the given string from
specified startIndex (inclusive).
• public String substring(int startIndex, int endIndex): This method
returns new String object containing the substring of the given string
from specified startIndex to endIndex.
Lec - 16 23
Substring in Java
1.String s="hello";
2.System.out.println(s.substring(0,2));//he
Lec - 16 24
Java String charAt()
• The java string 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 length of
the string. It returns StringIndexOutOfBoundsException if given
index number is greater than or equal to this string length or a
negative number.
Lec - 16 25
Java String charAt() Example 1
Output: t
Lec - 16 26
Java String charAt() Example 2
1.public class CharAtExample3 {
2. public static void main(String[] args) {
3. String str = "Welcome to Javatpoint portal";
4. int strLength = str.length();
5. // Fetching first character
6. System.out.println("Character at 0 index is: "+ str.charA Output:
t(0)); Character at 0 index is: W
7. // The last Character is present at the string length- Character at last index is: l
1 index
8. System.out.println("Character at last index is: "+ str.cha
rAt(strLength-1));
9. }
10.}
Lec - 16 27
Java String charAt() Example 3
1.public class CharAtExample5 {
2. public static void main(String[] args) {
3. String str = "Welcome to Javatpoint portal";
4. int count = 0;
5. for (int i=0; i<=str.length()-1; i++) {
6. if(str.charAt(i) == 't') {
7. count++;
8. }
9. }
10. System.out.println("Frequency of t is: "+count);
11. }
12.}
Output:
Frequency of t is: 4
Lec - 16 28
Write a Java program to get the character at the
given index within the String.
Lec - 16 29
Thank You
Lec - 16 30