Forums Register Login

byte[] buffer or byte buffer[]

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
This is a beginner question; the answer is "no". Either works. The "byte[]" form is preferred.

Moving to Java in General (Beginner).
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Haha, awesome, thanks for your help. Yet another reason why the first book I was using was awful.
+Pie Number of slices to send: Send
I think this is no different.
Just a custom of coding.
Maybe after compiled,the both are same
+Pie Number of slices to send: Send
 

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.
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1362 times.
Similar Threads
Accessing image in a zip file
CheckSum implemantation details for a file
HttpServletResponse.getOutputStream() - streaming or not streaming?
writing file to OutputStream
Get File from Server as Byte Array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:14:40.