JAVA Beginners part 5

Reference variable like a pointer , it doesn’t hold the object but knows how to access the Object which is in Heap.

example Dog mydog = new Dog();

Note: This reference variable is forever of type Dog.

It is very important to think of above object creation in a 3 step process.

q3

Interesting thing about Reference variables is regardless of the objects they refer to are of same size. They cannot be incremented like in C, example mydog++ wont work.

will discuss this in future, power of final keyword

Experiment with References, You see these stuff are really important for JAVA interview.

Book b = new book();
Book c = new book();

Book d = c;
Above , we are not creating a new object in heap. Rather we are pointing the object of c to d.
What that means is both c and d are pointing to same object.It is like having two remotes for same tv.

c = b;
Here, c is made to point to b. which means c is not pointing to the object it created anymore.
both b and c are pointing to same object.

Reference variable has NULL if it is not pointing to any object.

The active references are very useful to know because they the only ones that remain on Heap, rest will be cleared by garbage collection.

ways to declare Integer array. Same will follow for string as well.Note- Array is an Object.

Int[] test = new int[5];
Int[] test = {1,2,3,4,5};
Int[] test = new Int[]{1,2,3,4,5};

Note, here these are just references that can point to real objects until the values are assigned. Meaning if i say Int[] test = new int[5], there is no object creation. All i have is 5 references. When i assign value , like say test[1]=2;that is when the object is created and refernced.

or

if you want to create an array of objects lets says

Dog[] mydog = new Dog[3];
mydog[0] = new Dog();
mydog[0].name = “jack”;
mydog[0].bark();

test2

some more test programs

test4

State of an Object called as Instance Variable and Behavior of an Object called as method. Both are inter related. For example based on the values of instance variable, you wanna invoke different behavior/method. How? lets see.

Repeating, Class is a Blueprint of an Object. Class tells JVM how to create an Object of this type.In essence

test7

When you create each Object of above class, Lets A and B, then you would like to invoke a particular method considering the variables of A and B.

What does that mean, lets Say A is artist Madona and the song title is Frozen and B is Beyonce and the song title is Single Ladies, so when you play you would want the play method to play Frozen for Madona and Single Ladies for Beyonce. That is how you establish the connection between state and behavior.

Note: Even though all Objects have access to same methods, methods behave differently based on the instance variables.

example

test5.jpg

next up the line is, The Method can consume values. Meaning you can tell your Dog how many times to bark. We do that using parameters.

Parameters is what the method takes, argument is what the caller passes. Parameter is nothing more than a variable.

Methods can be used to re turn values example, instead of using void ( which means no return) use the type that you would like to return.

 

int key()
{
return 2;
}

A method can have more than one parameter. Note the order and type of parameters is important.

Here is powerful concept, JAVA is pass by value. Which means every time we are passing the value from one variable to other or method to method etc.

j2.jpg

 

 

next page 101

More in next part.

References:

Head First Java 2nd Edition

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s