1

In c# I can write below code;

object[] params = new object[4] { "a", 10, "c", true};

What is the Java notation of above code?

2
  • 1
    Why you want to do this? why array of objects intead of array of strings? I dont think this is efficient approach. Commented Jul 31, 2013 at 7:31
  • 1
    what do you really want to know... Commented Jul 31, 2013 at 7:44

7 Answers 7

3
Object[] param = new Object[]{ obj0, obj1, obj2, ..., objX };

or

Object[] param = { obj0, obj1, obj2, ..., objX};

or

Object[] param = new Object[X+1];
param[0] = obj0 ;
...
param[X] = objX ;
Sign up to request clarification or add additional context in comments.

1 Comment

I corrected your index value because if x is 5, then maximum aray index should be 5-1 = 4 . Hope you don't mind
1

You can do

Object[] array = new Object[]{ "a","b"};

  • Note that you do not have to specify the size of array while using this syntax
  • The array is filled up with values in the same statement.

OR

Object[] array2 = new Object[2];

If you do this, then polulate array using

array2[0] = "a";
array2[1] = "b";

You can also create anonymous array, which is can be used to pass to a method which takes array argument e.g.

public void methodTakingArray(Object[] array){
    // perform operations using `array` variable.   
}

and you call the method as methodTakingArray(new Object[]{"a","b"}); so that it can be accessed in the method using array local variable.

Comments

0
Object[] params = {"a","b"}

or

Object[] params = new Object[8];

params[0] = "a";...

Comments

0
Object[] params = new Object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Check the difference:

Object instead of object

No [8] : you cannot define dimension if you are initializing the array.

Comments

0

Simple,
Object []params=new Object[]{"a", "b", "c", "d", "e", "f" , "g", "h"};

You can declare any array like this, using the syntax

Type [] var_name=new Type[]{/*Values seperated by commas*/x, y, z};

Comments

0

object[] params = new object[8] { "a", "b", "c", "d", "e", "f" , "g", "h" }; // 8 is no need here

define array as follows

object[] params = new object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Follow this to learn more about Arrays in java.

Comments

0

Very Simple!

As you have started learning Java, I hope you have learned the syntax of Array creation. Array creation in Java is a two step process, unlike C/C++.

Since you have worked in C#, it would be easy for you to learn too:

Syntax:

    Array_type Array_name[];
    Array_name = new Array_type[Array_size];

So, your solution is:

    Object param[];
    param = new Object[8];

    param[0]='a';
    param[1]='b';
    and so on..

Ofcourse you can combine these two steps into one, by writing:

    Object param[]={'a','b','c','d','e','f','g','h'};

I hope that helped!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.