Kevin Florenz Daus
Kevin Florenz Daus

Reputation: 598

Java help -> weird code

Can someone explain to me this code

 new Object[]{"PLease","Help"};  

Ive never seen code like this before,
so It would be helpful if someone explains these to me. Thank you in advance

Upvotes: 4

Views: 415

Answers (3)

hvgotcodes
hvgotcodes

Reputation: 120258

You are creating a new Object array, that has 2 Strings in it, "PLease" and "Help".

The construct you are using is called an anonymous array, because you are not assigning the array to anything (useful if you want to pass the array to a method).

See http://docstore.mik.ua/orelly/java-ent/jnut/ch02_09.htm

Upvotes: 24

Michael J. Lee
Michael J. Lee

Reputation: 12416

It's short hand for a in-line array.

It's the same as doing...

Object[] aArray = new Object[2];
aArray[0] = "PLease";
aArray[1] = "Help";

Upvotes: 5

Simeon
Simeon

Reputation: 7792

This:

new Object[]{"PLease","Help"} ;

Is equivalent to:

Object[] array = new Object[size];
array[0] = "PLease";
array[1] = "Help";

I hope this clears it up a bit.

Upvotes: 3

Related Questions