• 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

Constructors question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!


What will be the output?
A1 4
A2 4 4
A3 Compiler Error
A4 Compiles and Runs without error

My question is why the answer is A3 not A1? why the compiler thinking this(4) in line 3 is calling Test6 (int) instead of calling existing one...public Test6(byte)? Would u guys help me out pls?

Thanks,
Huifen
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lu,

Any literal integer such as 4 is of type int by default. That's the compiler for you!! DON'T blame me though.

An explicit cast as follows would have solved the issue.

this((byte) 4);

Regards,
Priyanka.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
implicit narrower conversion does not take place during method call

eg byte m=67; //legal

however calling a(78) in following code is illegal::
void a(byte b){
}
reply
    Bookmark Topic Watch Topic
  • New Topic