aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "dereferenced mean" Watch "dereferenced mean" New topic
Author

dereferenced mean

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
[code]
public class Example{
public static void main(String args[])
{
int a2=5;
int b2=5;
System.out.println(a2==b2);
System.out.println(a2.equals(b2));
}

}

[\code]
it throws Compile time Exception:int cant be dereferenced.
System.out.println(a2.equals(b2))
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

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).

Marco
Nitesh Kant
Bartender

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!
 
Don't get me started about those stupid light bulbs.
 
subject: dereferenced mean
 
Similar Threads
Casting Questions
is rectangle?
Garbage Collection Problem
why variables inside interface has to final
Wrapper, boxing == huh????