The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "float error" Watch "float error" New topic
Author

float error

Sagarya Kulkarni
Greenhorn

Joined: Jun 09, 2009
Posts: 4
Float a=new Float(9.0f);
float b=(float)9.0;
System.out.println(b.equals(a));

the output is
Demo.java:7: float cannot be dereferenced
System.out.println(b.equals(a));
^
1 error

WHAT IT MEANS?
Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4050

float(b) is different from Float(a)


fall down seven times, get up eight times-bodhidharman
Sagarya Kulkarni
Greenhorn

Joined: Jun 09, 2009
Posts: 4
but what 'dereferenced' means?
if i use a.equals(b), it returns true. why is it so?
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
Sagarya Kulkarni wrote:Float a=new Float(9.0f);
float b=(float)9.0;
System.out.println(b.equals(a));

the output is
Demo.java:7: float cannot be dereferenced
System.out.println(b.equals(a));
^
1 error

WHAT IT MEANS?


.equals() is only used between objects. Not between primitive and Object.

if you want to check for equality, do the conversion before. like...

This message was edited 3 times. Last update was at by Madhu Desai



Thanks
Preparing for SCJP 6
Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4050

Sagarya Kulkarni wrote:if i use a.equals(b), it returns true. why is it so?


Because a object has an equals()
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 330
Sagarya Kulkarni wrote:but what 'dereferenced' means?
if i use a.equals(b), it returns true. why is it so?


a is an object. It has an equals method.
b is not an object. It is just a primitive and so you cannot call do anything like

Sagarya Kulkarni wrote: float b=(float)9.0;
System.out.println(b.equals(a));


Try changing the declaration

float b=(float)9.0;
to
Float b=(float)9.0;


Blog
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11214

Sagarya Kulkarni wrote:but what 'dereferenced' means?
if i use a.equals(b), it returns true. why is it so?

Variables of non-primitive types in Java area really references to objects on the heap. A reference is a pointer: it tells the JVM where on the heap the object is stored. Dereferencing means: looking up the object from the reference. To call a method or access a member variable via a variable that refers to an object, the reference must be dereferenced - the JVM must find the object on the heap.

The type 'float' is a primitive type, it is not a reference, so it can't be dereferenced. You get this error because you are trying to call the equals() method on a float - you can't do that, because the variable is not an object, and doesn't have methods that you can call.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: float error
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com