Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Declaration doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



it works when i write

but i want this in one line ? any idea ?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java we can return only class type value with return keyword.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:

Here you're trying to declare a variable and return it on the same line. You can't do that, but fortunately you don't need to - there's no need for the variable as you can just create the array and return it immediately. However, because you don't have an array declaration, you need to tell the compiler what kind of array to create. So:
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
saloni jhanwar wrote:

Here you're trying to declare a variable and return it on the same line. You can't do that, but fortunately you don't need to - there's no need for the variable as you can just create the array and return it immediately. However, because you don't have an array declaration, you need to tell the compiler what kind of array to create. So:


Thanks Metthew
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya would just like to supplement Matthew's answer. You can do it in the following way as told by Mathew



This is called ANONYMOUS ARRAY CREATION in java. It is like creating an array JUST IN TIME without assigning it to any variable. beware though of the syntax

//here we have declared size between the rectangular braces


So watch out for code like this.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic