File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling Arg-Constructor... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling Arg-Constructor..." Watch "Calling Arg-Constructor..." New topic
Author

Calling Arg-Constructor...

Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
Warm Regards to All Java Ranchers...

Please have a look at the following program...

public class Test7{
public Test7(){
this(4);
}
public Test7(short var){ // Line-1
System.out.println(var);
}
public static void main(String[] args){
Test7 t7 = new Test7();
}
}


This code gives compile time error. The problem is with the constructor call
this(4);

If I change the data type in Line-1 from short to int, the code compiles fine. Why am I getting this error although 4 lies in the range of short type..?

Thanks..
Rekha
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

hi Rekha Anand ,
All the integer values in java are by default considered as int according to the java language specification.

So when you call a constructor which accepts an int value (4) as a short it gives an error. Since int is much bigger and there might be a loss of precision.

but even when you typecast (4) to short something like ((short)4) then it is going to work fine because you are explicitly telling the compiler to consider the value (4) to be of type short.

hope this helps


-kolli
Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
Thanks!! It helped.

Rekha
 
Consider Paul's rocket mass heater.
 
subject: Calling Arg-Constructor...
 
Similar Threads
constructor doubt
wrong output?
Interface...
wrong output?
Objects by value/reference