6.object, Class and Strings
6.object, Class and Strings
6.object, Class and Strings
in java
An entity that has state and
behavior is known as an object
e.g., chair, bike, marker, pen,
table, car, etc. It can be physical
or logical (tangible and
intangible). The example of an
intangible object is the banking
system.
Object Characteristics:
State: An object has a state, represented
by the attributes of an object and their
values. For example, a Car object might
have attributes like color, make, and
currentSpeed.
• Fields
• Methods
• Constructors
• Blocks
Ex : BranchName
SYNTAX TO DECLARE A CLASS
class <class_name>{
field;
method;
}
class Book
{
String title;
}
// Creating an object
Book myBook = new Book();
• The Book class is loaded into the memory by the class loader.
• The new keyword tells the JVM to allocate memory in the
heap for a new Book object.
• JVM allocates memory space for the object, which is enough
to accommodate all instance variables declared in the Book
class.
• The default constructor is called to initialize the object. If
there were any initialization blocks, they would be executed
in this step.
• The heap memory now contains the Book object with its title
field set to null.
• A reference to the Book object is assigned to the myBook
variable. This reference is a pointer that points to the location
in the heap where the Book object is stored.
STACK HEAP
3 Ways to initialize object
There are 3 ways to initialize object in Java.
By reference variable
By method
By constructor
Object and Class Example: main within the class
In this example, we have created a Student class which has two data members
id and name. We are creating the object of the Student class by new keyword
and printing the object's value.
Here, we are creating a main() method inside the class.
//Java Program to illustrate how to define a class and fields
class Student{
//defining fields
int id;//field or data member or instance variable OUTPUT:
String name; 0
//creating main method inside the Student class
public static void main(String args[]){ null
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name); } }
Object and Class Example: main outside the class
In real time development, we create classes and use it from another
class. It is a better approach than previous one. Let's see a simple
example, where we are having main() method in another class.
//Java Program to demonstrate having the main method in another class
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{ OUTPUT:
public static void main(String args[]){
Student s1=new Student(); 0
System.out.println(s1.id); null
System.out.println(s1.name);
} }
Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Let's see a
simple example where we are going to initialize the object through
a reference variable.
class Student{
int id;
String name; 235550 Arun
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=235550;
s1.name=“Arun";
System.out.println(s1.id+" "+s1.name);//printing members with a
white space } }
Object and Class Example: Initialization through method
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation(); } }
Object and Class Example: Initialization through a constructor
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display(); } }
1
The clone() method is the method of Object class. It creates a copy of an object
and returns the same copy. The JVM creates a new object when the clone()
method is invoked. It copies all the content of the previously created object
into new one object. Note that it does not call any constructor.
We must implement the Cloneable interface while using the clone() method.
The method throws CloneNotSupportedException exception if the object's
class does not support the Cloneable interface. The subclasses that override
the clone() method can throw an exception if an instance cannot be cloned.
SYNTAX:
We use the following statement to create a new object.
❖ By string literal
❖ 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
• 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
1 char charAt(int index) It returns char value for the particular index
4 static String format(Locale l, String format, It returns formatted string with given locale.
Object... args)
6 String substring(int beginIndex, int endIndex) It returns substring for given begin index and end
index.
13 String replace(char old, char new) It replaces all occurrences of the specified
char value.
charAt(int index)
Returns the character at the specified index.
concat(String str)
Concatenates the specified string to the end of the calling string.
Compares the calling string to the specified object. The result is true if and
only if the argument is not null and is a String object that represents the
same sequence of characters as this object.
Converts all the characters in the string to lower case or upper case.
trim()
Returns a copy of the string, with leading and trailing whitespace omitted.