The moose likes Java in General and the fly likes dereferenced mean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "dereferenced mean" Watch "dereferenced mean" New topic

dereferenced mean

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
public class Example{
public static void main(String args[])
int a2=5;
int b2=5;


it throws Compile time Exception:int cant be dereferenced.
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hi Rex,

which Java version are you using? For this code example you need 1.5 or greater!

The problem is you're calling a method equals() on variable a2 which is a primitive int. Because primitive types like int aren't first class objects, you can't call a method on such variables. This is only possible to do with reference variables which point to an object.

With an older version of Java you'll have to use the wrapper object types like Integer for this code to work:

If you're using Java >= 1.5 this is done automatically for you with a new feature called autoboxing. Autoboxing converts a variable transparently between the primitive type (like int) and its corresponding wrapper type (like Integer).

Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

The . operator can only be used for Objects and not for primitive data types like int. This is what the error is suggesting.

(The code tag ends with a forward slash and not a backward slash. Alternatively you can use the UBB buttons to put code tags and lot of other formatting constructs in your post.)

apigee, a better way to API!
I agree. Here's the link:
subject: dereferenced mean
It's not a secret anymore!