different char handling from string | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1 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*; 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 - avatar
3 Antworten
+ 4
just write char x = 'c';
30th May 2018, 9:00 PM
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