12

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?
1
  • 1
    You are initializing the array in the braces ({}). In this instance with an Integer 3.
    – Danny
    Nov 18, 2013 at 17:58

9 Answers 9

22
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];

1
  • 3
    Arrays have no method add. I think you meant to have a list.... Or objs[0]=3; objs[1]=4;
    – Danny
    Nov 18, 2013 at 18:01
4

It's used to initialize an array of Object with a value of 3 at index 0.

3

You asked about this:

  new Object[] { 3 }

As the other answers have said, it is creating and initializing an array of objects that is going to be passed as a parameter to the queryForList method.

The array's actual type will be Object[], its length will be 1, and its first element will be an Integer object ... produced by autoboxing the int value 3.


The rest of your questions don't make much sense to me:

  • How is this called?

It is called "creating and initializing an array"

  • What is going on there?

It is creating and initializing an array

  • How could you access that parameters?

Umm ... as shown in the example? By value? By indexing the array? It is not clear what you are asking.

3

This code can possibly be translate as the following

private void queryforlist(string SQL, object[] parameters, Class c)
{
   //parameters is an array of objects used to replace the ? caracteres in the sql
   //text.

   if (parameters != null)
   {
     //we can access parameters by index inside the method
     for(int i=0; parameters.length; i++)
     {
       if (parameters[i] instanceof String )
       {
         //adding quotes to string for example
         String param = "\""+parameters[i]+"\"";
     
       } else if (parameters[i] instanceof Integer )
       {
         //Note is Integer not int because of AutoBoxing: 
       }
     }
   }
}   

This is useful to accept dynamic datatypes, like Integers, Strings, etc. And deal it them individually e.g: Adding to_date() in a date in case of Oracle database/provider.

It's defined as an array of objects to accept multiple parameters, so, the query can be build the way you want.

This specific line:

new Object[] { 3 }

can be translated as:

Object[] AnonymousArrayObject  = new Object[1];
AnonymousArrayObject [0] = 3;

Since queryForList receives an array of object as parameter, there several ways of calling this method:

Example 1

getJdbcTemplate().queryForList(
    "DELETE FROM foo WHERE id = ?",
    new Object[] { 3 }, 
    String.class
);

Example 2

Object[] Parameters = new Object[1];
parameters[0] = 3;

getJdbcTemplate().queryForList(
   "DELETE FROM foo WHERE id = ?",
   Parameters, 
   String.class
);

Example 3

Object[] Parameters = new Object[] { 3 };

getJdbcTemplate().queryForList(
   "DELETE FROM foo WHERE id = ?",
   Parameters, 
   String.class
);

Example 1,2 and 3 are doing the same thing

Example 4

getJdbcTemplate().queryForList(
   "DELETE FROM foo WHERE id = ?",
   null, 
   String.class
);

Example 5

Object[] Parameters = null;
getJdbcTemplate().queryForList(
   "DELETE FROM foo WHERE id = ?",
   Parameters, 
   String.class
);

Example 4 and 5 are doing the same thing

How is this called?

This is called Short-hand for creation and initialization of an array. Since you don't need to declare a variable to access the array of object in that specific moment.

What is going on there?

Explained in the answer!

How could you access that parameters?

Explained in the answer!

AutoBoxing

1

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

Creates anonymous array of Object

The parameters separated by comma are elements of the array.

Number of elements is the length of array

7
  • I am sure that anonymous classes have $id (where id is number) at the end of its class name but I don't see this part in "anonymous arrays". Also I can't find this term in official Java tutorial. Do you have some references Where can I read more about them? Maybe Java Language Specification?
    – Pshemo
    Nov 18, 2013 at 18:12
  • @Pshemo may be this and this can help.
    – Nishant
    Nov 18, 2013 at 18:36
  • I've seen this but I would prefer official sources. Anyway thanks for googling :)
    – Pshemo
    Nov 18, 2013 at 18:38
  • No info about anonymous arrays there either. I guess it is just term invented by community. Anyway thanks for help.
    – Pshemo
    Nov 18, 2013 at 18:55
  • @Pshemo yes, it is just short hand way of creating arrays on the fly. It has mention in some books like this one
    – Nishant
    Nov 18, 2013 at 18:59
0

The code new Object[] { /* parameters separated by comma */}; for example new String[] {"a","b", "c"} creates 3 objects of type String.

0

new Object[] means that it's an array of Object type. new Object[]{3} is a short-hand to assign this array with an Integer 3.

0

Creates anonymous array of Object.

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

initialize an array with a value 3 at index 0

new Object[] { 3 }

The last example is the same as:

Object[] obj = new Object[1];
objs[0] = 3;
1
  • Umm ... arrays don't have an add method in Java.
    – Stephen C
    Nov 18, 2013 at 18:09
0

Mostly this scenario comes while working with DAO examples or database related applications example:- ========

    public Emp getEmpById(int id){  
        String sql="select * from Emp99 where id=?";  
        return template.queryForObject(sql, new Object[]{id},new 
            BeanPropertyRowMapper<Emp>(Emp.class));

In the above example query to fetch the employee data with reference to dynamic user request id number all the employees consider as list respectively id no's dynamically responds for the requested data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.