Can we use length() method to get the size of array?
class Array14
{
public static void main(String[] args)
{
int[] x={10,20,30,40,50};
System.out.println(x.length());
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- In the above program, length() method is used which applicable for string object to
calculate the number of characters present in the string but it cannot be used with
arrays. To find the size/length of an array we should use length variable.
Ex:- System.out.println(x.length);
*/
{
public static void main(String[] args)
{
int[] x={10,20,30,40,50};
System.out.println(x.length());
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- In the above program, length() method is used which applicable for string object to
calculate the number of characters present in the string but it cannot be used with
arrays. To find the size/length of an array we should use length variable.
Ex:- System.out.println(x.length);
*/
0 comments: