In c# I can write below code;
object[] params = new object[4] { "a", 10, "c", true};
What is the Java notation of above code?
In c# I can write below code;
object[] params = new object[4] { "a", 10, "c", true};
What is the Java notation of above code?
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 ;
You can do
Object[] array = new Object[]{ "a","b"};
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.
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.
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!