aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anybody let me know! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anybody let me know!" Watch "Can anybody let me know!" New topic
Author

Can anybody let me know!

Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
Please refer to the code below
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();
}
}
I thought it will print "false is a boolean operator"
but it prints "true is a boolean operator" Why??.
Thanks in advance


Raj<br />Sun Certified Java Programmer
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Check out the API spec for String. If you look at the description for the startsWith method, you'll see this:

Returns:
true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.

I hope that answers your question,
Corey


SCJP Tipline, etc.
Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
Thanks Corey !. I think I should have browsed the API before firing the question.
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
Originally posted by Rajeev Nair:
processorA().processorB();

[/QB]

Why not java.lang.NullPointerException ?


Great thanks,<br />Luco Zhao
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When you're invoking a static method through an object reference, the JVM doesn't inspect the reference to see what it references, it only looks at its type and then invokes the static method for that class. Take a look at this section of the JLS: §15.12.4.6 Example: Target Reference and Static Methods. It has an example that illustrates this exact point.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anybody let me know!