• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

float error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float(b) is different from Float(a)
 
Sagarya Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but what 'dereferenced' means?
if i use a.equals(b), it returns true. why is it so?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Because a object has an equals()
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic