pulic class J_Test
{
public static void main(String args[])
{
String s="string";
int a=s.length();
int b=s.length;
char c=s[3];
s+=3;
}
}
哪里错了?
{
public static void main(String args[])
{
String s="string";
int a=s.length();
int b=s.length;
char c=s[3];
s+=3;
}
}
哪里错了?
public static void main(String args[])
{
String s="string";
int a=s.length();
int b=s.length();
char c=s.charAt(3);
s+=3;
}
}
char c=s[3]; The type of the expression must be an array type but it resolved to String
s是一个字符串,而String类型没有length方法的。只有length()方法
还有就是char c=[3]; 这里s是一个字符串不是祖父数组,当然可以先转化为子符数组:如下
char []c=s.toCharArray();
char b=c[3];
这时int b=c.length;才是正确的。length不带括号表示一个数组的长度,而带括号表示字符串的长度。length是一个property,而length()是一个成员函数。这样解释
例如你定义了一个数组
String[] suzu = new String(3);
suzu.length = 3 //这个是数组的长度;
suzu[0].length()//这个就是一个对象。它只能调用了String这个类的方法返回的是这个字符串的长度