Which fix removes the bug?
String name = "Ada"; System.out.println(name.length); // compile error
- Call it as a method:
name.length() - Use the size field:
name.size - Cast first:
((Object) name).length - Use
String.length(name)instead
For a String, length is a method, not a field. Arrays have a length field, which is why the two get mixed up.
Play Problem Solving ▸