Unit 4 OOPJ
Unit 4 OOPJ
Chapter 9- Autoboxing
1
0 9 -1 2 -2 0 2 4
2
0 9 -1 2 -2 0 2 4
Here, we are passing primitive type value. However, due to autoboxing, the
primitive value is automatically converted into an Integer object and stored
in the array list.
3
0 9 -1 2 -2 0 2 4
4
0 9 -1 2 -2 0 2 4
Wrapper Class
The wrapper
classes in Java are
used to convert
primitive types
(int, char, float,
etc) into
corresponding
objects.
Each of the 8
primitive types
has corresponding
wrapper classes.
Mrs. Divya M Patel (TMESICS) 9
Wrapper Class
Advantages of Wrapper Classes:
In Java, sometimes we might need to use objects instead of primitive data
types. For example, while working with collections.
5
0 9 -1 2 -2 0 2 4
Wrapper Class
We can store the null value in wrapper objects. For example,
Wrapper Class
Convert Primitive
Type to Wrapper
Objects.
We can also use the
valueOf() method to
convert primitive
types into
corresponding
objects.
6
0 9 -1 2 -2 0 2 4
Wrapper Class
In the above example, we have used the valueOf() method to convert the
primitive types into objects.
Here, we have used the instanceof operator to check whether the generated
objects are of Integer or Double type or not.
However, the Java compiler can directly convert the primitive types into
corresponding objects.
This process is known as auto-boxing.
For example,
Wrapper Class
Wrapper Objects into Primitive Types
To convert objects into the primitive types, we can use the corresponding
value methods (intValue(), doubleValue(), etc) present in each wrapper
class.
In the below example, we have used the intValue() and doubleValue()
method to convert the Integer and Double objects into corresponding
primitive types.
7
0 9 -1 2 -2 0 2 4
Wrapper Class
Wrapper Class
In the above example, we have used the intValue() and doubleValue() method
to convert the Integer and Double objects into corresponding primitive types.
This process is known as unboxing.
For example,
8
0 9 -1 2 -2 0 2 4
9
0 9 -1 2 -2 0 2 4
String
In Java, a string is a sequence of characters. For example, "hello" is a string
containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
We use double quotes to represent a string in Java. For example,
Here, we have created a string variable named type. The variable is initialized
with the string Java Programming.
String
In this example, we have created
three strings named first, second,
and third.
Here, we are directly creating
strings like primitive types.
However, there is another way
of creating Java strings (using
the new keyword).
10
0 9 -1 2 -2 0 2 4
String
Java String Operations:- Java String provides various methods to perform
different operations on strings. We will look into some of the commonly
used string operations.
1. Get length of a String:- To find the length of a string, we use the length()
method of the String.
2. Join Two Java Strings:- We can join two strings in Java using the concat()
method. We can also join two strings using the + operator in Java.
3. Compare two Strings:- In Java, we can make comparisons between two
strings using the equals() method.
String
We can also compare two strings using the == operator in Java. However, this
approach is different than the equals() method.
11
0 9 -1 2 -2 0 2 4
String
String
Escape character in Java Strings:- The escape character is used to escape
some of the characters present inside a string.
Suppose we need to include double quotes inside a string.
Since strings are represented by double quotes, the compiler will treat "This is
the " as the string. Hence, the above code will cause an error.
12
0 9 -1 2 -2 0 2 4
String
To solve this issue, we use the escape character \ in Java. For example,
Now, escape characters tell the compiler to escape double quotes and read
the whole text.
String
Java Strings are Immutable:- In Java, strings are immutable. This means,
once we create a string, we cannot change that string.
To understand it more deeply, consider an example:
13
0 9 -1 2 -2 0 2 4
String
Here, we have created a string variable named example. The variable holds the
string "Hello! ".
Now suppose we want to change the string.
Here, we are using the concat() method to add another string World to the
previous string.
It looks like we are able to change the value of the previous string. However,
this is not true.
String
Let's see what has happened here,
1. JVM takes the first string "Hello! “
2. creates a new string by adding "World" to the first string
3. assign the new string "Hello! World" to the example variable
4. the first string "Hello! " remains unchanged
14
0 9 -1 2 -2 0 2 4
String
Creating strings using the new keyword
So far we have created strings like primitive types in Java.
Since strings in Java are objects, we can create strings using the new
keyword as well. For example,
In the above example, we have created a string name using the new
keyword.
Here, when we create a string object, the String() constructor is invoked.
15
0 9 -1 2 -2 0 2 4
16
0 9 -1 2 -2 0 2 4
String Buffer
StringBuffer is a peer class of String that provides much of the functionality
of strings.
The string represents fixed-length, immutable character sequences while
StringBuffer represents growable and writable character sequences.
Java StringBuffer class is used to create mutable (modifiable) String objects.
The StringBuffer class in Java is the same as String class except it is mutable
i.e. it can be changed.
String Buffer
Constructors of StringBuffer class
1. StringBuffer(): It reserves room for 16 characters without reallocation.
17
0 9 -1 2 -2 0 2 4
18
0 9 -1 2 -2 0 2 4
19
0 9 -1 2 -2 0 2 4
20
0 9 -1 2 -2 0 2 4
String Buffer
String Buffer
Auxiliary methods of String buffer are as follows:
ensureCapacity(): It is used to increase the capacity of a StringBuffer
object. The new capacity will be set to either the value we specify or twice the
current capacity plus two (i.e. capacity+2), whichever is larger. Here, capacity
specifies the size of the buffer.
Syntax:
21
0 9 -1 2 -2 0 2 4
String Buffer
charAt(int index): This method returns the char value in this sequence at the
specified index.
Syntax:
int codePointAt(int index): This method returns the character (Unicode code
point) at the specified index.
Syntax:
String Buffer
int codePointBefore(int index): This method returns the character (Unicode
code point) before the specified index.
Syntax:
22
0 9 -1 2 -2 0 2 4
String Buffer
int indexOf(String str): This method returns the index within this string of
the first occurrence of the specified substring.
Syntax:
int lastIndexOf(String str): This method returns the index within this string
of the last occurrence of the specified substring.
Syntax:
23
0 9 -1 2 -2 0 2 4
Comparable interface
As the name itself suggests, Comparable is an interface which defines a way
to compare an object with other objects of the same type.
It helps to sort the objects that have self-tendency to sort themselves, i.e., the
objects must know how to order themselves. Eg: Roll number, age, salary.
This interface is found in java.lang package and it contains only one method,
i.e., compareTo().
Comparable is not capable of sorting the objects on its own, but the interface
defines a method int compareTo() which is responsible for sorting.
Comparable interface
public int compareTo(Object obj): It is used to compare the current object
with the specified object.
int compareTo(String str): Here, str is the String being compared with the
invoking String. The result of the comparison is returned and is
interpreted, as shown here:
24
0 9 -1 2 -2 0 2 4
Comparable interface
Using Comparable Interface
In this method, we are going to implement the Comparable interface from
java.lang Package in the Pair class.
The Comparable interface contains the method compareTo to decide the
order of the elements.
Override the compareTo method in the Pair class.
Create an array of Pairs and populate the array.
Use the Arrays.sort() function to sort the array.
Comparable interface
25
0 9 -1 2 -2 0 2 4
Comparable interface
26
0 9 -1 2 -2 0 2 4
27
0 9 -1 2 -2 0 2 4
28
0 9 -1 2 -2 0 2 4
Here, we have created objects list1 and list2 of classes ArrayList and
LinkedList. These objects can use the functionalities of the List interface.
29
0 9 -1 2 -2 0 2 4
30
0 9 -1 2 -2 0 2 4
Here, we have created a Set called animals. We have used the HashSet class to
implement the Set interface.
31
0 9 -1 2 -2 0 2 4
32
0 9 -1 2 -2 0 2 4
33
0 9 -1 2 -2 0 2 4
We have created a sorted set called animals using the TreeSet class.
Here we have used no arguments to create a sorted set. Hence
the set will be sorted naturally.
34
0 9 -1 2 -2 0 2 4
35
0 9 -1 2 -2 0 2 4
36
0 9 -1 2 -2 0 2 4
37
0 9 -1 2 -2 0 2 4
38
0 9 -1 2 -2 0 2 4
39
0 9 -1 2 -2 0 2 4
40
0 9 -1 2 -2 0 2 4
41
0 9 -1 2 -2 0 2 4
42
0 9 -1 2 -2 0 2 4
43
0 9 -1 2 -2 0 2 4
44
0 9 -1 2 -2 0 2 4
45
0 9 -1 2 -2 0 2 4
46
0 9 -1 2 -2 0 2 4
47
0 9 -1 2 -2 0 2 4
48
0 9 -1 2 -2 0 2 4
49
0 9 -1 2 -2 0 2 4
50
0 9 -1 2 -2 0 2 4
51
0 9 -1 2 -2 0 2 4
52
0 9 -1 2 -2 0 2 4
53
0 9 -1 2 -2 0 2 4
54
0 9 -1 2 -2 0 2 4
55
0 9 -1 2 -2 0 2 4
56
0 9 -1 2 -2 0 2 4
57
0 9 -1 2 -2 0 2 4
58
0 9 -1 2 -2 0 2 4
59
0 9 -1 2 -2 0 2 4
60