Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Confused about brackets (when used with String command)

  Asked By: Ibthaj    Date: Jun 21    Category: Java    Views: 2076
  

I'm a total newbie studying Java and am currently stumped by brackets.

The book I'm reading often shows code that says:

(String[] args)

or

(String [] args)

or

String[] wordListOne = ...

There are a few things I don't get: (1) what are these brackets for,
(2) do you ever put anything inside them, (3) do they only go after
the word "String", and (4) are you supposed to have a space before
the [] or not?

Any help on this would be great. The book I'm using doesn't seem to
explain these brackets--why they're there, what they're used for,
etc. Are they just symbolic of something having to do
with "arguments"? I'm totally clueless.

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Heidi Larson     Answered On: Jun 21

> There are a few things  I don't get:


> (1) what are these brackets  for,

They declare an Array.

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

Sometimes, but not where you have them :)

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

No they can go after any Object (include an Object that you wrote yourself)

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

It doesn't really matter, but my advice is to not put a space after it.
At least untill you are a insane Code Ninja, then you can do what you
are want.

>
> Any help  on this would be great. The book  I'm using doesn't seem to
> explain these brackets--why they're there, what they're used for,
> etc. Are they just symbolic of something having to do
> with "arguments"? I'm totally clueless.

Someone will probably jump on my head for this description but I find
that people understand this idea of it more than other explanations.

Arrays are basically a list of stuff. The stuff you put into it are
Objects (like in your case String Objects).

String[] list;

This tells the Java compiler to get ready to remember a list of Strings
which will be called list.

list = new String[5];

This tells the Java compiler to make the list 5 elements long (from 0 – 4)

Then you could write stuff to the list like

list[0] = “ABC”;

Which would make the item in the very first element equal to the String
“ABC”.

 
Answer #2    Answered By: Nagina Mian     Answered On: Jun 21

> No they can go after any Object (include an Object that you wrote yourself)

or primitate type;

int[] numbers;
numbers = new int[10];

We now have numbers[0] to numbers[9].

 
Answer #3    Answered By: Whitney Cruz     Answered On: Jun 21

Yes :) or primitives :).............

 
Answer #4    Answered By: Asir Hashmi     Answered On: Jun 21

They are telling the compiler that an array is to be used.
If you have int[] matrix = new int[5]; then you're creating
an array of integers with 5 elements. The first int[] set of
brackets says hey I'm an array but I'm not telling you how
big I am. The second set of brackets  in new int[5] says
we want a new object an array of integers that will be 5
integers long. I hope this has helped a little bit.

 
Answer #5    Answered By: Saxon Anderson     Answered 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.

 
Answer #6    Answered By: Geraldine Perkins     Answered On: Jun 21

The code:

methodName(String[] args):

means that an array of strings can be passed to this method.

Often you see this:

main(String[] args):

This the args will get passed the command  line argument as an array of
strings. Take this code:


class ProcessFile{
public static void main(String[] args){
System.out.println(args[0]);
}
}

Now if you typed:

java ProcessFile myFile.txt

You will see

myFile.txt

If you did not put  the [] after String, the method main would think it
was getting just one string. But in fact the main method can take many
strings. So the brackets  indicate that the arguement passed to the
method is an array.

 
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: