• 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

SCJP return's question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to understand why this function doesn't produce any error.
The result is not exactly an integral value.

Even, the line
System.out.println(x/13);
produces an integral output when it's not the real value of the operation.

long test(int x){
System.out.println(x/13);
return x/13;
}

Thank's in advance for the answers.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(x/13);
In the above code 'x' is an integer so an integer / integer returns another int. This int is promoted to a long & returned to the called place.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to the last post:

The "/" operation, when perfomed on two ints, is a "truncated division operation". That is, the fractional part (which would be there in a mathematical sense) is thrown away. So 1/3 is not 0.333333..., it's 0 (exactly.)
28/13 is not 2.1538461538461538461538461538461..., it's 2 (exactly.)

If you want an approximation to the real result you can use:

[ July 17, 2005: Message edited by: Barry Gaunt ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry,
This makes sense & more clearer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic