eversor
eversor

Reputation: 3073

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

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

Upvotes: 12

Views: 35927

Answers (9)

murali
murali

Reputation: 1

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

Upvotes: 0

Felipe Volpatto
Felipe Volpatto

Reputation: 155

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;

Upvotes: 0

EProgrammerNotFound
EProgrammerNotFound

Reputation: 2471

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

Upvotes: 3

Stephen C
Stephen C

Reputation: 719709

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.

Upvotes: 3

Deniz
Deniz

Reputation: 459

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

Upvotes: 22

Muhammad Kashif Nazar
Muhammad Kashif Nazar

Reputation: 23965

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.

Upvotes: 0

ksv
ksv

Reputation: 37

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

Upvotes: 0

Benoit Wickramarachi
Benoit Wickramarachi

Reputation: 6226

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

Upvotes: 4

Nishant
Nishant

Reputation: 1142

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

Upvotes: 1

Related Questions