Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Ibthaj Mansour   on Jun 21 In Java Category.

  
Question Answered By: Saxon Anderson   on Jun 21

> (1) what are these brackets  for,

They indicate that you want an array of that type of object.

Contrast string  x and String []y.
x can only hold one string. y can hold n strings, where n is set when
you initialize the array.

> (2) do you ever put  anything inside  them,

Yes. just like when you initialize a single object, you need to
initialize arrays to a given size.

so, using the examples above,

x = new String(); // Allocates space  for 1 string and sets it to ""
y = new String[5]; // Allocates space for 5 strings. Does not set any
of them

> (3) do they only go after the word  "String",

No, you can have an array of any type, including the fundamental types
(int, char, double,...)

> and (4) are you supposed to have a space before the [] or not?

Coder's preference. The Java compiler ignores whitespace. Use whatever
looks good to you.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Confused about brackets (when used with String command) Or get search suggestion and latest updates.


Tagged: