r/javahelp • • Sep 07 '19

What is the difference between "char c = a" and "char c = 'A'"?

I've tried googling it, but I can't find an explanation..

2 Upvotes

4 comments sorted by

4

u/Swedophone Sep 07 '19

Have you tried writing a small program to find out?

4

u/gonnahike Sep 07 '19

I just did and came back here to say I didn't need help anymore.

I always need to put aposotrophes around the letter I want to put in a variable when using char. If I don't, I need to use Unicode

6

u/dudebobmac Sep 07 '19

And can you guess why that might be?

Hint: What's the difference between something with quotes and without quotes?

1

u/iamsooldithurts Sep 07 '19

You can use hex, octal, and binary numbers as well as Unicode.

The char type is actually a type of int in Java.

Being a char just tells Java to handle it a little differently from an int, mostly during output (converting to a String).

char c1 = ‘a’;
int i = c1;  //implicit upcast because char is type of int
char c2 = 0x0061; //hex
char c3 = 0141; //oct
char c4 = 0b0000000001100001; //bin
char c5 = ‘\u0061’;

String s1 = c1 + c2 + “”;
String s2 = i + i + “”;
String s3 = “”+ c4 + i;

Have fun!