aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers + Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers + Overloading" Watch "Wrappers + Overloading" New topic
Author

Wrappers + Overloading

Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

I'll get straight to my questions

After going through Ch-3, I happened to think of the following.

Date d;
and
Date d = null;

What is the difference between an uninitialized and nulled reference (the only one I could think of is how the garbage collector treats them)?

And in overloading, it is said that when an exact match is not found, the JVM uses the method with the smallest argument wider than the parameter. Can someone please explain with an example?


OCJP 6
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Pritish Chakraborty wrote:I'll get straight to my questions

After going through Ch-3, I happened to think of the following.

Date d;
and
Date d = null;

What is the difference between an uninitialized and nulled reference (the only one I could think of is how the garbage collector treats them)?

And in overloading, it is said that when an exact match is not found, the JVM uses the method with the smallest argument wider than the parameter. Can someone please explain with an example?


consider the following scenario;

public void play(int a, int b) {}

public void play(long a, long b){}


now consider this method call:

byte x = 9;
byte y =8;
play(x,y); // this will call play with int arguments, because that is the method with the smallest argument wider than the parameters viz wider than byte , byte.


regarding to your first question if the Date d is an instance variable then there is no difference between Date d; and Date d = null;

if it is a local variable then if you write Date d; you are just declaring the variable d of the type Date. you are not initializing it. if you try to use it you will get a compiler error.

when you do Date d = null; you have initialized the variable to null ,which is nothing. d is not pointing to any actual Date object. however it won't give you any compiler error.

Garbage collector won't make any difference. Garbage collector is to save the memory by blowing objects on heap. none of the statements Date d; and Date d = null; produce any objects so no role of garbage collector.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

So it depends on scope of the variable....of course, this was referred to many times in the chapter itself!

The overloading part is clear as well, thanks!

Also, I meant that Date d = null; makes a difference on the garbage collector if d were previously referring to an object...silly, I know! lol
chain singh
Ranch Hand

Joined: Feb 28, 2012
Posts: 117
JVM treat 'NULL' as a value ,so if you assign to any local reference variable it will not give any compiler error unless you use this reference variable.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Pritish Chakraborty wrote: I meant that Date d = null; makes a difference on the garbage collector if d were previously referring to an object...

what could be the answer and how will you prove if i say yes or no
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

Seetharaman Venkatasamy wrote:
Pritish Chakraborty wrote: I meant that Date d = null; makes a difference on the garbage collector if d were previously referring to an object...

what could be the answer and how will you prove if i say yes or no



Also, "Date d = null;" is a declaration. There is no way for d to refer to a previous object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Right, of course...I don't know what I was thinking xD.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrappers + Overloading
 
Similar Threads
Polymorphism
overriding
Threads
public static void main(String args)
Constructor question