Object[] variableName; vs Object variableName[]; [duplicate] - java

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).

Related

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

What's the meaning of three dots in parameter declaration? [duplicate]

This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 9 years ago.
What do the three dots after "Object" mean in this parameter declaration:
public static int queryCount (
Connection conn, String whereClause,
Object ... params)
throws Exception
In what way does it differ from the parameter declaration Object params ?
Three dots mean that there method can get as parameters as much argument of type Object as it likes. Reading more about "varargs" arguments could be helpful.
This feature was introduced in Java to hide the process of using Arrays as parameters, in form of varargs.
As the documentation states, the process is stil same but complexity has been reduced.
Please note following points:
This allows for entering an array or sequence of type specified.
This form must be used at last in parameters list.
This is not available in older version, so be careful if you plan to deploy to older versions of Java
In short, it's a syntactic sugar for array with restriction that this should be the last parameter in arguments list.
e.g. it's totally legal to declare main method as follows
public static void main(String... args) {}
And another feature of this, this argument is optional, but you still will get an empty array as a value of argument.

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.

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

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.)

What is the difference between int[] x; and int x[];? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between int[] array and int array[]
I was sure that this question is already asked and just wrote the title to find it, but to my surprise it wasn't. I was working on one issue and this question raised. I tried this:
int[] x = new int[1];
int y[] = new int[1];
x=y;
y=x;
and compiler didn't give me an error. So is there any difference between these two declarations?
There is no difference in semantics. Both syntaxes mean the same. Some extract from the JLS §10.2:
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, as in this example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
However, as Voo states in the below comments, there can be some tricky confusion about these declarations when declaring several arrays in a single statement.
There is no difference.
It's just syntactic sugar to make transition from C easier. In Java int[] x is the preferred (and recommended) notation.
None, it's just a matter of taste. Some people prefer to put it alongside the aggregate type (because feel that it is part of the type), others prefer to put it alongside the variable name (because it's closer to the access syntax).

Categories

Resources