Strings

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Strings in Java

Java String
• String is a sequence of characters.
• In java, objects of String are immutable which
means a constant and cannot be changed
once created.
• There are two ways to create string in Java:
String s = “GLA University”; //String literal
String s = new String (“GLA University”);
// Using new keyword
String Literal
• Java String literal is created by using double quotes.   
• String objects are stored in a special memory area
known as string constant pool.
• 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 string doesn't exist in the pool, a new string
instance is created and placed in the pool.
String Constant Pool
String s1="Welcome";  
String s2="Welcome";   

Here, a new instance will


not be created for s2 as
the same literal is
available in the pool.
Hence, s2 will also refer
to the same instance in
the pool.
Declaring String using new keyword

String s=new String("Welcome");
• The above code creates two objects and one
reference variable.
• 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 heap(non
pool).
Methods defined in String class
No. Method Description

returns char value for


1 charAt(int index)
the particular index
2 length() returns string length
returns substring for
3 substring(int beginIndex)
given begin index
returns substring for
4 substring(int beginIndex, int endIndex) given begin index and
end index
returns true or false
after matching the
5 contains(CharSequence s)
sequence of char
value
Methods defined in String class
No. Method Description

checks the equality of


6 equals(Object another)
string with object
checks if string is
7 isEmpty()
empty
concatenates specified
8 concat(String str)
string
replaces all
9 replace(char old, char new) occurrences of
specified char value
replaces all
replace(CharSequence old, occurrences of
10
CharSequence new) specified
CharSequence
Methods defined in String class
No. Method Description

returns splitted string


11 String[] split(String regex)
matching regex
returns splitted string
12 String[] split(String regex, int limit) matching regex and
limit
returns specified char
13 int indexOf(int ch)
value index
returns specified char
14 int indexOf(int ch, int fromIndex) value index starting
with given index
returns specified
15 int indexOf(String substring)
substring index
Methods defined in String class
No. Method Description

returns specified substring


int indexOf(String substring, int
16 index starting with given
fromIndex)
index
17 String toLowerCase() returns string in lowercase.
18 String toUpperCase() returns string in uppercase.
Returns last index of a
19 int lastindexOf(int ch)
character in the string
int out =
s1.compareTo(s2); 
If : out < 0 // s1 comes
 int compareTo(String
20 before s2
anotherStr)
out = 0 // s1 and s2 are
equal.
out >0 // s1 comes after s2.
Abstract class in Java

A class which is declared as abstract is known as an abstract


class. It can have abstract and non-abstract methods. It needs
to be extended and its method implemented. It cannot be
instantiated.

Points to Remember

•An abstract class must be declared with an abstract keyword.


•It can have abstract and non-abstract methods.
•It cannot be instantiated.
•It can have constructors and static methods also.
•It can have final methods which will force the subclass not to
change the body of the method.
abstract class Bike{  
  abstract void run();  
}  
class Honda4 extends Bike{  
void run(){System.out.println("running safely");}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  
}  
Java Recursion Example 

public class RecursionExample3 {  
    static int factorial(int n){      
          if (n == 1)      
            return 1;      
          else      
            return(n * factorial(n-1));      
    }      
  
public static void main(String[] args) {  
System.out.println("Factorial of 5 is: "+factorial(5));  
}  
}  

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy