Home arrow static arrow Java Programming [Archive] - some explnations needed
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - some explnations needed
This topic has 11 replies on 1 page.

Posts:114
Registered: 7/5/04
some explnations needed  
Aug 2, 2004 6:37 AM



 
Hi

I've explored some of the classes in java, I saw some combinations I don�t understand. What is the use of return without a n element to be returned?

Thanks
S
 

Posts:21,718
Registered: 98-02-20
Re: some explnations needed  
Aug 2, 2004 6:41 AM (reply 1 of 11)



 
If the return type is void, then adding a "return" statement can be a matter of taste. If you write a method that executes from top to bottom, control will be passed back to the statement after the method call whether you include a "return" statement or not.

However, if you have multiple return points adding a "return" is meaningful, because in that case you're saying "If this condition occurs, I want to leave my method and not execute any of the stuff that follows." It's not just taste in that case, it's valid logic.

The return type has to be void if you're going to leave "return" out. If the return type is anything other than void the compiler will chide you for leaving it out.

%
 

Posts:114
Registered: 7/5/04
Re: some explnations needed  
Aug 2, 2004 12:04 PM (reply 2 of 11)



 
However, if you have multiple return points adding a
"return" is meaningful, because in that case you're
saying "If this condition occurs, I want to leave my
method and not execute any of the stuff that follows."
It's not just taste in that case, it's valid logic.

so i could have using a break for this. can u give my an example code?
 

Posts:2,206
Registered: 8/15/02
Re: some explnations needed  
Aug 2, 2004 12:07 PM (reply 3 of 11)



 
No, break will break you out of a loop, not return from a method.
 

Posts:1,888
Registered: 22/08/01
Re: some explnations needed  
Aug 2, 2004 1:56 PM (reply 4 of 11)



 
public void doSomething(Obhect it){if  (it==null)   return;start doing something}
 

Posts:28
Registered: 7/28/04
Re: some explnations needed  
Aug 2, 2004 2:02 PM (reply 5 of 11)



 
using return is just a better programming practice. in most cases using "else" is an option, but the code becomes more and more difficult to read and maintain that way. using the previous example again:
public void method(Object it){  if (it==null)   {    do this;   }  else   {    do that;   }}


But a better way again is this:
public void method(Object it){  if (it==null)  {    do this;    return;  }  do that;}

In this case the "else" scenario is coded without using "else", just makes for better (and perhaps more efficient??!!) code
 

Posts:114
Registered: 7/5/04
Re: some explnations needed  
Aug 2, 2004 8:36 PM (reply 6 of 11)



 
However, if you have multiple return points adding a
"return" is meaningful, because in that case you're
saying "If this condition occurs, I want to leave my
method and not execute any of the stuff that follows."
It's not just taste in that case, it's valid logic.
Thanks

How can I put multiple "return" in my method if the compiler always shout on me whenever I do this(unrichable statment)?

Is using "return" and "do" should be used in parallel fusion? Is "do" means more than just do something? because the statement will be executed anyway so what is the use of "do'"?

Thanks again
D

 

Posts:5,965
Registered: 5/17/03
Re: some explnations needed  
Aug 2, 2004 9:15 PM (reply 7 of 11)



 
using return is just a better programming practice.

No it's not.

Preferably you shouldn't use return at all in void methods. When a method returns a value preferably this should be the last statement in the method.

There are exceptions to the above. For example a "guard" return at the very beginning of a method to quit upon some error or extreme condition. Also when the method is very small and "algoritmic" in its nature it's okay to put in multiple returns.

But the general rule is one return only at the end of a method, because it keeps down the structural complexity. To bail out of a large method at zillion places just makes it error prone to changes later.
 

Posts:8
Registered: 6/3/04
Re: some explnations needed  
Aug 2, 2004 10:34 PM (reply 8 of 11)



 
How can I put multiple "return" in my method if the
compiler always shout on me whenever I do
this(unrichable statment)?

Compiler will complain about unreachable statement only if it finds definite return path from
your method before this return statement.
Consider the following code.
public void someMethod(Object o){    if ( o == null)    {        return;    }    else     {        doSomething();        return;    }    return; // Unreachable statement}  


In this code the final return statement is unreachable as the method will definetly return either from if or else block.
 

Posts:114
Registered: 7/5/04
Re: some explnations needed  
Aug 4, 2004 8:46 PM (reply 9 of 11)



 
what is the use of "do" ?
 

Posts:2,830
Registered: 9/1/03
Re: some explnations needed  
Aug 4, 2004 8:48 PM (reply 10 of 11)



 
what is the use of "do" ?

it's a keyword.
do {  System.out.println("doing it.");} while(true);
 

Posts:37,103
Registered: 3/30/99
Re: some explnations needed  
Aug 4, 2004 9:59 PM (reply 11 of 11)



 
what is the use of "do" ?

You really ought to go through a book or tutorial.

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)
Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]
Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].
 
This topic has 11 replies on 1 page.