• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Declaration doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



it works when i write

but i want this in one line ? any idea ?
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in java we can return only class type value with return keyword.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic