This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Why no compiler error? I thought we have 2 constructors of Process8(int x) // cons 1 and Process8(short s) . When we pass new Process8('J') from line aa I thought.... Implicit promotion of char to int and it will get in to cons 1. I also thought that it may be confused between cons 1 & 2 as short s can also be seen as one possible thing for promotion to int type... (Am I Right.. Appears to be bit confused here) Sooooo I thought for long & inferred that it will show compiler error. But the program compiles fine.. Help me in walking through the code. Ques No 2. The line marked //cc passes V in to the constructor; But no o/p. Why. Thanks in advance. tvs sundaram ------------------------ code ------------------------ <ubb> class Process8 { boolean b; public Process8(int x) { System.out.println((char)x); } public void Process8(char c) { if(b = java.lang.Character.isJavaIdentifierStart(c)) System.out.println(b); } public Process8(short s) { System.out.println((char)s); } } public class Test8 { Process8 p3 = new Process8('V'); // cc public static void main(String[ ] args) { Process8 p4 = new Process8('_'); } static Process8 p1 = new Process8('J'); // line aa static Process8 p2 = new Process8('$'); } </ubb> ---------------------------------------------
David Weitzman
Ranch Hand
Joined: Jul 27, 2001
Posts: 1365
posted
0
The type of a primitive can be implicitly promoted if necessary. The key to this is the 'if necessary' -- which does not apply in your example. There is a contructor for characters available, so the compiler need look no further.
Cameron Park
Ranch Hand
Joined: Apr 06, 2001
Posts: 371
posted
0
When you pass a short value as a parameter, the compiler will look for constructor/method that has short parameter. If it can find one, that one will be used. If compiler can not find one, one that has parameter types that can be implicitly converted will be called, and the short value will be converted to whatever the parameter type is(usually int but really anything but char or byte).
swati bannore
Ranch Hand
Joined: Oct 18, 2000
Posts: 201
posted
0
Hi, I modified the code little bit to see , which constructor is actually called.To my surprise, even if u send a char literal, the constructor with int parameter is invoked.WHy so ?
And the it gives output as - From Int Version J From Int Version $ From Int Version _ From Int Version V Thanx
Hei David, That char argument taket Process() is not a constructor, but only a method! TVS, ur line cc is also of question to me. But i think it is non-staitic initialzer expression (not initializer code block) wherese line aa and later is static initializer expresions. So it does not give any compiler error.... Correct me if i m wrong.
------------------ azaman
Ashik Uzzaman Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
swati bannore
Ranch Hand
Joined: Oct 18, 2000
Posts: 201
posted
0
Thanx aashik, See, I did not read the code carefully and mistook method for constructor.. i got it now.
sriram gupta
Ranch Hand
Joined: Aug 09, 2001
Posts: 39
posted
0
even if there are two methods public void a1(int i){} public void a1(char i){} and then if there is some call for a1 with short argument then first method will be called as char byte and short are converted to int...
Mini Pilla
Ranch Hand
Joined: Jul 15, 2001
Posts: 112
posted
0
For tvs, U'r question NO 2: There is no instance created for the class Test8.So Process8 p3 = new Process8('V'); // cc Does not execute!!! Thanx Rajani
tvs sundaram
Ranch Hand
Joined: Jan 28, 2001
Posts: 153
posted
0
Thank u all. I think i have understood where the problem was. tvs sundaram