String.length() vs Array.length [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is String.length() a method?
Java - Array's length property
Was there a specific design/performance reason as to why String has a method for length but Array has a variable length?

There is no Array class in Java (other than in reflections). Arrays are "primitives" of a sort in Java and play by different rules from declared classes.
Certainly a length() method could have been defined on arrays, but the designers wanted to keep length as a property rather than a pseudo-method. (In part this may have made it easier for early Java implementations.) The reasons are somewhat buried in history.
(A better question is why Java couldn't decide whether to call the concept "length", "count", or "size" -- I always end up trying all three before I hit on the right one for an aggregating class.)

Related

Performance impact of vararg in Java [duplicate]

This question already has answers here:
Performance of variable argument methods in Java
(6 answers)
Java's varargs performance
(6 answers)
Closed 5 years ago.
I have few overloaded method, which I can replace with vararg, but these are getting called numerous times.
I just wanted to know its impact. I assume jvm creates an array from the parameters at runtime, so theoretically there must be some impact, not sure practically will it impact or not!
The one and only answer that makes sense here: go and measure yourself. Yes, you are correct - varargs are syntactic sugar - and the compiler creates arrays under the hood. So, yes - there is a certain performance impact.
But if that penalty really matters to you depends solely on your requirements and your context.
I think (opinion) here: when this really impacts the perceived performance of your application - then you probably have other problems already.

What is the difference in using .length and .length() to find length [duplicate]

This question already has answers here:
How can I get the size of an array, a Collection, or a String in Java?
(3 answers)
Closed 8 years ago.
The former (.length) isn't even a function , how does it return length?
The reason length isn't function in an array is that it is a field. In particular, a public final one as specified by the Java Language Specification section 10.7.
While ArrayList (ad Vector of the 1.0 libraries) have a length() method, the key thing to realize is that this value can change. You call add(Object o) on a Vector, and you've changed its length.
On the other hand, you can't change the length of an array.
Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.
(from JLS Section 10.2 - Array Variables)
And thus, the simplest thing that works is to make the length a field. One that is public and can't change.
Realize in the Java 1.0 days, HotSpot wasn't quite as advanced as it is today. It wouldn't be able to do all the optimizations that it can now with inlining methods. And thus, to make array access that much faster for situations that needed it, its a field.
Related reading on Stack Overflow: length and length() in java

Object[] variableName; vs Object variableName[]; [duplicate]

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 9 years ago.
Working in Java, what is the difference between using
Object[] variableName;
and using:
Object variableName[];
Does it have the exact same effect on compilation and run? Or is there a difference?
Both statements are entirely equivalent.
The statements will compile to the same code, BUT if you write
Type[] name instead of Type name[] the code becomes more readable, because you always can see the type (Array or Not-Array) in front of the variable name. (In fact this is some kind of my ppersonal meaning)
From Java language specification (for Java 7) :
The [] may appear as part of the type at the beginning of the
declaration, or as part of the declarator for a particular variable,
or both.
So yes, they are both equivalent and you can even mix the two styles in the same declaration (although the specification gives a healthy reminder to us that that tends to get ugly and confusing).

Why isn't .length() a method for arrays in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java - Array’s length property
String.length() vs Array.length
I'm currently in my AP Computer Science class in high school and I came across this in my reading.
From what I understand, .length() is a method used for strings, but why isn't .length() a method when applied on arrays? I understand that they're different objects, but why didn't Java just make another method for finding the length of arrays?
I appreciate any response I get. Thanks!
Since arrays are fixed length defined at the time they are instantiated length is a public final field on the class. There is no need to make it a method since there is no calculation to be done at run time.
See this section of the Java Spec for details:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7
Now, as for the design question of why they didn't provide an accessor method to obtain the value isn't specified. Perhaps this was done before any other convention was set and this is just a legacy thing. Only the language designers would know the "why" portion of their decision to do it this way.
Arrays are defined in the Java Language Specification #10.7. In particular:
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
[...]
I can't answer why this approach was chosen by the language designers.
Interestingly, it was already the case in the Oak specifications, which is the ancestor of Java.
I doubt that there's a good technical reason for this.
I suspect that this is one of those little inconsistencies that didn't get spotted early enough to get fixed without breaking a ton of code.

"..." being used in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I pass an array as arguments to a method with variable arguments in Java?
What is … in a method signature
I first saw this when I was modding Minecraft. It had a constructor that specified (String ... line), and thought it was just some shorthand that Mojang had created. But now, I was looking over ProcessBuider, and saw it again. I was wondering what this is used for. My best guess is that it allows developers to add as many of that type of object as they want. But if that's the case, why not just use an Array or List?
So, really, I am asking two questions:
What is the "..." operator, and
Why would it be more useful than using an Array or List?
... indicates a multiple argument list to a variadic function: a function that can take a variable number of arguments.
For an example of this, look at PrintStream.format. The first (required) argument is a format String, and the remaining 0 or more arguments fulfill that format.
It is called varargs, and as you say it is used to be able to let a method be called with any number of arguments of the specified type. It was introduced in Java 5.
You can read more in the Java tutorials - Varargs.
This is equivalent to a String[] line. It is Java's equivalent to the varargs keyword in C/C++. Similar to C/C++ it must appear as the last parameter.
You've already answered question #1 yourself. As to why it's more useful, it's just a shorthand that requires less typing.
To answer your second question, one advantage of varargs is that you can call a function taking varargs parameter without passing that param. Whereas instead if your function takes in an array, and you need to call it without any value, the caller needs to explicitly pass null.

Categories

Resources