MyEclipse Enterprise Workbench
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

float error

Sagarya Kulkarni
Greenhorn

Joined: Jun 09, 2009
Messages: 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
Ranch Hand

Joined: Jan 28, 2008
Messages: 2212

float(b) is different from Float(a)

How to Ask Good Question|EL or JSTL problem|Tomcat Faq
Sagarya Kulkarni
Greenhorn

Joined: Jun 09, 2009
Messages: 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
Messages: 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
Ranch Hand

Joined: Jan 28, 2008
Messages: 2212

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


Because a object has an equals()

How to Ask Good Question|EL or JSTL problem|Tomcat Faq
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Messages: 296

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;
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7631

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 6.0 API documentation
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench

trailboss links:

permaculture
diatomaceous earth
Sepp Holzer
rocket mass heater
raising chickens
cast iron
lawn care