• 0

[Java] length() vs length


Question

In Java, if you want to know the length of a String, you do:

myString.length()

But for an array, you do:

myArray.length

What is length for arrays? A property like in C#? A public field ?

Thanks.

PS: While we're at it, what's the equivalent of a C# "using" statement in Java? For instance I want to use

int k = java.lang.Math.min(a, b);

And write something like

using java.lang.Math;
int k = min(a, b);

When I write "import java.lang.Math;", I still get "min is undefined", and when I add ".*" at the end (which I don't understand anyway, but I've seen import java.util.*; pretty often), it doesn't work any better.

Edited by Dr_Asik
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

min is not working because it's a static method, it should be used like so:

int k = Math.min(a, b);

the * is used as a short hand to include all sub packages that you're currently using instead of importing them one by one. For example, if you wrote: java.util.*, all sub packages that you are USING will be imported.

EDIT: myArray.length returns the number of elements in the array, and yes, it's a property.

Edited by Ali Koubeissi
Link to comment
Share on other sites

  • 0

There are a couple of differences in the way package/namespace imports are handled in Java and C#.

In Java, types are arranged in are Packages, and you can import types using the "import" keyword:

import java.lang.*;
import java.lang.Math;

In C#, types are arranged in Namespaces, and you can import types using the "using" keyword:

using System.IO;
using Stream = System.IO.Stream;

And as Ali has said, the min() method of the Math type is a static method, not a type. Therefore you can't import the method using the "import" statement.

Link to comment
Share on other sites

  • 0
In Java, if you want to know the length of a String, you do:

myString.length()

But for an array, you do:

myArray.length

What is length for arrays? A property like in C#? A public field ?

  /**
   * The count is the number of characters in the String.
   */
  private final int count = 0;

  /**
   * Returns the length of this string. The length is equal to the number of <a
   * href="Character.html#unicode">Unicode code units</a> in the string.
   * 
   * @return the length of the sequence of characters represented by this
   *		 object.
   */
  public final int length() {
	return this.count;
  }

An array is a simple data structure of Objects with following members:

  • All the members from Object via inheritance, with the exception of clone, that is overridden by a special public clone method.
  • public final int length field that has the number of tokens in the array.

And as Ali has said, the min() method of the Math type is a static method, not a type. Therefore you can't import the method using the "import" statement.

You can use static imports.

package datatypes.list;

import static java.lang.Math.min;
import static java.lang.System.out;

public class example {

  public static final int a = 0x90;
  public static final int b = 0x42;

  public static void main(String[] args) {
	final int k = min(a,b);
	out.print("Minimum of " + a + " and " + b + ") is " + k);
  }
}

Edited by tiagosilva29
Link to comment
Share on other sites

  • 0

Oh, length is a public final field, so in other words, it's read-only. That would be logical since the size of an array cannot change.

As for import, I stumbled on "import static" right after asking the question :p , but thanks anyway.

Link to comment
Share on other sites

  • 0

Just a quick fix:

private final int count;

I used = 0; up there because I was using Eclipse to format the code and had to assing a value because of the error message.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.