1

I've been trying to get some streams working via TCP however I seem to fail, probably due to the fact that I don't understand byte streams enough.

I know what a byte it, it's 8-bit. example: 0000 0001 (which would be "int 1")

When i define let's say:

Byte[] myByte = new byte[1];

What does the "1" do? Is myByte capable of carrying only one byte?

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • 4
    Are you asking what an array is? Because the [Arrays](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) tutorial is pretty clear what the `1` is. – Sotirios Delimanolis Mar 17 '14 at 00:28
  • You are just making an array with `byte[] myByte = new byte[1];` part of the code. – wns349 Mar 17 '14 at 00:29
  • Nono I know very well what an Array is, so your saying that the number defines how many bytes the byte[] may carry? – basickarl Mar 17 '14 at 00:29
  • @SotiriosDelimanolis Ah alright, I was looking at Byte tutorials, not the Array one, think you just answered my question :) – basickarl Mar 17 '14 at 00:30

1 Answers1

2

new type[x] is the syntax for an expression creating an array [object] (of type type[]1) with x elements.

See the Nuts & Bolts: Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array [object] is created (i.e. new byte[1]). After creation, its length is fixed ..

So, new byte[1] creates an array [object] for a single byte (length = 1), and new byte[1024] creates an array of 1024 byte elements (length = 1024).


1 The code in the post is a bit "funny" because it uses Byte[] as the array type but new byte[1] to create the actual array object; it should be byte in both places. I'm ignoring that as a typo because automatic boxing of a primitive array is not supported in Java.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220