user5161233
user5161233

Reputation:

Java: When to use parenthesis after a method

When I use the method .length() I sometimes must use parenthesis and sometimes not. What is the rule for using this? For example:

        int[] secArray = {1, 2, 3};
    System.out.println(secArray[2]);
    System.out.println(secArray.length);

While here, I must use it: (capLetter is a string)

        System.out.println("Your name consists of " + capLetter.length() + " letters.");

I could not find any question nor answer about this. I am a beginner in Java, sorry if the question is ridiculous. Thanks in advance!

Upvotes: 1

Views: 3933

Answers (4)

yunus emre bey
yunus emre bey

Reputation: 1

We can approach the question under two headings. We can think of it as a length expression in Arrays and a length expression in Strings.

The point we need to focus on in the Array length statement is that you divide 1 notebook into 3 parts, and these parts can be divided into other parts in themselves. For this, we are trying to get the parts of the main part of this notebook when using length with Array.

that is, consider 3 subgroups of 1 team, we take the 2nd part of this group as an array, and we determine how many parts the second part consists of and gives us the result. As in the example below with 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("#People in second team: " + teams[1].length);

Note: A one dimensional array has a length field that holds the number of elements in the array. A two dimensional array, however, has multiple length field. It has a length field that holds the number of rows, and then each row has a length field that holds the number of column.

//#People in second team: 4

When using length in strings, we put an empty parenthesis () in order to determine how many characters the content defined to us consists of. Let's share an example again;

String S1 = "Hello Java String Method”;

int length = S1.length();

    System.out.println("Length of a String is: " + length);

//Length of a String is: 24

I hope i could help you:) it will also be my first comment on this platform.

Upvotes: 0

user5022341
user5022341

Reputation:

length() /*method*/     .length /*field*/

length() is a function or method which returns the length of an object, is a field which also gives you the length. It is important to understand the difference because sometimes you will encounter situations where you have to define an object's length. In this case if you use length() you may get an error saying it requires a variable but if you use .length it will work fine.

Upvotes: 1

tarikfasun
tarikfasun

Reputation: 324

private Array() {}

  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**

// int[] x = {length} //@param length the length of the new array but

length() is a method

Upvotes: 1

yxre
yxre

Reputation: 3704

It depends on whether the object is implementing length as a field or a method.

Array.length is a field.

String.length() is a method

List.size() is a method

In java, you will see methods more often than fields. Arrays are a special case since they are part of core java.

Upvotes: 6

Related Questions