3

I m new to java, what does return; mean? is it like break ?

  public void run() {
            if(imageViewReused(photoToLoad))
                return;
            Bitmap bmp=getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if(imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }

if the second imageViewReused(photoToLoad) returns true, BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad) won't be executed, right?

Blake
  • 7,367
  • 19
  • 54
  • 80
  • 4
    please go and read a basic (and freely available) java tutorial... – Mitch Wheat Dec 22 '12 at 05:45
  • http://stackoverflow.com/questions/744676/what-does-a-return-key-word-do-in-a-void-method-in-java – kosa Dec 22 '12 at 05:47
  • Just my editorial comment but breaks and returns from a void method should be avoided : they are generally bad code design. Usually they can be avoided and in the example above you could do something like if(!imaveViewReused(photoToLoad)) { ... }. – Vineet Kosaraju Dec 22 '12 at 06:19

7 Answers7

5

Yes there is a similarity but there is also difference

  • break - will stop a loop and switch condition. Can be used only for switch, and loop statements
  • return - will finish the function execution but the statements below of this keyword will not be executed. Can be used only for any functions.

Usage of return keyword in void function

If you use return in a void function like this

void trySomething()
{
  Log.i("Try", "something");

  return;
  Log.e("Try", "something"); 
}

the execution of this function is done but the statement(s) below will not be executed.

Usage of break keyword

for any loop statements

void tryLoop()
{
   while(true)
   {
      Log.d("Loop", "Spamming! Yeah!");
      break;
   }
}

the loop will be stopped and continue the remaining statements of this function

for switch condition

void trySwitch()
{ 
   int choice = 1;
   switch(choice)
   {
      case 0:
        Log.d("Choice", "is 0");
        break;
      case 1:
        Log.d("Choice", "is 1");
      case 2:
        Log.d("Choice", "is 2");
   }
}

using break in switch condition is also same as loop. Omitting the break will continue the switch condition.

1

Yep, you can use it like a break.

Evos
  • 3,915
  • 2
  • 18
  • 21
1

Yes, return is break your next execuation of same block.

for more information about return check this

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • *"Yes, `return` is same as `break` in java"* - this is simply not true. They're similar in a sense but they are definitely not the same. – arshajii Dec 22 '12 at 10:51
1

return ends the execution of the method in which it appears when it is called. For void methods, it simply exits the method body. For non-void methods, it actually returns a value (i.e. return X). Just be careful with try-finally: remember that the finally block will be executed even if you return in the try block:

public static void foo() {
    try {
        return;
    } finally {
        System.out.println("foo");
    }
}

// run foo in main
foo

This is a good reference for learning more about return.


is it like break?

Well in the sense that both statements 'end' a running process; return ends a method and break ends a loop. Nevertheless, it is important to know the differences between the two and when each should be used.

if the second imageViewReused(photoToLoad) returns true, BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad) won't be executed, right?

Correct - the method will "return" if the body of that if-statement is executed and no subsequent statements will be reached.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

A function's execution is finished, when it comes to the return statement and then it returns back to its invoking code. In your case,

if imageViewReused(photoToLoad) is true, then the code block after return will not get executed.

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
0

Here return act as end of function. You can avoid it by changing your code as,

 public void run() {
        if(!imageViewReused(photoToLoad))
        {
          Bitmap bmp=getBitmap(photoToLoad.url);
          memoryCache.put(photoToLoad.url, bmp);
          if(!imageViewReused(photoToLoad))
          {
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
          }
    }
EdChum
  • 376,765
  • 198
  • 813
  • 562
viSH98
  • 1
-1

Return statement skips the remaining execution of a function scope.

Worth reading:

  1. return : http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
  2. break : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
S.D.
  • 29,290
  • 3
  • 79
  • 130