Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

byte[] buffer or byte buffer[]  RSS feed

 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference in the way buffer variable is declared here?
1) byte[] buffer=new byte[4096];

and

2)byte buffer[]=new byte[4096];


Thanks,

Vinod
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a beginner question; the answer is "no". Either works. The "byte[]" form is preferred.

Moving to Java in General (Beginner).
 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,

I used the second approach(byte buffer[]). Iam writing this in a template which contains xml and java(used as scripting).It throws a BSFException.

But if i use the first approach(byte[] buffer) it runs successfully.
So i thought there may be some difference in them.

Thanks
[ July 13, 2007: Message edited by: vinod awar ]
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along the same lines, I have seen all of these used at various points in time
so far:

(String[] args)
(String [] args)
(String args[])

And as far as I can see they all run successfully and in fact I can't tell a difference. Is there a rule about when/where the brackets need to be placed in general? If not, what's the difference?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the "C++" language -- whose syntax Java borrows from, heavily -- the brackets always follow the variable name.

int counts[];

The problem with this notation is that it doesn't read well. "int counts array -- oh, an array of int named counts."

Java introduced a variation:

int[] counts;

which reads better -- "int array counts". The two words that describe the type are together, so it's clearer.

Java also allows, for backwards compatibility only, the C++ syntax, with the braces after the variable name. But braces-first is intended as a superior replacement.

As for the space between the type name and the braces: it's optional, as is most whitespace in Java. I personally often omit it -- int[] as oppsed to int [] -- but you should use whatever style suite you or your workplace.

There's actually another reason why the Java version is better: if you declare more than one variable on a line, it does the right thing. In Java, you can say

int[] counts, sizes;

and that means that both "counts" and "sizes" are int arrays. In the C++ syntax, you can say

int counts[], sizes;

and while "counts" is an int array, sizes is not! This is really scary ugly, and you should never do it.
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, awesome, thanks for your help. Yet another reason why the first book I was using was awful.
 
Shoumin Li
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is no different.
Just a custom of coding.
Maybe after compiled,the both are same
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

int counts[], sizes;

and while "counts" is an int array, sizes is not! This is really scary ugly, and you should never do it.
What about this?That is even scarier, and even more confusing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!