Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why does long default to 0L but float to 0.0?

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

Why does the long primitive type default to 0L, but the float type defaults to 0.0? Is this some weird rule?

If the float defaults to 0.0 and not 0.0f, then I'd expect that long would default to 0 and not 0L. I think it should make sense?

Also, the table num 2.3 in the complete study book (815/816/817) says that long defaults to 0 and not 0L. But then, the question 8 in revision num 2 says long defaults to 0L...

How do I memorize that?
 
Nani Ksowy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nvm, I think I got it now, thanks!
 
Author
Posts: 34
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have access to that book, and apologies if I've misunderstood your question, but:

"An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int"
https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.10

So if you write just the literal 0, it is an int. If you write 0L, it is a long.

Dan
 
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0, 0L and 0.0 are all the same thing: zero. The difference comes when you assign them.

You can assign "0" to any int, long, float or double (among other things). It's the abstract/universal value zero. Or more precisely, an int 0, which can be automatically cast to 0L, 0.0f and 0.0d.

When you code "0.0", you're defining a floating-point zero. When you code "0L" you're coding a long int zero. If possible, the compiler will automatically cast to the more appropriate value type. If it cannot determine what that type should be, or if there is no simple cast option, the compiler will complain.
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

A simple question indeed; but of course, a conceptual one.

Well, a data type is a particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it. Example - integer, character, float, double, etc.

Now, in Java, the default values of float and double are as below.

Float - 0.0f
Double - 0.0d
You can verify the same by running the below program.

public class DefaultValue

{

static double d;

static float f;

public static void main(String[] args)

{

System.out.println("Double :" + d);

System.out.println("Float :" + f);

}

}

Output:


Hope by now, you are confident with the default values of float and double in Java.
 
reply
    Bookmark Topic Watch Topic
  • New Topic