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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

constructor doubt

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can someone help me understand why the compiler gave me error with following code on the line this(4)

public Test6{
public Test6(){
this(4);
}
public Test6(byte var){
System.out.println(var);
}

public static void main(String[] args){
Test6 t6 = new Test6();
}
}

Thanks
Mary
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Mary,

I believe the issue is literal integers are always implicitly ints so to call a byte constructor with a literal int you would need to explicitly cast it: this((byte)4);

Thanks,

Dave
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by dave natx:
Hi Mary,

I believe the issue is literal integers are always implicitly ints so to call a byte constructor with a literal int you would need to explicitly cast it: this((byte)4);

Thanks,

Dave



Hi dave,

I am confused and need some help in understanding this.

It is true that literal integers are always implicitly int.

However, AFAIK, a literal integer such as 4 should be implicitly casted into byte which can take upto 2^7-1 = 127.

eg/
byte var = 4; // this works without explicit cast

I thought explicit cast is only required when you are trying to assign
bigger value into smaller container.

Is this incorrect for invoking overloaded constructors/methods ?
 
Mary John
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi jason,

in my other post (accidently posted the same topic, btw)Ahmed mentioned that byte casting works only for declarartions. This prompted me to search the web for related issue. and I got this. ----- please read
http://java.sys-con.com/read/37817.htm

It says that integers wont be implicitly cast to a byte, short or char even if in the acceptable range, for methods or constructors to make sure that overloading with int, byte, short etc works as expected.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Double post, please continue the discussion in the other post.
 
    Bookmark Topic Watch Topic
  • New Topic