• 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

Why this Precision Error

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have this simple program

=====

public class Hello
{

public static void main(String[] args)
{
long size = 123456789l;
int arr[] = new int [size];
}
}
=====

Why does the compiler give the error

Hello.java:7: possible loss of precision
found : long
required: int
int arr[] = new int [size];
^
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think it is because Java expects all array sizes to be int. Thus, it forces size to be converted to an int, and warns you of the loss in precision.

_steve.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Simon Joseph Fernandez:

I think it is because Java expects all array sizes to be int.



That would explain why it's saying "required: int, found: long"...
 
sopal Pal
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it strange that array indices cannot exceed the int limit.
So if I need to have an array whose size is greater than int ( max ) my only alternative is to use linked list kind of things - right ?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isn't it strange that array indices cannot exceed the int limit.

Not really "strange", per se. The language is specified that way.

So if I need to have an array whose size is greater than int ( max ) my only alternative is to use linked list kind of things - right ?

I'm unaware of any theoretical upper limit on the number of elements in a LinkedList. I would imagine that memory and performance constraints would typically be a problem long before you reached the 2 billion+ limit on an array-backed structure, though.

I'm sure they're out there, but I haven't had the "fortune" to work with an application that required an array approaching Integer.MAX_VALUE size...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic