Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why no compiler error?

 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx aashik,
See, I did not read the code carefully and mistook method for constructor..
i got it now.
 
sriram gupta
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u all.
I think i have understood where the problem was.
tvs sundaram
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic