Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP return's question

 
enediel Gonza
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • 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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Barry,
This makes sense & more clearer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic