This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 224

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: 4245
    
    7

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: 923
    
    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.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Declaration doubt
 
Similar Threads
question from Mughal cert book
Applet to Javascript Communication
output of the code
Simple program(but cofusion with output)
Scanner