You are on page 1of 29

CS 8392

OBJECT ORIENTED
PROGRAMMING
Object Cloning

VASANTHA KUMAR V,
AP/CSE
this keyword in java

• The this keyword can be used to refer current class


instance variable.
• If there is ambiguity between the instance variables and
parameters, this keyword resolves the problem of
ambiguity.
Usage of java
this keyword
EXAMPLE
class Student class TestThis2
{ {
int rollno; public static void main(String args[])
String name; {
float fee; Student s1=new Student(111,"ankit",5000f);
Student(int rollno,String name,float fee) Student s2=new Student(112,"sumit",6000f);
{ s1.display();
this.rollno=rollno; s2.display();
this.name=name; }
this.fee=fee; }
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
Object Cloning in Java

• The object cloning is a way to create exact copy of an


object.
• The clone() method of Object class is used to clone an
object.
• The java.lang.Cloneable interface must be implemented
by the class whose object clone we want to create.
• If we don't implement Cloneable interface, clone()
method generates CloneNotSupportedException.
Object Cloning in Java

The clone() method is defined in the Object class.

Syntax of the clone() method is as follows:


Access specifier Object clone() throws CloneNotSupportedException
Why use clone() method ?

• The clone() method saves the extra processing task for


creating the exact copy of an object.
• If we perform it by using the new keyword, it will take a lot
of processing time to be performed that is why we use
object cloning.
Advantage of Object cloning

• You don't need to write lengthy and repetitive codes.


• Just use an abstract class with a 4- or 5-line long clone() method.
• It is the easiest and most efficient way for copying objects,
especially if we are applying it to an already developed or an
old project.
• Clone() is the fastest way to copy array.
Example of clone() method
class Student18 implements Cloneable public static void main(String args[])
{ {
int rollno; try{
String name; Student18 s1=new Student18(101,"amit");

Student18(int rollno,String name) Student18 s2=(Student18)s1.clone();


{
this.rollno=rollno; System.out.println(s1.rollno+" "+s1.name);
this.name=name; System.out.println(s2.rollno+" "+s2.name);
}
}catch(CloneNotSupportedException c){}
public Object clone()throws CloneNotSupported
Exception }
{ }
return super.clone();
}
Java ArrayList

• The ArrayList class is a resizable array, which can be


found in the java.util package.
• The difference between a built-in array and an ArrayList in
Java, is that the size of an array cannot be modified .
• While elements can be added and removed from an
ArrayList whenever you want.
Syntax and Example

Syntax:
import java.util.ArrayList;
ArrayList<Datatype> Array_name= new ArrayList<Datatype>();

Example:
import java.util.ArrayList;
ArrayList<String> cars = new ArrayList<String>();
Example

import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
Access an Item

To access an element in the ArrayList, use the get() method


and refer to the index number .

Example:
cars.get(0);
Change an Item

To modify an element, use the set() method and refer to


the index number.

Example:
cars.set(0, "Opel");
Remove an Item
To remove an element, use the remove() method and refer
to the index number.
Example:
cars.remove(0);

To remove all the elements in the ArrayList, use the clear()


method.
Example:
cars.clear();
ArrayList Size

To find out how many elements an ArrayList have, use the


size method.

Example:
cars.size();
Loop Through an ArrayList
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
Loop Through an ArrayList
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (String i : cars) {
System.out.println(i);
}
Sort an ArrayList
import java.util.ArrayList;
import java.util.Collections; // Import the Collections class
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars); // Sort cars
for (String i : cars) {
Other Types - Example
import java.util.ArrayList;

public class MyClass {


public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
Java Strings

A String variable contains a collection of characters


surrounded by double quotes .
Example:
String greeting = "Hello";
String Length

Example:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " +
txt.length());
UpperCase and LowerCase

Example:
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
Finding a Character in a String

Example:
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate"));
String Concatenation

Example:
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
System.out.println(firstName.concat(lastName));
Special Characters

Escape character Result Description

\' ' Single quote

\" " Double quote

\\ \ Backslash
Example

String txt = "We are the so-called \"Vikings\" from the north.";

String txt = "It\'s alright.";

String txt = "The character \\ is called backslash.";


Adding Numbers and Strings

Example 1 :
String x = "10";
String y = "20";
String z = x + y;
Example 2 :
String x = "10";
int y = 20;
String z = x + y;
THANK YOU

You might also like