• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jtips Question

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]


reply
    Bookmark Topic Watch Topic
  • New Topic