Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jtips Question

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output when we compile and run the program?
public class Q1 {
static void processorB() {
System.out.println(" is a boolean Operator.");
}
static Q1 processorA(){
Q1 q=null;
if (("java").startsWith("null"))
return q;
else
return null;
}
static {
System.out.print("Java".startsWith(""));
}
public static void main(String[] args) {
processorA().processorB();
}
}
The answer is true boolean is operator.
My first question is how come System.out.print("Java".startsWith("")). will be true. The string is empty string right?
Second question is the processorA method will return null, so how the null.processorB() is valid. Am not getting it. Please explain
Thanks in advance
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arathi,
The first answer is because startsWith defaults to true. It tries to see if the given prefix is not found in the string. Since you gave it an empty string it won't try to see if it is found in the string but will return the default value: true. The code is below.

As you can see, the code will skip over the if block because it will be false and the while loop will never execute.
The second answer relates to the processorB method being static. The JVM doesn't require an object to call the processor. Since it doesn't need an object we can use null to call it because it is ignored by the JVM anyway. In other words we could have just done the following:
processorA();
processorB();
Regards,
Manfred.
 
Vinny Chun
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfred, your explanation is billiant. However, I have never thought that I could call a static method just like the example! Do we really need to know these kinds of tricky method calling in the exam? I am very nervous!
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the syntax processorA().processorB(); make any sense ?
well it compiles ok though.
But for methods we always use as class_name.method() or in case of static method we could use simply the call the method like
processorB();
but the syntax processorA().processorB() looks pretty
weird , particularly when it actually evaluates to
null.processorB(). does this make any sense ?

Originally posted by Manfred Leonhardt:
Hi Arathi,
The first answer is because startsWith defaults to true. It tries to see if the given prefix is not found in the string. Since you gave it an empty string it won't try to see if it is found in the string but will return the default value: [b]true
. The code is below.

As you can see, the code will skip over the if block because it will be false and the while loop will never execute.
The second answer relates to the processorB method being static. The JVM doesn't require an object to call the processor. Since it doesn't need an object we can use null to call it because it is ignored by the JVM anyway. In other words we could have just done the following:
processorA();
processorB();
Regards,
Manfred.[/B]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic