This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

returning array from a method  RSS feed

 
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write a code to to return an array from method :


Why i can not   write return statement in line no 5 as :


while it is valid if i write code inside method num as:


 
Saloon Keeper
Posts: 9703
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's how Java was designed. There's not really any other answer.
 
Stephan van Hulst
Saloon Keeper
Posts: 9703
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give you some elaboration that might satisfy you a little bit more:

It's easy for the compiler to determine that an opening brace that follows an array declaration is the start of an array initializer.

It's hard for the compiler to determine that an opening brace in a spot where any expression may occur (such as the right hand of the return keyword) is not the start of a new scope.
 
Shubham Rai
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean if i return an anonymous array from a method, i have to use new keyword?
 
Stephan van Hulst
Saloon Keeper
Posts: 9703
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no such thing as an anonymous array. If you mean you want to return a newly created array directly without assigning a reference to a variable first, then yes, you have to use the new keyword.
 
Bartender
Posts: 10720
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's hard for the compiler to determine that an opening brace in a spot where any expression may occur (such as the right hand of the return keyword) is not the start of a new scope.


Which suggests that maybe Java should have used a different "bracket" for initialisers.

@Shubham Rai: A lot of this comes down to the fact that, way back in the '80s, Java was designed to "look" like C and C++, so that programmers would have less of a learning curve when learning the language.
As you can see, that comes with some trade-offs that aren't always wonderful. :-)

Winston

 
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The similarity of Java® syntax to C/C++ has also caused lots of confusion to people coming from C++, but some people have said they wouldn't have started using Java® if it had looked different from the older languages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!