简体   繁体   中英

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

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

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";

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM