Mod 4 Chap-1
Mod 4 Chap-1
com/c/EDULINEFORCSE
STUDENTS
MODULE 4
ADVANCED FEATURES OF JAVA
CHAPTER 1
Java Library & Collections framework
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={‘h','a',‘i',‘j',‘a',‘v',‘a'};
String s=new String(ch);
is same as:
String s = “haijava";
• Java String class provides a lot of methods to perform operations
on strings such as compare(), concat(), equals(), split(), length(),
replace(), compareTo(), intern(), substring() etc.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 2
String s1="Welcome";
String s2="Welcome"; //It doesn't create a new instance
• 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.
• Note: String objects are stored in a special memory area known as
the "string constant pool".
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).
String Example
OUTPUT
STRING CONSTRUCTORS
• The string class supports several types of constructors in Java APIs.
The most commonly used constructors of String class are as
follows:
1. String() : To create an empty String, we will call a default
constructor. For example:
String s = new String();
• It will create a string object in the heap area with no value
EXAMPLE
STRING LENGTH
• The java string length() method gives length of the string. It returns
count of total number of characters.
• Internal implementation
public int length() {
return value.length;
}
Signature - The signature of the string length() method is given
below:
public int length()
Output
Output
STRING COMPARISON
• We can compare string in java on the basis of content and reference
• There are three ways to compare string in java:
By equals() method
By = = operator
By compareTo() method
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
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 19
Example 2
Output
Eg:
Output
SEARCHING STRINGS
String contains()
• The java string contains() method searches the sequence of
characters in this string.
• It returns true if sequence of char values are found in this string
otherwise returns false.
Internal implementation
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 26
Signature
• The signature of string contains() method is given below:
public boolean contains(CharSequence sequence)
Output
Output
CHARACTER EXTRACTION
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.
• Signature - The signature of string charAt() method is given below:
public char charAt(int index)
Example:
Output
t
MODIFY STRINGS
• The java string replace() method returns a string replacing all the
old char or CharSequence to new char or CharSequence.
Signature
• There are two type of replace methods in java string.
public String replace(char oldChar, char newChar)
and
public String replace(CharSequence target, CharSequence
replacement)
• The second replace method is added since JDK 1.5.
Output
my name was khan my name was java
Output
• The java string replaceAll() method returns a string replacing all the
sequence of characters matching regex and replacement string.
Internal implementation
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
Signature
public String replaceAll(String regex, String replacement)
Output
My name was Khan. My name was Bob. My name was Sonoo.
Output
MynameisKhan.MynameisBob.MynameisSonoo.
STRING VALUE OF ( )
• The java string valueOf() method converts different types of values
into string.
• By the help of string valueOf() method, we can convert int to
string, long to string, boolean to string, character to string, float to
string, double to string, object to string and char array to string.
Internal implementation
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Signature
• The signature or syntax of string valueOf() method is given below:
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
public static String valueOf(Object o)
Output
3010
Output
true
false
Output
A
B
Output
10.05
10.02
Output
Output Sachin
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 52
• As you can see in the figure that two objects are created but s
reference variable still refers to "Sachin" not to "Sachin Tendulkar".
• But if we explicitely assign it to the reference variable, it will refer
to "Sachin Tendulkar" object. For example:
Output
Sachin Tendulkar
In such case, s points to the
"Sachin Tendulkar". Please notice
that still sachin object is not
modified.
COLLECTIONS IN JAVA
The Collection in Java is a framework that provides an architecture
to store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on
a data such as searching, sorting, insertion, manipulation, and
deletion.
Java Collection means a single unit of objects. Java Collection
framework provides many interfaces (Set, List, Queue, Deque) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet).
The java.util package contains all the classes and interfaces for the
Collection framework.
Collection Interface
• The Collection interface is the interface which is implemented by all
the classes in the collection framework.
• It declares the methods that every collection will have. In other
words, we can say that the Collection interface builds the
foundation on which the collection framework depends.
• Some of the methods of Collection interface are Boolean add (
Object obj), Boolean addAll ( Collection c), void clear(), etc. which
are implemented by all the subclasses of Collection interface.
LIST INTERFACE
• List interface is the child interface of Collection interface.
• It inhibits a list type data structure in which we can store the
ordered collection of objects.
• It can have duplicate values.
• List interface is implemented by the classes ArrayList,
LinkedList, Vector, and Stack.
• To instantiate the List interface, we must use :
Java ArrayList class uses a dynamic array for storing the elements.
It is like an array, but there is no size limit. We can add or remove
elements anytime.
So, it is much more flexible than the traditional array. It is found in
the java.util package. It is like the Vector in C++.
The ArrayList in Java can have the duplicate elements also. It
implements the List interface so we can use all the methods of List
interface here.
The ArrayList maintains the insertion order internally.
It inherits the AbstractList class and implements List interface.
ArrayList Example