What does this line of code do?

I have tried testing it, but when I declared the methods as public and alter them within a class, it doesn't work, so I'm not sure how to inspect the objects:

int x = 5;

int xx[] =newint[]{x};//this is the one I'm unsure about

is it the same as:

int x = 5;

int xx[] =newint[x];

?

[677 byte] By [abu5ea] at [2007-11-27 5:18:30]
# 1
Oh, I think I've figured it, providing it is the same as:int xx[] = {x};?
abu5ea at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 2
> int xx[] = new int[]{x}; //this is the one I'm unsureAn int[] with a single element, whose value is x.> int xx[] = new int[x];An int[] with x elements, all of whose values are the default intiial value--0.
jverda at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 3
> Oh, I think I've figured it, providing it is the same> as:> > int xx[] = {x};> ?Try it and see.
jverda at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 4

I did figure a way to try it, but it was confusing me by not allowing me to declare it within a method (I think I know why now). I wasn't being lazy - I did have BlueJ (for inspecting) open and I was trying it.

It seems to give me the same result as:

int xx[] = {x};

, but is there some subtle difference?

Message was edited by:

abu5e

abu5ea at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 5
This:int xx[] = new int[]{x};is NOT the same asint xx[] = new int[x];is it however the same as:int xx[] = new int[1];xx[0] = xor what you said.
TuringPesta at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 6

> I did figure a way to try it, but it was confusing me

> by not allowing me to declare it within a method (I

> think I know why now). I wasn't being lazy - I did

> have BlueJ (for inspecting) open and I was trying

> it.

>

> It seems to give me the same result as:

> > int xx[] = {x};

>

, but is there some subtle difference?

Not in what it does, as far as I know.

You can only use the simple {x} form in a variable declaration like that. Other places, you have to use new int[] {x}.

jverda at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 7

> but is there some subtle difference?

nope. you can only use int xx[] = {x} when declaring a class or object

variable.

class x{

methods{ }

int xx[] = {};

}

where as int xx[] = new int[]{ } is a delightful shorthand for use inside

methods and the such.

TuringPesta at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 8
That explains why I was having trouble testing the code. Thanks a lot for your help as always.
abu5ea at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...
# 9

I think this is what jverd was referring to. Just thought I would spell it out.

int[] y = {1,2,3,4}; // ok on one line

int[] x;

x = {1,2,3,4}; // error when not one one line

floundera at 2007-7-12 10:41:39 > top of Java-index,Java Essentials,Java Programming...