• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output of code snippet  RSS feed

 
Ann Sebastian
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body tell me why the output of the following code snippet is "ic"

Code Snippet:
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));

Why isn't it raising an exception since char is present as second argument
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1: Can any body tell me why the output of the following code snippet is "ic"

Answer: Because string indexes start at 0

Q2: Why isn't it raising an exception since char is present as second argument

Answer: Because char is a numeric type, and will be interpreted here as an unsigned 16-bit integer
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!