• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Primitive data type - long and integer

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

The error message that the Java compiler gives me is confusing.

I purposely have a numeric (integer) literal that is outside the integer range. I know it will give me an error message as follows:


------------------------------------------------------
But if I purposely give a numeric (long) literal that is also outside of the long range, the message I get is also the integer too large.
Why doesn't Java say long too large?

 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you get an error for your second line of code? It is correct.
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How did you get an error for your second line of code? It is correct.



Yes, 2nd line is correct. I put the wrong code.

The error code should be this one.

long number = 666666666666666666666666L; // error

Just wondering why the error message says integer number too large.

Why not long number too large?

 
Saloon Keeper
Posts: 12723
277
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The word "integer" refers to the mathematical concept of a number that is "whole".

Java uses the keyword int to declare a variable that can hold integers up to 2^16-1, and it uses long to hold "long integers" up to 2^32-1, but they are both "integers".
 
henry leu
Ranch Hand
Posts: 96
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The word "integer" refers to the mathematical concept of a number that is "whole".

Java uses the keyword int to declare a variable that can hold integers up to 2^16-1, and it uses long to hold "long integers" up to 2^32-1, but they are both "integers".



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

Stephan van Hulst wrote:The word "integer" refers to the mathematical concept of a number that is "whole".

Java uses the keyword int to declare a variable that can hold integers up to 2^16-1, and it uses long to hold "long integers" up to 2^32-1, but they are both "integers".


This doesn't sound right to me. As far as I'm aware, ints are 32 bits and longs are 64 bits. This results in the ranges of:
int: -2^31 - 2^31-1
long: -2^63 - 2^63-1
The missing 32nd/64th bit is used to designate if a number is either positive or negative.
 
Campbell Ritchie
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right; the 16 must be a misprint. The 31st bit doesn't only determine the sign; it represents a value of −(2³¹). That is different from an S&M representation where the leftmost bit inverts the sign of the rest of the number.
 
Saloon Keeper
Posts: 23263
158
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

Stephan van Hulst wrote:The word "integer" refers to the mathematical concept of a number that is "whole".

Java uses the keyword int to declare a variable that can hold integers up to 2^16-1, and it uses long to hold "long integers" up to 2^32-1, but they are both "integers".



No, Java does NOT base itself on 16-bit integers. A Java int is 32 bits worth of data (31 + sign). A Java long is 64 bits worth of data. The days of 16-bit integers are long gone outside of specialty processors.
 
Stephan van Hulst
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote that in a less than rested state of mind. The corrections of course are correct.
 
Tim Holloway
Saloon Keeper
Posts: 23263
158
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

Stephan van Hulst wrote:I wrote that in a less than rested state of mind. The corrections of course are correct.

Thank you. Too many times, I give incorrect corrections, alas.

Still, I departed the 16-bit world after only a brief sojourn there. IBM mainframes were 32-bits back then. The Motorola MC68000 CPU could be used in either 16 or 32-bit mode (Apple used 16 bits, Commodore used 32 and I was an Amiga geek), so it was only around 1990 that I regularly worked with 16-bit processors and my thought patterns developed accordingly.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic