Sunday, November 8, 2009

Java Certification SCJP : String

Here is common question about String.


Exercise :

public class test {

public static void main(String args) {
String s = "toto";
System.out.println("Size of s:" + s.length);
}
}


What is the result ? (choose one)

A Size of s:4
B Size of s:3
C Compilation fails
D An exception is thrown at runtime














Solution :

The answer is C because String have a method that's call length() but it has no attribute length.
Note that it's really easy to be a mistake here !