• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

RandomStyuff

Junior Member
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
 
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# 😛
 
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!
 
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]
 
Back
Top