String
ETL LABS PVT LTD – JAVA PROGRAMMING 96
What is Strings?
String is a sequence of characters. The
java.lang.String class is used to create a
string object.
4 How to create a string object?
There are two ways to create String
object:
1. By string literal
2. By new keyword
ETL LABS PVT LTD – JAVA PROGRAMMING 97
By String Literal
Java String literal is created by using
double quotes.
Syntax:
String_Type string_variable = 3
“sequence_of_string”;
Example:
String s="welcome";
ETL LABS PVT LTD – JAVA PROGRAMMING 98
By new keyword
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
4 will refer to the object in a heap (non-
pool).
Example
String s=new
String("Welcome");//creates two objects
and one reference variable
ETL LABS PVT LTD – JAVA PROGRAMMING 99
String Concatenation
string concatenation forms a new string
that is the combination of multiple Concatenation
strings. There are two ways to concat 3
string in java:
1. By + (string concatenation) operator
2. By concat() method
ETL LABS PVT LTD – JAVA PROGRAMMING 100
By + (string concatenation)
4 operator
Java string concatenation operator (+) is
used to add strings.
ETL LABS PVT LTD – JAVA PROGRAMMING 101
By concat() method
The String concat() method concatenates
the specified string to the end of current
string. 3
Syntax:
public String concat(String
another)
ETL LABS PVT LTD – JAVA PROGRAMMING 102
Three Ways To Compare String In Java
Type 1 Type 2 Type 3
By equals() By = = operator By compareTo()
method method
ETL LABS PVT LTD – JAVA PROGRAMMING 103
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:
4 • public boolean equals(Object
another) compares this string to the
specified object.
• public boolean
equalsIgnoreCase(String another)
compares this String to another
string, ignoring case.
ETL LABS PVT LTD – JAVA PROGRAMMING 104
By == operator
The = = operator compares references 3
not values.
ETL LABS PVT LTD – JAVA PROGRAMMING 105
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.
4
Suppose s1 and s2 are two string
variables. If:
• s1 == s2 :0
• s1 > s2 :positive value
• s1 < s2 :negative value
ETL LABS PVT LTD – JAVA PROGRAMMING 106
Substring
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. Index always starts from
0.
You can get substring from the given string
object by one of the two methods:
1. public String substring(int startIndex): 3
This method returns new String object
containing the substring of the given
string from specified startIndex (inclusive).
2. 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.
ETL LABS PVT LTD – JAVA PROGRAMMING 107
String toUpperCase() and
toLowerCase() method
The java string toUpperCase() method
4 converts this string into uppercase letter
and string toLowerCase() method into
lowercase letter.
ETL LABS PVT LTD – JAVA PROGRAMMING 108
String trim() method
The string trim() method eliminates white 3
spaces before and after string.
ETL LABS PVT LTD – JAVA PROGRAMMING 109
String length() method
4 The string length() method returns
length of the string.
ETL LABS PVT LTD – JAVA PROGRAMMING 110
String trim() method
The string trim() method eliminates white 3
spaces before and after string.
ETL LABS PVT LTD – JAVA PROGRAMMING 111
String charAt() method
4 The string charAt() method returns a
character at specified index.
ETL LABS PVT LTD – JAVA PROGRAMMING 112
String s1="Java is a programming language.
Java is a platform. Java is an Island.";
String replace() method String
The string replace() method replaces all 3 replaceString=s1.replace("Java","Kava");
occurrence of first sequence of character with //replaces all occurrences of "Java" to "Kava"
second sequence of character. System.out.println(replaceString);
ETL LABS PVT LTD – JAVA PROGRAMMING 113
Type Casting
Type casting is when you assign a value
of one primitive data type to another
type.
There are two types of casting:
• Widening Casting (automatically) -
converting a smaller type to a larger
4 type size
byte -> short -> char -> int ->
long -> float -> double
• Narrowing Casting (manually) -
converting a larger type to a smaller
size type
double -> float -> long -> int ->
char -> short -> byte
ETL LABS PVT LTD – JAVA PROGRAMMING 114
Widening Casting
Widening casting is done automatically when 3
passing a smaller size type to a larger size
type:
ETL LABS PVT LTD – JAVA PROGRAMMING 115
Narrowing Casting
4 Narrowing casting must be done
manually by placing the type in
parantheses in front of the value:
ETL LABS PVT LTD – JAVA PROGRAMMING 116
super Keyword
The super keyword refers to superclass
(parent) objects.
It is used to call superclass methods,
and to access the superclass constructor.
The most common use of the super
keyword is to eliminate the confusion
between superclasses and subclasses
that have methods with the same name.
ETL LABS PVT LTD – JAVA PROGRAMMING 117
What is thread?
A thread is a:
• Facility to allow multiple activities
within a single process
• Referred as lightweight process
• A thread is a series of executed
4 statements
• Each thread has its own program
counter, stack and local variables
• A thread is a nested sequence of
method calls
• Its shares memory, files and per-
process state
ETL LABS PVT LTD – JAVA PROGRAMMING 118
Two way to create thread
By extending By implementing
Thread class Runnable interface
11
ETL LABS PVT LTD – JAVA PROGRAMMING 9
Commonly used methods of
Thread class
public void run(): is used to perform
•
action for a thread.
• public void start(): starts the execution of
3
the thread. JVM calls the run() method on
the thread.
• public void sleep(long miliseconds):
Causes the currently executing thread to
sleep (temporarily cease execution) for the
specified number of milliseconds.
There are so many methods in threads class.
ETL LABS PVT LTD – JAVA PROGRAMMING 120
Runnable interface
The Runnable interface should be
implemented by any class whose
4 instances are intended to be executed by
a thread. Runnable interface have only
one method named run().
• public void run(): is used to perform
action for a thread.
ETL LABS PVT LTD – JAVA PROGRAMMING 121