Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help with Literals, Primitive data types, & Numeric Promotion

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am currently reading through the Oracle Certified Associate Java SE 8 Programmer I and am fairly new to Java and have run into a question on Literals that I thought I understood but now I am seeing something in the text that now has me confused.

In Chapter 1 a literal is described as

When a number is present in the code, it is called a literal.



In the same chapter when looking at the primitive types float & long the text states:

A float requires the letter f following the number so Java knows it is a float.


By default, Java assumes you are defining an int value with a literal. In this example, the number listed is bigger than what fits in an int. Remember, you aren't expected to memorize the maximum value for an int. The exam will include it in the question if it comes up.





Java complains the number is out of range. And it is-for an int. However, we don't have an int. The solution is to add the character L to the number...



This all seems to make sense to me...until I got to chapter 2 and reviewing Numeric Promotion examples.

In one example (which makes sense to me):

What is the data type of x + y?




This is actually a trick question, as this code will not compile! As you may remember from Chapter 1, floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value was set properly to 2.1f, then the promotion would be similar to the last example, with both operands being promoted to a double, and the result would be a double value.



This example seems to support the information in chapter 1.

However the last example

What is the data type of x * y/z?





Based on everything above, it would seem that y would not be considered a float by java as it does not list f but the explanation seems to allude that the f is not necessary for a float primitive.  

Is this because the literal does not contain a decimal? If that is the case why does java even look at this as a float and not an int? If it looks at y as an int then after x is promoted to an int it would never need to be promoted to a float and would just get promoted directly to a double when it is divided by z.

Am I completely missing something here or am I just losing my mind? Any explanation on this would be much appreciated. Thank you in advance.
 
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means that writing 123 or 123.45 or 123.45e67 is creating a number literal.
There are other ways to write number literals. Maybe the Java™ Tutorials will help, maybe this section or this part. It is a long time since I read those sections, so I might be mistaken. The definitive guide to the details is in the Java® Language Specification (=JLS).
I hope the book doesn't give -123 as a valid number literal.

You rarely ever use a float in real life, but if you do, you have to write 123.45f or 123.45F. Please explain what it is that is so difficult; the quotes you have written look straightforward to me. 3123456789 is a valid number, but it is too large to be an int, so you have to append L to it (not l): 3123456789L. Now it is a long literal and the code will compile.
If you write float f = 21; the 21 is an int. It remains an int until the assignment operator = is executed, and the assignment causes it to be widened to a float, converting it to (float)21.0.

You wrote:What is the data type of x * y/z?

You go left to right.
  • x is a short, another datatype hardly ever used in real life.
  • The * operator does multiplication, but first the short value is widened to an int retaining sign (positive/negative).
  • The * sign applies the int to a float and the int is widened to a float (float)14.0.
  • The multiplication is done giving the result (float)294.0.
  • The division sign causes the float to be widened to a double 294.0 if the other operand is a double.
  • When the division is executed, the result is a double 9.8.
  • If you go through the JLS it explains that, also telling you where widening or numeric promotion occurs.
     
    Greenhorn
    Posts: 17
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Based on everything above, it would seem that y would not be considered a float by java as it does not list f but the explanation seems to allude that the f is not necessary for a float primitive.   Is this because the literal does not contain a decimal?



    Yes, this is because the literal does not contain a decimal.


    If that is the case why does java even look at this as a float and not an int?



    It did look at the right as an int... but then you wanted to shove it into a float.  So the rule in java is that it will let you put an int into a float and even be nice enough to convert it for you.  The 13 becomes a 13.0 in the variable y which is a float.


    If it looks at y as an int then after x is promoted to an int it would never need to be promoted to a float and would just get promoted directly to a double when it is divided by z.



    As already mentioned, it doesn't look at y as an int.  


    Am I completely missing something here or am I just losing my mind? Any explanation on this would be much appreciated. Thank you in advance.



    No man, you aren't going crazy.  Also, the main place you'll see the F issue is when you have a decimal on the right of the = and a float type variable on the left.  You will get a compile error without the F since java gets scared you might be losing information by putting that Big ole literal double into a little wimpy float.  When you add the F you are pretty much saying that you are okay that 3.434324324F will lose some precision.  Then java breathes a sigh of relief.  









     
    And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
      Bookmark Topic Watch Topic
    • New Topic