+ 5
What is/are the difference of the two? Why they have different output?
in C++ cout<<'h'<<'i'; Output is: hi But in Java System.out.print('h'+'i'); Output is: 209 I am confused
15 Answers
+ 13
In both C++ and Java:
'h'+'i'
is 209 as 'h' is 104 and 'i' is 105.
cout << 'h'+'i'; // outputs 209.
+ 13
This is bcoz of ASCII values since computer does not understand characters and in Java we need to use " " for printing characters.
+ 8
Your C++ code prints two characters. The equivalent Java is:
System.out.print('h');
System.out.print('i');
+ 6
Make a string.
""+'h'+'i'
will make "hi" in both languages. Also:
"h"+"i"
will do it in both languages.
+ 5
Because each << operator makes a separate print call.
+ 4
You are welcome.
+ 2
Ahh ok, tnx a lot 😇
+ 2
In 1st thing with C++ you just print to chars to console. But in 2nd with java you perform a char concatenation which is actually an addition of values of the 2 char values.
+ 2
check this link... https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
+ 1
Why I tried to compile cout<<'h'<<'i'; in c++ the out is "hi"?
+ 1
So how can I concatenate two characters in c++ that the output is like in java?
+ 1
Nice
Ok
+ 1
in both languages 'h' + 'i' is 209, because it's the sum of the ascii values
if u use cout << 'h' + 'i'; in c++ it would also output 209
cout << 'h' << 'i'; is like cout << 'h'; cout << 'i';
which would be the java equivalent of System.out.print('h'); System.out.print('i');
+ 1
Tnx, for all your help , now I know 😇
0
https://code.sololearn.com/WFDiadEBecAj/?ref=app
Minh Dang Ngoc please delete this spam. We only post on topic posts here.