Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP Brainteaser (1)

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
toukhir mujawar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the output shouls be like....
Hello world!

because JAVA is case sensative... and the class is Null not null...
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dinu Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out put is hello world!
 
Vishal K Patel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try replacing "(Null)Null" with "(Null)null".
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congrats Valentin. It's going to be super-hit or it is already.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic