wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jtips Question 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 "Jtips Question" Watch "Jtips Question" New topic
Author

Jtips Question

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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

Arathi<br />Sun Certified Java Programmer
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Dec 06, 2001
Posts: 24
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

Joined: Dec 18, 2001
Posts: 417
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]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtips Question
 
Similar Threads
Jtips Question
urgent code problem!!!!
jtips mock exam
marcus green mock exam
Can anybody let me know!