Syntax to create Object in java
The main intention to create object in java applications is
1.
To store data temporally in java application.
2.
To access members of a particular class.
To create object for the classes in java applications. We
have to use the following
syntax.
Class_Name ref_var=new Class_Name([Param_List]);
Ex :-
class A
{
int
i=10;
float
f;
char c;
A()
{
c="B";
}
---------
}
A a=new A();
When JVM encounter the above instruction, JVM will create
Object with the following three steps
1.
Create memory for the object
2.
Generation identifiers to the object
3.
Providing initial value to the object
1.
Create memory for the object
When JVM encounter “new” keyword, JVM will load the respective class
keyword to the memory [method area], with respective to the class loading JVM
will create java.lang.class Object in heap memory with the meta data of the
respective loaded class.
After loading class byte code, JVM will recognize all the instance
variables and their data types in the loaded class and identify memory size for
the object
After getting memory size, JVM will send a request to heap manger about
to crate object with the memory size, where heap manger will accept JVM request
and Heap manager will assign a block of memory as a single unit [Object] to the
JVM.
2.
Generating identifiers to the object:-
After crating object, heap manger will assign an integer value as an
unique identity called as hash code.
After getting hash code value, heap manager will send hash code value to
JVM, where JVM will convert hash code value to its hexa decimal form called
as Object reference value JVM will store
that reference value in a variable is
called as reference variable.
3.
Providing initial values to the object:-
After generations identities, JVM will provide memory allocation for the
instance variables inside the object and JVM will provide initializations for
the instance variable by checking initialization at class level declaration and
at constructer, if any variable is having initialization at declaration and at
constructor then JVM will store its default value as per the data type.
If we want to get hash code value and reference value
explicitly in java applications, then we have to use the following methods.
public native int hashcode()
public String toString()
Note:-
Native method is a method declared in java but implanted in
non-java programming like c, c++, assembly language ……
Ex code :-
class A
{
}
class Test
{
public
static void main(String[] args)
{
A
a=new A();
int
hc=a.hashCode();
System.out.println(hc);
String
ref=a.toString();
System.out.println(ref);
}
}
Output:-
Date: july-23-2016
In java, there is a common and default super class for each
and every Java class [predefined classes and defined classes] that is
“java.lang.Object” class, it contains the following 11 number of methods.
1. hashCode()
2. toString()
3. equals(Object obj)
4. clone()
5. wait(int time)
6. wait(int time, long time)
7. wait()
8. notify()
9. notifyAll()
10. finalize()
11.getClass()
Q) In java applications, if we
extends a class to another class then the respective sub class is having two
super classes [Object class and explicit super class], it represent multiple
inheritance, how can we say multiple inheritance is not possible in java?
Ans):-
In java, Object class is common and default super class for
each and every java class whenever the respective class is not extending any
other classes explicitly. If a class is extending from some other class
explicitly then object class is not direct super class to the respective sub
class, where object class is super class to the respective sub class through
multi level inheritance, not through multiple inheritance.
In java object class, toString(), was implemented in such a
way to return a string contains “Class_Name@Ref_Val”.
In java application, when we pass reference variable of a
particular class object as a parameter to System.out.println(-) method the “JVM
will access toStrin() method on the providing reference variable internally and
JVM will display the return of toString() method on commend prompt.
Ex code:-
class A
{
}
class Test
{
public
static void main(String[] args)
{
A
a=new A();
String
ref=a.toString();
System.out.println(ref);
System.out.println(a.toString());
System.out.println(a);
}
}
Output:-
A@1cde100
A@1cde100
A@1cde100
In the program, which we Access toStrin() method explicitly
(or) when JVM access toString() method implicitly, JVM will go to the class
which is referred by reference variable which we passed a parameter to
System.out.println(-) and JVM will search for toString() method, if
toString()method existed then JVM will access toString() method here if self,
JVM will not go to object class execute toString() method. If toString() method
is not existed. In the respective class then JVM will go to the Super class to
search for toString() method, if no explicitly super class is existed for the
respective class then JVM will go to common and default super class that is java.lang.Object class to
execute toString() method.
As per the requirements, if we want to display our own data
by passing a reference variable as a
parameter to System.out.println(-) method then we have to define our own
toString() method in the respective class.
Ex code:-
class Account
{
String
acc_No="abc123";
String
acc_Name="Sai";
String
acc_Type="Savings";
String
acc_Branch="S R Nagar";
String
acc_Bank="SBI Bank";
public
String toString()
{
System.out.println("Account
Details");
System.out.println("----------------");
System.out.println("Account
Number :"+acc_No);
System.out.println("Account
Name :"+acc_Name);
System.out.println("Account
Type :"+acc_Type);
System.out.println("Account
Branch :"+acc_Branch);
System.out.println("Account
Bank :"+acc_Bank);
return
"*******************";
}
}
class Test
{
public
static void main(String[] args)
{
Account
acc=new Account();
System.out.println(acc);
}
}
Output:-
Account Details
----------------
Account Number :abc123
Account Name :Sai
Account Type
:Savings
Account Branch :S R Nagar
Account Bank :SBI Bank
Note:-
In java programming language, some of pre-defined classes
like String, StringBuffer, Exception, Thread, Wrapper classes, all Collection
classes……. Are not depending on java.lang.object class toString(), they are
having their own toString() method in order to display their own data on
command prompt bypassing the respective classes reference variable as parameter
to System.oout.println(-) method.
Ex code:-
class Test
{
public
static void main(String[] args)
{
String
str=new String("abc");
System.out.println("str");
Exception
e=new Exception("My Own Exception");
System.out.println(e);
Thread
t=new Thread();
System.out.println(t);
Integer in=new
Integer(10);
System.out.println(in);
java.util.ArrayList al=new java.util.ArrayList();
al.add("AAA");
al.add("BBB");
al.add("CCC");
al.add("DDD");
System.out.println(al);
}
}
Output:-
java.lang.Exception: My Own Exception
Thread[Thread-0,5,main]
10
[AAA BBB CCC DDD]



No comments:
Post a Comment