+ 1

java.io different char handling from string

Does anybody know what is going on here? if I change out char for string, this runs and works, but now it is telling me it expects [] instead of (). When I change them to that it tells me I can't have incompatible types and dereference the char. import java.io.*; class In { public static void main(String[ ] args) { char x = new char('c'); //was String x = new String("c"); System.out.print("Return Value :" ); System.out.println(x.toUpperCase() ); } }

30th May 2018, 8:52 PM
Michelle
Michelle - avatar
3 Réponses
+ 4
just write char x = 'c';
30th May 2018, 9:00 PM
Stefanoo
Stefanoo - avatar
+ 4
You are treating x as an object, so it's a must to declare it as one. Character x = new Character('c'); On the contrary, 'char' is a primitive type. That's the first thing. The second would be that there isn't a toUpperCase() method in class Character.
1st Jun 2018, 3:22 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
You can not create an object from a primitive data type. The keyword new on primitive data type is used for creating an array. But you may want to use... char x = 'c'; System.out.println(Character.toUpperCase(x));
30th May 2018, 9:26 PM
Elmer Martens
Elmer Martens - avatar