• 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about float primitive types

 
Greenhorn
Posts: 11
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am studying for the OCA Java SE 8 IZ0-808. I am taking the Enthuware tests and I can't figure out why I missed a problem. In the future, if I have a question, should I post it in a specific place?

So I am on Test 2, question 33.
It asks which answers are valid.

float f = -123;
is valid but I don't know why? I thought you needed an "f" at the end to make it a float or it won't compile.

Thanks,
Simon
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Implicit casting from an int to a float is allowed -- so that assignment compiles.

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Shi wrote:I thought you needed an "f" at the end to make it a float or it won't compile.


Try this code snippet and check if it compiles
 
Simon Shi
Greenhorn
Posts: 11
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Simon Shi wrote:I thought you needed an "f" at the end to make it a float or it won't compile.


Try this code snippet and check if it compiles



Aww. So this does not compile. If I put a float equals to a primitive integer, it compiles. If I put a float equals to a primitive double, it doesn't compiles unless a cast is there.

Thanks for the help!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Shi wrote:If I put a float equals to a primitive double, it doesn't compiles unless a cast is there.


Can you provide a code snippet? Assigning a float to a double should compile without adding an explicit cast. So this statement compiles successfully
 
Simon Shi
Greenhorn
Posts: 11
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Simon Shi wrote:If I put a float equals to a primitive double, it doesn't compiles unless a cast is there.


Can you provide a code snippet? Assigning a float to a double should compile without adding an explicit cast. So this statement compiles successfully



Aww. That what I was meant to say, putting the "f" at the end.
Thanks for the help!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Shi wrote:That what I was meant to say, putting the "f" at the end.


That's not an explicit cast! Adding the F (or f) suffix changes the type of the literal value to float. As you (probably) know, the default type for floating-point numbers in Java is double.
reply
    Bookmark Topic Watch Topic
  • New Topic