• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

returning array from a method  RSS feed

 
Ranch Hand
Posts: 37
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: 9986
206
  • 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: 9986
206
  • 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: 37
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: 9986
206
  • 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: 10759
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: 63791
209
  • 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.
 
You've gotta fight it! Don't give in! Read this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!