• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

indexof

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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") );
}
}
 
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 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.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic