BCA : 5TH SEM
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN
RAM GOPAL GUPTA
ASSOCIATE PROFESSOR
DISCLAIMER
Contents of the tutorial, shown image(s) (if shown here) and movie(s) (if shown here)
are just compilation of research work/creation of various authors/organizations.
Contents are collected from various research papers, reports, books, websites and other
sources.
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN
UNIT-1
String IN JAVA
String is a sequence of characters. But in Java, string is an object that represents a sequence of
characters. The [Link] class is used to create a String object.
How to create a string object?
There are two ways to create String object:
1. By string literal
2. By new keyword
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:
String s1="Welcome";
String s2="Welcome"; //It doesn't create a new instance
s2 “Welcome”
s1
String Constant pool
Heap
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN
In the above example, only one object will be created. Firstly, JVM will not find any string object
with the value "Welcome" in string constant pool that is why it will create a new object. After that it
will find the string with the value "Welcome" in the pool, it will not create a new object but will
return the reference to the same instance.
2. By new keyword
String s=new String("Welcome");//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 a heap
(non-pool).
EXAMPLE 1:
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 new keyword
7. [Link](s1);
8. [Link](s2);
9. [Link](s3);
10. }
11. }
Output:
java
strings
example
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN
Java String class methods
The [Link] class provides many useful methods to perform operations on sequence of char
values.
No. Method Description
1 char charAt(int index) returns char value for the particular index
2 int length() returns string length
3 String substring(int returns substring for given begin index.
beginIndex)
4 String substring(int returns substring for given begin index and end
beginIndex, int endIndex) index.
5 boolean equals(Object checks the equality of string with the given object.
another)
6 String concat(String str) concatenates the specified string.
7 static String compares another string. It doesn't check case.
equalsIgnoreCase(String
another)
8 int indexOf(int ch) returns the specified char value index.
9 int indexOf(int ch, int returns the specified char value index starting with
fromIndex) given index.
10 int indexOf(String substring) returns the specified substring index.
11 int indexOf(String substring, returns the specified substring index starting with
int fromIndex) given index.
12 String toLowerCase() returns a string in lowercase.
13 String toUpperCase() returns a string in uppercase.
14 String trim() removes beginning and ending spaces of this string.
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN
15 static String valueOf(int value) converts given type into string. It is an overloaded
method.
BCA-S301T : JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN