• 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

Question on casting

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book (pg no 267) ques no :3

class a{
String inv(short s){return "inv";}
String inv(short... s){return "var arg";}
}
class b
{
public static void main(String args[])
{
System.out.println(new a().inv(7)); //compilation fails
}
}

the ans is:compilation fails since there is no explicit cast
during a noraml assignment short s=7; succeeds but y does it fail wen it is passed as an argument
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a thread about this a few posts back. when you say short s = 7; its not the same as passinga short as an argument to a function. The least that a function will accept is an int. You MUST cast if an implicit cast will not work. For example...

Short s = new Short(1); will not compile

But this will

Short s = new Short ( ( int ) 1 );

So remember to spot errors when data is passed into a method/constructor as an argument.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think in method calls we can send same or widening types.but here when we went send it it is kind of narrowing this as the method is expecting a short.

this is my idea regaridng method calls.
thanks
 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think in method calls we can send same or widening types.but here when we went send it it is kind of narrowing this as the method is expecting a short.

this is my idea regaridng method calls.
thanks
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicit cast for primitives takes place only when assigning variables.

For method calls this rule is not valid. Implicit cast for primitives does not take place for method calls.

You have to pass the exactly argument the method expects.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short s = new Short ( ( int ) 1 );

this is simillar to

Short s = new Short (1 );

both won't compile.

the arument is int and hence it won't compile, cast it to short as Short class constructor has short as an argument.

i.e ,

Short s = new Short ((short)1);






Originally posted by John Meyers:
There was a thread about this a few posts back. when you say short s = 7; its not the same as passinga short as an argument to a function. The least that a function will accept is an int. You MUST cast if an implicit cast will not work. For example...

Short s = new Short(1); will not compile

But this will

Short s = new Short ( ( int ) 1 );

So remember to spot errors when data is passed into a method/constructor as an argument.





"Nothing is impossible even impossible has i 'M POSSIBLE"

santhosh
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm... my brain is begeinning to rot. Thanks for pointing that out. Yes the cast should have been for a ( short ). Sorry.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing you have to remember is that there is <b>no implicit broadening conversion for argument passing</b>...

implicit broadening conversion is the one

destination type should be char,short,byte
source should be int,char,short,byte constant values

example char a=27;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic