• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will there be any advantage in these two String array declaration?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

My Question is Suppose I am declaring an String array as

String[] a
or
String a[]
or
Comparable[] a


Will there be any perfomance or other advantage or disadavantage using these syntax?


Thanks
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] a and String a[] is same. It is just matter of representation. 1st is Java way and 2nd is more like C way.

Comparable []a , it is array of an Comparable interface. So you are making an array of elements which implement Comparable. Can be of primitive or object type.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an opinion on the subject?
Have you done any testing to prove/disprove it?

Any reasons for you to think that there might be a performance advantage for using one syntax over the other?
Any reason that you would prefer the first option over the second?
Any reason you would use a Comparable in place of a String ?

I'm deliberately not answering your question directly because you need to be able to think and judge for yourself.

So I'm asking you - what do you THINK is the answer to your own question? And why do you think it is so?

We can then if you wish enter into a long in-depth discussion on their advantages/disadvantages, but you first need to ShowSomeEffort in solving your own problem


 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Post Today 9:26:55 AM Subject: Will there be any advantage in these two String array declaration?
Do you have an opinion on the subject?
Have you done any testing to prove/disprove it?

Any reasons for you to think that there might be a performance advantage for using one syntax over the other?
Any reason that you would prefer the first option over the second?
Any reason you would use a Comparable in place of a String ?

I'm deliberately not answering your question directly because you need to be able to think and judge for yourself.

So I'm asking you - what do you THINK is the answer to your own question? And why do you think it is so?

We can then if you wish enter into a long in-depth discussion on their advantages/disadvantages, but you first need to ShowSomeEffort in solving your own problem


I think Comparable[] a this is like a raw type which is not parameterized but String[] a is parameterized so their might be difference or may be not(That's why I asked questin here)

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:String[] a and String a[] is same. It is just matter of representation. . . .
Not quite. What you are saying in the first form is that you want an array of Strings and the type is Strin‍g‑array.

In the second you are saying you want Strings and by the way make that into an array. That is why the first form is recommended. The second form declares the type as plain simple Stri‍ng.

The second form is one of the vestiges of C syntax. I think in the early days the fact that Java™ syntax looked like C/C++ syntax encouraged people to start using it, but it also causes the problem that people think it means the same as in C.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote: . . .
I think Comparable[] a this is like a raw type which is not parameterized but String[] a is parameterized so their might be difference or may be not(That's why I asked questin here)
Comparable is a raw type because you cannot create arrays of any types which has <> in their names. You can try creating a Comparable<Str‍ing> array and see what the compiler has to say for itself.

Stri‍ng is not a parametrised type. It is Strin‍g not S‍tring<T>. The fact that it implements Comparable<Str‍ing> rather than Comparable is neither here nor there.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote: . . .
Will there be any perfomance or other advantage or disadavantage using these syntax?
. . .
There will be no performance difference.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are saying in the first form is that you want an array of Strings and the type is Strin‍g‑array.

Thanks Campbell.. So there is difference with respect to their meaning or in better words how we explaining it but both are same otherwise.

I tried to check if both have same value or not.It shows both are same:





output:

Hello 1
Hello 1
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is correct.

There is no technical difference at all in placing [] before reference variable or after.
In terms of convention - it is. It suppose to go right after the data type.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better way to check for array equality: this equals method.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote: . . . [] . . . suppose to go right after the data type.
Agree.
I always thought the convention was that the [] is part of the data type. That is why the [] go on the left of the identifier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!