Y
Yichao
asked

Expert
Abhay Jajodia answered
Hi Yichao,
In Java, length for an array is a field (a value stored in the array), not a method. That is why you write it without parentheses:
int[] nums = {1, 2, 3};
System.out.println(nums.length); // 3
Parentheses are only used for methods. For example, strings use a method:
String s = "abc";
System.out.println(s.length()); // 3
So the simple rule is: arrays use length, strings use length().
If you have more questions, I am here to help π
Java
This question was asked as part of the Learn Java Basics course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
