• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Implicit Cast from Int to Char

 
Greenhorn
Posts: 4
MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above program, there are 2 contradicting scenarios for me.

Scenario 1
In the above program below line works fine.

char c [] = new char []{'\u0000', 'C', 'A', 65};

This means 65 which is an int, is some how (implicitly) converted to char and stored inside the char array.

Scenario 2
And, in the above program below line (now commented) gives me a compilation error.

new A().m(65);

This means, the method m() which takes a char input argument does not take an int literal as an argument. That means in this situation

the int value is not implicitly converted to char.


So, in scenario 1 - int is implicitly converted to char
in scenario 2 - int is not implicitly converted to char
why is this contradiciting behavior ?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the designers prefered to keep all implicit casts within the same method. I'm sure there is a paragraph on this somewhere in the Java Language Specification.

It's simply not allowed to pass arguments to methods that don't have the same type, whether you can cast them implicitly or not.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the reference in the Java Language Specification that Stephan refers to:

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion (§5.2). The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process (§15.12.2).

 
buddhika Gunasekara
Greenhorn
Posts: 4
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys for your valueble information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic