• 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

int divided by int

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This must be a simple question. I seem to think that if x is declared int in a program, x/3 would be a double. Will the outcome of x/3 be an int too?
I have this doubt because, depending on the value of x, the outcome would be an arithmetic integer (if x were multiples of 3) or an arithmetic float (if x were not multiples of 3).

Thanks in advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gyanesh Sharma:
...I seem to think that if x is declared int in a program, x/3 would be a double. Will the outcome of x/3 be an int too? ...


The result of int division will be of type int, and it will always truncate. So, for example, if x is 5, then x/3 will result in 1.

If you wanted a double as a result, then at least one of the operands would need to be of type double.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark. That helps a lot!!
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also keep in mind if x is anything smaller then an int,
like a byte or short, the compiler would complain,
because 3 is an integer.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x is byte or short, it will be promoted to int. So the outcome would be int again.
 
Steve Liem
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte passValue (int x) {
return (byte) x / 3;
}

Try this one.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If x is byte or short, it will be promoted to int. So the outcome would be int again.





Here too..the value is int after the operation...and the compiler error because of the return type which you have to cast explicitly from int to byte.

change byte to int for the return type...and see...there wont be compiler error... OR

return (byte) (x / 3);

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic