• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assingment and casting

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Genius,

I am referring Kathy & Sierra Book. I am stuck with a question on a assingment chapters' Self Test.
It has a following code...





What is the result :
A. Many
B. A few
C. Compilation fails.

answer C is correct and book has following explanation for this

}

compilation fails. The var-args declaration is fine, but invade takes a short, so the argument 7 need to be cast to a short. with the cast, the answer is B, 'a few'



i dont understand y the compilation is failing and why do we have to cast it....argument 7 is well within the range of short limit.
and even if we require to cast how would we do it .

any help is much appreciated

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

Madan wrote: i dont understand y the compilation is failing and why do we have to cast it....argument 7 is well within the range of short limit.
and even if we require to cast how would we do it .

any help is much appreciated




Assigning 7 to short: Compiler only has this power, means these things are possible at compile time, where compiler checks range of value and assign to short via implicit casting.

Now this is done in argument passing, so here come JVM boss, that will not do any implicit casting for you.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madan Mohan wrote:



Madan I think you added too many dots there. It should be short... not short......
 
Madan Mohan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Punit...


I am sorry but I am yet not clear on what you are trying to convey

Best Regards
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you write:



It works as it is evaluated at compile time by the compiler only when .java is converted to .class file. And compiler treats 7 as constant and checks it value range, if it is assignable to short then it assigns 7 to it.

when you write:


then assigning 7 to short s is delayed to runtime and performed by JVM, and JVM treats 7 as int type,
It will complain you saying "cannot assign int to short".
so it asks for explicit casting.
like:
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:then assigning to 7 to short s is delayed to runtime and performed by JVM,


I feel this sentence could be better phrased, just feel this is telling the right thing in a wrong way.
Maybe, it can be elaborated a bit more to be accurate........ my feelings though....
After all, we get a compile error.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe what you say is right, but I see no reason why method(7) couldn't be made to work. It's true that the invocation is made at runtime, but the compiler could analyze the method signature, see that 7 is a compile time constant in range of short, and insert a cast at compile time. It's just that that's not the way things work. Maybe I'm missing some other reason why it's not allowed.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic