0

I was carry out 3 tests to check if the bitmap is pass by value or reference but get confused after I run the following code:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    V v = new V(this);
    setContentView(v);
}

class V extends View{
    Bitmap b1;
    Bitmap b2;

    public V(Context context) {
        super(context);
        //load bitmap1
        InputStream is = getResources().openRawResource(R.drawable.missu);
        b1 = BitmapFactory.decodeStream(is);
        //testing start
        b2 = b1;
        //b1 = null; 
        //1.test if b1 and b2 are different instances
        if(b2 == null){
            Log.d("","b2 is null");
        }
        else{
            Log.d("","b2 still hv thing");
        }
        //2.test if b2 is pass by ref or value
        test(b2);
        if(b2 == null){
            Log.d("","b2 is null00");
        }
        else{ 
            Log.d("","b2 still hv thing00");
        }
                    //3.want to further confirm test2
        b2 = b2.copy(Config.ARGB_8888, true);
                    settpixel(b2); 
        if(b2.getPixel(1, 1) == Color.argb(255,255, 255, 255)){
            Log.d("","b2(1,1) is 255");
        }
        else{
            Log.d("","b2(1,1) is not 255");
        }
    }

    void test(Bitmap b){
        b = null;
    }
    void settpixel(Bitmap b){
        b.setPixel(1, 1, Color.argb(255,255, 255, 255));
    }
}}

results:

  • b2 still hv thing

  • b2 still hv thing00

  • b2(1,1) is 255

The problem is test 2 and 3 contradict to each other. test2 show that b2 is pass by value because b2 didn't become null. But if bitmap is passed by value, then in test3, the setPixel() should be working on the copy of b2(the one in the function scope), but why b2(outer scope) change it's pixel value? p.s. the loaded bitmap is in deep red color.

jv42
  • 8,521
  • 5
  • 40
  • 64
ViciV
  • 303
  • 2
  • 11
  • check out how bitmap works http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/graphics/Bitmap.java#Bitmap.nativeCopy%28int%2Cint%2Cboolean%29 – weakwire Feb 06 '13 at 17:27

2 Answers2

2

This has to do with the way that Java works, more than how Android or Bitmap work. Java passes the reference by value. A much more thorough explanation can be found at http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html

Scott W
  • 9,742
  • 2
  • 38
  • 53
0

JAVA IS ALWAYS PASS BY VALUE

Each time you pass a variable to a function of java you are copying its reference, and when you get a result from a function in java return it is a new copy of the object's reference.

Now here is a step by step how your test() function works:

  • When you pass b2 throw calling test(b2); you will get a new reference to b2's object called b.
  • Then you set that reference to null. You are not affecting the reference out of your scope because you can't access it anyway.

Hope that helps. please take a look at this detailed question :

Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • 1
    this description is a bit misleading. It sounds like you are saying that you get a copy of the entire Bitmap, when what you really get is a copy of the reference to the Bitmap object. – Scott W Feb 06 '13 at 17:31
  • I think I got it. When calling settpixel(), the new ref. is still linking to my outer scope b2 , so .setpixel will affect the outer scope b2. But when calling test(), I just link the parameter b2 to the address of null, so any change to parameter b2 will not affect the outer scope b2 since parameter no longer linked to outer b2. am i correct? – ViciV Feb 07 '13 at 04:08