+ 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() ); } }
3 Réponses
+ 4
just write char x = 'c';
+ 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.
+ 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));