• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

indexof

 
Roopa Bagur
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain the output of the following code

public class Test035
{
public static void main(String args[])
{
System.out.print(" ".indexOf("") + " " );
System.out.print("".indexOf("") + " " );
System.out.print("Java".indexOf("") + " ");
System.out.println("Java".indexOf("J") );
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roopa,
I am not sure what you are having problems with but the first three indexOf method are looking for the empty string character. The indexOf method will never fail to find a match for an empty string. It will always be the first index of any string!
The last indexOf method call is looking for a capital J which is found in the first index position.
Therefore, the output is four zeroes with a single space between them.
Regards,
Manfred.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic