Java - Class, Object, String
Understanding Class, Object:
Concept -
Consider the following analogy: A class is a blueprint to make objects. For example, suppose you have a class House. The class House is the blueprint that explains exactly how to make a specific House object.
Your own house, or your sister's house, are instances of class House - they are the concrete House objects that are made using the blueprint.
So, a class is like a recipe, or a set of instructions, or a plan, that explains how to make a specific kind of object.
Your own house, or your sister's house, are instances of class House - they are the concrete House objects that are made using the blueprint.
So, a class is like a recipe, or a set of instructions, or a plan, that explains how to make a specific kind of object.
or you can think of a class as a 'Reference' for making the 'real' objects!
Every time you look at the reference/guide before you create an object in existence as it serves you the basic information of
1. What actually constitutes an object? Other way, what will the object be having with it? If it is a House object, it would be having doors, windows etc., If it is a Person object, it would be having eyes,ears, nose etc., -- They are called as 'members' of the class (or an object really)
2. They also convey the quantity/volume -- how many doors, windows a 'house' object should have? --- represented by the 'datatypes' like String, int etc.,
3. What are the objects supposed to do? -- In case of a house object, it does NOT seem to do any real activity except staying/lying as it is. But in case of a person object, it is supposed to do many actions like 'breathe()', 'eat()', 'sleep()', 'speak()', 'run()' etc., --- they are called as 'methods' or 'behaviors'.
All these information are present in the class. So you look at your 'class' or the 'reference' or 'template' or a 'blueprint' each time before you create an object.
Moreover, being a 'reference' a class does NOT exist in reality whereas an 'object' is the one which is existing in reality. It is alive!!
Every time you look at the reference/guide before you create an object in existence as it serves you the basic information of
1. What actually constitutes an object? Other way, what will the object be having with it? If it is a House object, it would be having doors, windows etc., If it is a Person object, it would be having eyes,ears, nose etc., -- They are called as 'members' of the class (or an object really)
2. They also convey the quantity/volume -- how many doors, windows a 'house' object should have? --- represented by the 'datatypes' like String, int etc.,
3. What are the objects supposed to do? -- In case of a house object, it does NOT seem to do any real activity except staying/lying as it is. But in case of a person object, it is supposed to do many actions like 'breathe()', 'eat()', 'sleep()', 'speak()', 'run()' etc., --- they are called as 'methods' or 'behaviors'.
All these information are present in the class. So you look at your 'class' or the 'reference' or 'template' or a 'blueprint' each time before you create an object.
Moreover, being a 'reference' a class does NOT exist in reality whereas an 'object' is the one which is existing in reality. It is alive!!
Implementation -
A class is a type definition and from it one can create many objects of that type. Say you want to define a type called Human. In its simplest form it would look like this,
class Human {
class Human {
}
From it you could create objects like,
Human me = new Human();
Human you = new Human();
Human someoneElse = new Human();
Strings and String Literal Pool
Strings are immutable: String objects are immutable objects i.e. a string object cannot be changed once it is created. For example in the below code string object is not changed instead a new string object is created on concatenation. So whenever some operations are performed on the String objects a new string object is created.
String s = "Java ";
s = s.concat("Blog");
String Literal Pool: Whenever a class is loaded, java virtual machine checks for all the string literals in the class and for all the literals it finds whether they are already on the heap, if not a new instance is created on the heap and a reference is stored in the string literal pool. Subsequently any reference to that string literal in the program is replaced with the reference from the string literal pool.
String s1 = "Blog";
String s2 = "Blog";
1. s1.equals(s2) : true
2. s1==s2 : true
the first statement returns true and so the second statement because the second time a new string object is not created instead it is referenced from the String literal pool.
But, in the below code
String s1 = "Blog";
String s2 = new String("Blog");
1. s1.equals(s2) : true
2. s1 == s2 : false
second statement returned false because java virtual machine is bound to create a new string object no matter it was there in the String Literal Pool. So, avoid using new when creating a string literal otherwise new string objects are created unnecessarily.
Comments
Post a Comment