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

Can anybody let me know!

 
Rajeev Nair
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajeev Nair
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey !. I think I should have browsed the API before firing the question.
 
luco zhao
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajeev Nair:
processorA().processorB();

[/QB]

Why not java.lang.NullPointerException ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic