Java:何时在方法后使用括号
Java: When to use parenthesis after a method
当我使用 .length() 方法时,我有时必须使用括号,有时则不需要。使用这个的规则是什么?例如:
int[] secArray = {1, 2, 3};
System.out.println(secArray[2]);
System.out.println(secArray.length);
在这里,我必须使用它:(capLetter是一个字符串)
System.out.println("Your name consists of " + capLetter.length() + " letters.");
我找不到关于此的任何问题或答案。我是 Java 的初学者,如果这个问题很荒谬,我深表歉意。提前致谢!
这取决于对象是将长度实现为字段还是方法。
Array.length
是一个字段。
String.length()
是一个方法
List.size()
是一个方法
在java中,您会比字段更频繁地看到方法。数组是一种特殊情况,因为它们是核心 java 的一部分。
私有数组() {}
Creates a new array with the specified component type and
length.
* Invoking this method is equivalent to creating an array
* as follows:
* <blockquote>
* <pre>
**** int[] x = {length};***
* Array.newInstance(componentType, x);
* </pre>
* </blockquote>
*
* <p>The number of dimensions of the new array must not
* exceed 255.
*
* @param componentType the {@code Class} object representing the
* component type of the new array
*** @param length the length of the new array**
// 整数[] x = {长度}
//@param length 新数组的长度
但是
length() 是一种方法
length() /*method*/ .length /*field*/
length() 是 returns 对象长度的函数或方法,
是一个也给你长度的字段。
了解差异很重要,因为有时您会遇到必须定义对象长度的情况。
在这种情况下,如果您使用 length() ,您可能会收到一条错误消息,指出它需要一个变量,但如果您使用 .length ,它将正常工作。
我们可以在两个标题下处理这个问题。我们可以把它看作是Arrays中的长度表达式和Strings中的长度表达式。
Array length语句中需要注意的一点是,你把1本notebook分成3份,这些份本身又可以分成其他份。为此,我们试图在将长度与数组一起使用时获取本笔记本主要部分的部分。
也就是说,考虑1个团队的3个子组,我们将这个组的第2部分作为一个数组,我们确定第二部分由多少部分组成并给出结果。如下面的 With Dimension Arrays;
示例
String[][] teams = new String[3][4];
teams[0][0] = "Mike";
teams[0][1] = "Tonny";
teams[0][2] = "Smith";
teams[0][3] = "Evan";
teams[1][0] = "David";
teams[1][1] = "Emmy";
teams[1][2] = "John";
teams[1][3] = "Ryan";
teams[2][0] = "David";
teams[2][1] = "Emmy";
teams[2][2] = "John";
teams[2][3] = "Ryan";
System.out.println("#第二队中的人:" + teams[1].length);
注:
一维数组有一个长度字段,用于保存数组中元素的数量。
然而,二维数组具有多个长度字段。它有一个包含行数的长度字段,然后每一行都有一个包含列数的长度字段。
//#二队人数:4
在字符串中使用length时,我们放了一个空括号()来确定给我们定义的内容由多少个字符组成。再分享一个例子;
String S1 = "你好 Java 字符串方法";
int length = S1.length();
System.out.println("Length of a String is: " + length);
//字符串的长度为:24
希望能帮到你:)这也是我在这个平台上的第一条评论。
当我使用 .length() 方法时,我有时必须使用括号,有时则不需要。使用这个的规则是什么?例如:
int[] secArray = {1, 2, 3};
System.out.println(secArray[2]);
System.out.println(secArray.length);
在这里,我必须使用它:(capLetter是一个字符串)
System.out.println("Your name consists of " + capLetter.length() + " letters.");
我找不到关于此的任何问题或答案。我是 Java 的初学者,如果这个问题很荒谬,我深表歉意。提前致谢!
这取决于对象是将长度实现为字段还是方法。
Array.length
是一个字段。
String.length()
是一个方法
List.size()
是一个方法
在java中,您会比字段更频繁地看到方法。数组是一种特殊情况,因为它们是核心 java 的一部分。
私有数组() {}
Creates a new array with the specified component type and
length.
* Invoking this method is equivalent to creating an array
* as follows:
* <blockquote>
* <pre>
**** int[] x = {length};***
* Array.newInstance(componentType, x);
* </pre>
* </blockquote>
*
* <p>The number of dimensions of the new array must not
* exceed 255.
*
* @param componentType the {@code Class} object representing the
* component type of the new array
*** @param length the length of the new array**
// 整数[] x = {长度} //@param length 新数组的长度 但是
length() 是一种方法
length() /*method*/ .length /*field*/
length() 是 returns 对象长度的函数或方法, 是一个也给你长度的字段。 了解差异很重要,因为有时您会遇到必须定义对象长度的情况。 在这种情况下,如果您使用 length() ,您可能会收到一条错误消息,指出它需要一个变量,但如果您使用 .length ,它将正常工作。
我们可以在两个标题下处理这个问题。我们可以把它看作是Arrays中的长度表达式和Strings中的长度表达式。
Array length语句中需要注意的一点是,你把1本notebook分成3份,这些份本身又可以分成其他份。为此,我们试图在将长度与数组一起使用时获取本笔记本主要部分的部分。
也就是说,考虑1个团队的3个子组,我们将这个组的第2部分作为一个数组,我们确定第二部分由多少部分组成并给出结果。如下面的 With Dimension Arrays;
示例String[][] teams = new String[3][4];
teams[0][0] = "Mike";
teams[0][1] = "Tonny";
teams[0][2] = "Smith";
teams[0][3] = "Evan";
teams[1][0] = "David";
teams[1][1] = "Emmy";
teams[1][2] = "John";
teams[1][3] = "Ryan";
teams[2][0] = "David";
teams[2][1] = "Emmy";
teams[2][2] = "John";
teams[2][3] = "Ryan";
System.out.println("#第二队中的人:" + teams[1].length);
注: 一维数组有一个长度字段,用于保存数组中元素的数量。 然而,二维数组具有多个长度字段。它有一个包含行数的长度字段,然后每一行都有一个包含列数的长度字段。
//#二队人数:4
在字符串中使用length时,我们放了一个空括号()来确定给我们定义的内容由多少个字符组成。再分享一个例子;
String S1 = "你好 Java 字符串方法";
int length = S1.length();
System.out.println("Length of a String is: " + length);
//字符串的长度为:24
希望能帮到你:)这也是我在这个平台上的第一条评论。