Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does new Object[]{} in Java means?

Tags:

java

In Java you are able to do the following:

new Object[] { /* parameters separated by comma */};

Indeed, this is used in the prepared statements of the Spring framework. Eg:

getJdbcTemplate().queryForList(
   "DELETE FROM foo WHERE id = ?", //the "?" mark will be substituted by "3"
   new Object[] { 3 }, //What kind of magic is this?
   String.class //Irrelevant in this example
);
  • How is this called?
  • What is going on there?
  • How could you access that parameters?
like image 343
eversor Avatar asked Nov 18 '13 17:11

eversor


1 Answers

Object[] objs = new Object[]{3,4};

is the same as:

Object[] objs = new Object[2];
objs[0] = 3;
objs[1] = 4;

So you access it as objs[0];

like image 161
Deniz Avatar answered Oct 14 '22 01:10

Deniz