File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dereferenced mean

 
rex tony
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic