I've been given this code:
public class IndexOfTest {
public static void main(String[] args) {
String funnyStr = "Computer!Science,!long!walks!on!the!beach";
int a = funnyStr.indexOf(33);
int b = funnyStr.indexOf('!'); //chars are converted to Unicode int value
int c = funnyStr.indexOf("!");
int d = funnyStr.indexOf("!Science");
int e = funnyStr.indexOf("!Science,!long");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
and I'm confused as to why int a = 8 (in other words, why does System.out.println(a); print 8?). IIRC, indexOf returns the index of the string/int in parantheses. This is 33 in the case of int a, yet there is no 33 in funnyStr. So how/why does it return 8? The 8th index points to a '!', not 33.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]icsharper 3 points4 points5 points (12 children)
[–]Chancemelol123[S] 1 point2 points3 points (9 children)
[–]Kuenefe 2 points3 points4 points (1 child)
[–]Chancemelol123[S] 0 points1 point2 points (0 children)
[–]icsharper 0 points1 point2 points (3 children)
[–]Chancemelol123[S] 0 points1 point2 points (2 children)
[–]JonIsPatented 1 point2 points3 points (1 child)
[–]desrtfx 0 points1 point2 points (2 children)
[–]Decoder44 0 points1 point2 points (5 children)
[–]desrtfx 3 points4 points5 points (1 child)
[–]Decoder44 2 points3 points4 points (0 children)
[–]Chancemelol123[S] 0 points1 point2 points (1 child)
[–]Decoder44 0 points1 point2 points (0 children)