wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Declaration doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Declaration doubt" Watch "Array Declaration doubt" New topic
Author

Array Declaration doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583




it works when i write

but i want this in one line ? any idea ?


Tell the difficulties that i am difficult.
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

in java we can return only class type value with return keyword.


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Declaration doubt