• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Return Value

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come this compiles & runs without an explicit cast ?

long test( int x, float y) {
return x/7;
}

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as the return type is larger the result would be automatically casted to long!!! (long can hold an int)

Its only when the return type is smaller like a byte an explicit cast is required!!

byte test( int x, float y) {
return (byte)x/7;
}
 
Melanie Jones
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically, implicit cast happens for all "widening" conversion. Putting a smaller thing (int) in a larger container (long).

Conversely, putting a large value (int) into a small container (byte) is caller "narrowing". This requires explicit cast.

If more details are required on this refer K&B for 1.4 Ch#3 Pg - 113. (thats is if you have the book)

Thanks
Mel
 
Bee Dinu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my example, assume x=10 so mathematically result of 10/7 would be 1.42 but in the method its being truncated to 1 without any casting. Why would it be so ?

long test( int x, float y) {
return 10/7;
}
 
Melanie Jones
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using a "/" operator which would perform only integer division. The result of using this operator is only the integer part of the entire divison operation!! Hence the result is only 1 and not 1.42

Casting and the truncation has not connection.

Truncation is because of the operator "/"

- Mel
 
Bee Dinu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mel, thanks for your replies. I googled some more information on this and found that "/" can also perform floating point division provided atleast one of the operands is of type float.

Looks like "/" operator is overloaded.

I was thinking, it would depend on the result value and not on the operand types for determining the result type. And one would expect an error and not truncation unless the result is casted. I was wrong.

Thanks again for helping me clear this up. And I do have K&B and I love it !
 
Melanie Jones
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for the update too!!

I almost forgot "/" operation depends on the operand types!! Guess I was

All the best in your preparation!! 1.4 or 1.5??

- Mel
 
Bee Dinu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you !! I don't have any coding experience in Java, so I'm planning to take easier one (1.4).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the answer is simple
x is an int
x/7 is a division of two integer, so the result is an integer
an int is implicitly convert to long
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Minh Y Do

i think the answer is simple
x is an int
x/7 is a division of two integer, so the result is an integer
an int is implicitly convert to long



I dont think its x/7 , I think therei s a type and it should read x/y
otherwise whole question would not make sence,....
(and as number seven is right above letter 'Y' maybe it was unintentinal mistake )))
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic