aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit promotion for constructor arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit promotion for constructor arguments" Watch "Implicit promotion for constructor arguments" New topic
Author

Implicit promotion for constructor arguments

Andreas Sandberg
Ranch Hand

Joined: Sep 14, 2005
Posts: 31
Hi Folks,

Can somebody explain why the code below compiles. Why does Main a = new Main(3) call the constructor that takes the float argument. How does JAVA handle implicit promotion for constructor arguments? Thanks!



"When the compilers not happy ain't nobody happy"
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
That's because an int can be automaticly converted to a float (or to a long, or a double)


Regards,<br />Seb<br /> <br />SCJP 1.4
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,

int value 3 is converted into float. So Math(3) is executed. For more information about casting refer this link.

http://www.javaranch.com/maha/Resources/CarlsNotes.txt


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

You can look at this way


byte->short->int->long->float->double

In fact here you can have char converts into int...apart from short...

char->int..



[ October 20, 2005: Message edited by: A Kumar ]

[ October 20, 2005: Message edited by: A Kumar ]
[ October 20, 2005: Message edited by: A Kumar ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Try it with code tags...

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi.

Thanks Henry...Thats what i was trying to convey...


Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
Andreas
In your constructor Main(float i) you pass the float value for i in but you never assign it to anything. The this() call your constructor with no arguments which is always going to assign the value 3 to i.
 
jQuery in Action, 2nd edition
 
subject: Implicit promotion for constructor arguments