Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between these statements :

//1
return;

//2
return null;
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 is used to return early from a void method. Ugly style IMO.

2 is used to return a null reference from a method that has an object reference return type.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In correct way could i say, first return statement returns void ?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really as void means there is no return type. So returning void makes no sense.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we write method definition i.e

public int method1()
{
return 0;
}
//here method1's return type is int that's why we call it returns an int.

public void method2()
{
return;
}

//Same as it doesn't mean that method2's return type is void that's why we could say it returns void.Because it wont be appropriate to say it returns nothing, because it really has type that is void.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return; it simply means return i.e. return the control back to invoking method. it does not return(value) anything. just pass of control. a method with return type void can include return statement at any point

return null; this can be put in any method with return type of the type Object. it actually does something. it returns null , so say i have something like this

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay nice.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I stated earlier, I think using return on its own is an ugly style and for this example:

it is redundant as well. Only use return statement when you have something to return to calling code.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic