How come this will work:
int i={1,2,3};
…but this will not ???:
int i;
i={1,2,3};
How can I make the second one work O_o?
How come this will work:
int i={1,2,3};
…but this will not ???:
int i;
i={1,2,3};
How can I make the second one work O_o?
The first line also doesn’t work (at least it doesn’t compile in Eclipse and I’d doubt such a bug would be in javac)
it is not an int, it is an int-array.
There are multiple ways to define it:
int[] a = {0, 1, 2};
int[] a = new int[]{0, 1, 2};
int[] a = new int[3]; a[0] = 0; a[1] = 1; a[2] = 2;
And you can replace the “int[] a” by “int a[]” for some obscure reason.
Oops, I meant to put:
int i[]={1,2,3};
int i[];
i={1,2,3};
but yeah you answered my question, thanks ;D
And you can replace the “int[] a” by “int a[]” for some obscure reason.
You can also write int []a or even int[]a. What doesnt work is []int a 
at instantiation, you can use just braces, like so…
int x[] = {1,2,3};
however, if you’re trying to intialize the variable after it has been declared, plain ol’ braces won’t do the trick:
int x[];
x = {1,2,3}; // wont compile
you have to re-specify the type…
int x[];
x = new int[] {1,2,3};
who knows why. shrug
I’m gonna gues, it might already be assigned and it would be unclear how to interpert it?
The shorthand notation is ambiguous in certain circumstances, so cannot be used all the time.
Therefor, for consistency I never use it.
Also, interestingly enough - I just found an erroneous compilation error message given by the Eclipse compiler =)
Object [] c = {{}};
Gives the compiler error :-
[quote]Type mismatch: cannot convert from Object[] to Object
[/quote]
Quite clearly Object[], being an Object itself should always be able to be converted to Object.
Sun’s javac is alot more clear in its error :-
[quote]illegal initializer for java.lang.Object
[/quote]
The obscure reason is that int[] a makes a lot more sense, but int a[] is how C does it and the language desigenrs were afraid of the programmer lashback if they didnt make it work that way, too.
programmer lashback such as lack of operator overloading… etc :
Well, if you ask programmers what they would do with operator overloading they only mention vector stuff, which like… makes some sense. The other useful thing you can do with operator overloading is unnecessary in java, because its managed to begin with.
Other than that operator overloading causes more trouble than it helps.
So… dunno… guess most would be happy if there were useful default vector classes with overloaded operators (like String). Is there any other default thing? (Apart from String, which is already there… vectors, which I just mentioned… and trying to make it sorta smart, which isnt neccessary.)
You will find programmer opiniosn evry mixed on the alck of operator overlaoding.
I for one agreewith the deicsion not to include it as I feel the obfuscatory nature of it vastly outweighs its usefulness.
YMMV.
lucky for me, java was my first language, and you can’t miss what you never had 
back to the original topic, I kinda wish C# allowed both type of array declaration like java does. I usually use the C-style declaration so I can keep my same-type declarations on one line, like…
int x,y,speed[]; // one line
but in C# you have to use a brand new line…
int x,y;
int[] speed;
small-scale example, but it just annoys me. shrug
I would consider the first example bad code ;D
My reasoning is 2 fold :-
1)
As im sure you are aware this is false;
an int[] is an Object type, an int is a primitive type.
yeah int[] may be object, but I don’t think of it that way 
I look at it as “a lot of ints” and therefore they stay on my int line! >=P
most places I go coding conventions define that you should declare every field on a new line, reguarding methodes, some best practice defined that scopes of var should be as small as possible.
it’s a good thing none of this really affects the outcome at runtime, since we all go by different preferences