aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (1) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Brainteaser (1)" Watch "SCJP Brainteaser (1)" New topic
Author

SCJP Brainteaser (1)

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi all,

I'd like to start a series of Java brain teasers which would ask a question about a tricky Java code excerpt. I will probably publish one/two teasers a week and they could serve as thinking material for preparing for the SCJP exam. After a couple days, I will provide the right answer as well as a pointer to the correct section in the JLS handling the subject. I hope you'll find this exercise useful. As an example, please find below the first one:

What output is printed out when the following code is run?

[ October 27, 2006: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70
the output shouls be like....
Hello world!

because JAVA is case sensative... and the class is Null not null...
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
Good one Valentin. Since the method 'greet' is static, output will be "Hello world", if we remove the 'static' keyword, we will get a NullPointerException.
[ October 27, 2006: Message edited by: Rajesh Kadle ]

-Raj
Dinu Kumar
Greenhorn

Joined: Oct 27, 2006
Posts: 1
Out put is hello world!
Vishal K Patel
Ranch Hand

Joined: Oct 20, 2006
Posts: 43
In the above code....
compiler will complains because....we are using "null" to call the method greet().

"null" is a reserved word in JAVA....and we cant use as a literal or instance name......

If i m wrong....please correct me.....
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70
hey... i tried this....

public class Null {
public static void greet() {
System.out.println("Hello world!");
}
public static void main(String[] args) {
((Null)Null).greet();
}
}


that is made null to Null and the compiler error message was....

Null.java:6: cannot find symbol
symbol : variable Null
location: class Null
((Null)Null).greet();
^
1 error


can anyone explain this.......
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Try replacing "(Null)Null" with "(Null)null".


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Java language also allows you to use an object reference to access a static member.
When you access a static member on an object reference, what the value of the object reference is (point to an instance or null) ignored, only the type of the object reference matters.
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Java language also allows you to use an object reference to access a static member.

Is there any legitimate reason for writing code that uses this syntax? (Other than for writing tricky Java brainteasers, of course!) At first glance, it seems that it would always be clearer to access static members using the class name.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
This is bad or at least poor form because it creates the impression that some instance variables in the object are used, but this isn't the case.
It's best practice to always invoke static member using the class name rather then an object reference of the class.
[ October 27, 2006: Message edited by: wise owen ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
All right, guys, thanks for participating !

The correct answer was that "Hello World!" is getting printed out (without the quotes ).

The relevant sections of the JLS are 15.12.4.1 Compute Target Reference (If Necessary) and 15.12.4.6 Example: Target Reference and Static Methods.

As wise owen correctly pointed out, it is bad practice to invoke a static method on a reference variable. As a good practice, the declaring class name should be used instead. Also, the fact that the class was called Null is not a problem as such.

Thanks to those who participated. Stay tuned...
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Congrats Valentin. It's going to be super-hit or it is already.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SCJP Brainteaser (1)