• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

char assignment behaviour when assigning int values

 
Greenhorn
Posts: 10
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could someone please explain it to me the reason for the char assignment behavior?

public class Test {
public void take (char c){
System.out.println(c);
}
public static void main(String[] args) {
new Test().take(200); /// Compilation fails
}

}

class Test2 {
public static void main(String[] args) {
char c = 200;
System.out.println(c);
}
}

Why does compilation fails in Test class and the int assignment succeeds in the Test2 class?
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while compiling Test.java at this point compiler searches for a method in class Test whose parameter is int because 200 value is considered as int (default data type for whole number) so it finds a method whose parameter is char so gives compilation error can't find symbol.

In Test2 as I've told you 200 is an int value and here we are assigning large data type to small data type so narrowing conversion takes place implicitly as char range is 0 to 65,535. The char value for 200 is '?' so prints '?' without any error. try assigning 65,536 gives error possible lossy conversion from int to char because it transcends char range limit.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS: 5.3. Method Invocation Conversion
this may help.

Method invocation contexts allow the use of one of the following:
an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by widening reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.



pay note that method invocation not use narrowing conversion, that is differ from assignment:

but you can`t

 
G Batra
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sergej. That JLS link is really helpful.
 
Ranch Hand
Posts: 40
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed really helpful! I knew about the narrowing conversion of constant expressions when assigning so I was surprised this doesn't work for method invocation. Indeed in the linked JLS, method invocation does not list the narrowing conversion but for assignment conversion it additionally says:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic