File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP return's question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP return Watch "SCJP return New topic

SCJP return's question

enediel Gonza

Joined: Jun 11, 2005
Posts: 4
I need to understand why this function doesn't produce any error.
The result is not exactly an integral value.

Even, the line
produces an integral output when it's not the real value of the operation.

long test(int x){
return x/13;

Thank's in advance for the answers.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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.

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Thanks Barry,
This makes sense & more clearer.
I agree. Here's the link:
subject: SCJP return's question
It's not a secret anymore!