• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

a Primitive Conventions Question On Dan's Mock

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the answer is true,but I still can't understand it.because a+b+c+d is a float point number ,yet,4*'a' is a integer.how can they be equal?
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equality operator (==) compares the values of the operands not their type as it makes an implicit promotion to the operands, that's in case of primitive data types operands. The equals() method in the wrapper classes is the one that returns false if the data type of the argument is different from the data type of the object the method is invoked on.
Note: In case of using (==) with objects the comparison is made between the value of the references not the meaningful value of the object.
HTH
[ December 10, 2002: Message edited by: Alfred Kemety ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before performing the equality test (==) both operands are promoted. The left hand side will be a double since the variable a is a double primitive. The right hand side will be promoted to a double before the equality comparison.
The left hand side is equal to 388.0 (a double not a float)
The right hand side is equal to 388 (4 * 97) and is promoted to a double, that is 388.09, and then compared.
The result is obviously true.
 
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