Java pointers

Put your problem here if it does not fit any of the other categories.

Java pointers

Postby smittix » Mon Dec 01, 2008 9:13 pm

I've been doing C++ for a while but cannot figure out how pointers are supposed to work in Java. I want to create one Bitmap image and send a pointer to that image to several different objects. This is what I've got but it crashes..

private Bitmap mButton;

mButton = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.button);

anrdButton button1 = new anrdButton(mButton);

--------------------

public class anrdButton {
public Bitmap mImage;

anrdButton(Bitmap mImage_)
{
this.mImage = mImage_;
}
}

-Chris
smittix
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 9:06 pm

Top

Postby MrSnowflake » Mon Dec 01, 2008 11:23 pm

Every object you create (Object, so not primitive data types (but arrays of primitive data types are objects)) is already a reference so if you would:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap bitmap = new Bitmap(someFile);
  2.  
  3.  
  4.  
  5. drawBitmap(bitmap);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Then drawBitmap will receive a reference to the original bitmap.

Actually, if you don't know this stuff, maybe you should learn a bit more Java, as that (as you would know) would make your programs better.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby smittix » Tue Dec 02, 2008 2:22 am

Thanks for the reply. Yeah, I definitely do need to learn some more Java to progress.
smittix
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 9:06 pm

Postby ismarc » Tue Dec 02, 2008 8:27 pm

Something that's important to point out is that calling it a "reference" to the object is inaccurate if you are used to C/C++ (or even perl) style referencing. Rather than go into the details myself, smittix, it's covered really well and concisely at http://javadude.com/articles/passbyvalue.htm .

EDIT: I tried clicking my link and realized the period was getting lumped onto the URL..woops, should be easier to click now
Last edited by ismarc on Tue Dec 02, 2008 9:01 pm, edited 1 time in total.
ismarc
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 29, 2008 6:09 am

Postby MrSnowflake » Tue Dec 02, 2008 8:40 pm

ismarc wrote:Something that's important to point out is that calling it a "reference" to the object is inaccurate if you are used to C/C++ (or even perl) style referencing. Rather than go into the details myself, smittix, it's covered really well and concisely at http://javadude.com/articles/passbyvalue.htm.
You are correct, didn't know this actually. But to make it more clear to c++ developers references is probably more clear.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby smittix » Wed Dec 03, 2008 6:26 pm

I actually just happened to see that link yesterday, and it was quite helpful. Yeah I didn't realize that (thinking in C++ terms) everything is basically a reference. It seems that there are still times when I would want to send just the value of an object and not a reference to it, but there are ways around that so it's not a big deal I guess. Thanks for the reply though.

- Chris
smittix
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 01, 2008 9:06 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 24 guests