Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java help -> weird code

Tags:

java

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

like image 543
Kevin Florenz Daus Avatar asked Nov 28 '22 06:11

Kevin Florenz Daus


2 Answers

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

like image 78
hvgotcodes Avatar answered Dec 01 '22 00:12

hvgotcodes


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";
like image 23
Michael J. Lee Avatar answered Nov 30 '22 22:11

Michael J. Lee