Is an array accessable directly on a return? (Java)

RandomStyuff

Junior Member
Jul 10, 2007
5
0
0
I just had a major computer science exam, and I don't know why, maybe a split second of stupidity, had me use an array I received from a get directly. I know I should have just made an array and copied it into that, but in the stress of the exam, I don't know why I didn't do that. Now the suspense is killing me: is it right? do you think it will be accepted?

Thanks in advance
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You might need to surround the method call with () before you can access the index operator on it, but I don't see why it wouldn't work. At least that syntax works in C# :p
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I think you're fine. Any reasonable get-reference function would allow that syntax.
 
Oct 27, 2007
17,009
5
0
Easy way to find out :) Yep I just tried it, works fine.

public static void main(String[] args)
{
System.out.println(getArray()[2]);
}

public static String[] getArray()
{
String[] result = {"Oh", "hi", "there!"};
return result;
}

Output: there!
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
of course it is.

what happens when you call getArray() in the above example?

getArray() is evaluated as result. So then getArray()[1] is result[1]
 

heymrdj

Diamond Member
May 28, 2007
3,999
63
91
Well yeah they already answered you. I know it works in C#, never tried the Java though :p.