Hello,
First of all Hapy Xmas to every one..! :)
As you have already know how to work with Primitive Data types (at least I believe :P). Now I will explain the working of Non-Primitive Data Types which are also called as Reference Data Types.
First of all Hapy Xmas to every one..! :)
As you have already know how to work with Primitive Data types (at least I believe :P). Now I will explain the working of Non-Primitive Data Types which are also called as Reference Data Types.
Non-Primitive Data
Types:
Our ancestors had already defined some data types so that we
can use them is storing the data into variables. If we need to store the
user-defined data into a variable then we need to define our own Data type.
This brings the Non-Primitive data types into the picture. All user defined
data types come under Non-Primitive category. But not vice-versa. All pre-defined data types are not
Primitives..!! Remember this. We have ONLY 8 primitive data types. Any other
data type in this Java world is Non-Primitive.
To discuss further, you should be aware of what is a Class ,
Object and Reference.
Anything and everything in Java Programming language is object
except Primitives. So all Non-primitive data types are Objects.
Object – In English language, the meaning of “Object” is a
material thing where you can sense it by a touch or a vision. The same meaning
applies for Object in Java language as well. But the Object in Java is virtual,
neither we can touch it nor see it..! Every object in real time will have a
state and behavior. In the same way, the objects in Java will also have state
and behavior. The following explanation will give you clear idea.
Class – Class is nothing but a blue print of an object. We
define all the properties and characteristics of an object in the class.
Reference – We use reference to point to a particular
object. Reference variables are used to store the memory address of objects.
The following real-time example will give you more clear
idea about reference, object and Class. (I found an interesting example in
StackOverflow Q&A and felt that will be easiest way to explain).
Consider Sanjay (me..:P) as a Civil Engineer where I design
planning for house constructions. So now I have a plan for constructing a house.
Using this plan I can create as many houses I (or you) want.
So here the plan is
the Class and the house is an object in Java. Plan is a blue print from which we can create number of
houses. In the same way from one Class we
can create number of Objects. That’s the reason we say Class is the blue print of
an object.
Now you and I have built 2 houses from my plan. So your house and my house look SAME, but your house address is different and so as mine. If I want to invite my friend to my home, I
give him my house address so that he won’t get confused even both the houses
look same. Here, the address is the reference
in Java. To store the address of an object or to point to an object, we create
a reference so that JVM can easily
identify the object.
We create objects using the following syntax,
From the above line I am telling JVM,
House houseObj = new House();
From the above line I am telling JVM,
- to build a house (object) for me.
- take the construction plan from Class House for bulding.
- store my house reference (address) into the houseObj variable.
So here the houseObj
type is a user defined Class House which
is a Non-Primitive Data Type.
Sample Java Class looks as following,
public class House
{
// constructor
public House()
{
}
// house properties
private String ownerName;
private String address;
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Hit Points:
- Primitive Data Types are NOT objects. Non-Primitive Data Types are objects.
- All user defined data types come under Non-primitive data types.
- Objects in Java are very similar to the objects in the real world. Every Object will have properties and characteristics.
- Object in java refers to virtual thing which possess a state and behavior.
- Class is the blue print of an object. You can create as many objects you want from a single class.
- Reference is a variable where we store the memory address of an object.
Thanks :-)
0 comments:
Post a Comment