But it can be accomplished by using the java.lang.reflect and set the accessible flag for the String object.
Here is how:
import java.lang.reflect.Field;
public class GetStringLength{
public static void main(String[] args)
throws Exception{
String t="test";
Field f=String.class.getDeclaredField("value");
f.setAccessible(true);
System.out.println(((char[])f.get(t)).length);
}
}
The output:
4 BUILD SUCCESSFUL (total time: 0 seconds)As you can see no java.lang.String methods were used to get the length.
No comments:
Post a Comment