+ 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

14th Dec 2018, 11:57 PM
Rovie M. Francisco
Rovie M. Francisco - avatar
15 Respostas
+ 13
In both C++ and Java: 'h'+'i' is 209 as 'h' is 104 and 'i' is 105. cout << 'h'+'i'; // outputs 209.
15th Dec 2018, 12:10 AM
John Wells
John Wells - avatar
+ 13
This is bcoz of ASCII values since computer does not understand characters and in Java we need to use " " for printing characters.
16th Dec 2018, 4:58 AM
Ananya Srivastava
Ananya Srivastava - avatar
+ 8
Your C++ code prints two characters. The equivalent Java is: System.out.print('h'); System.out.print('i');
15th Dec 2018, 12:13 AM
John Wells
John Wells - avatar
+ 6
Make a string. ""+'h'+'i' will make "hi" in both languages. Also: "h"+"i" will do it in both languages.
15th Dec 2018, 12:21 AM
John Wells
John Wells - avatar
+ 5
Because each << operator makes a separate print call.
15th Dec 2018, 12:15 AM
John Wells
John Wells - avatar
+ 4
You are welcome.
15th Dec 2018, 12:22 AM
John Wells
John Wells - avatar
+ 2
Ahh ok, tnx a lot 😇
15th Dec 2018, 12:22 AM
Rovie M. Francisco
Rovie M. Francisco - avatar
+ 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.
15th Dec 2018, 1:15 PM
Seniru
Seniru - avatar
18th Dec 2018, 5:27 AM
Nitin Gutte
Nitin Gutte - avatar
+ 1
Why I tried to compile cout<<'h'<<'i'; in c++ the out is "hi"?
15th Dec 2018, 12:14 AM
Rovie M. Francisco
Rovie M. Francisco - avatar
+ 1
So how can I concatenate two characters in c++ that the output is like in java?
15th Dec 2018, 12:18 AM
Rovie M. Francisco
Rovie M. Francisco - avatar
+ 1
Nice Ok
15th Dec 2018, 1:57 PM
Rovie M. Francisco
Rovie M. Francisco - avatar
+ 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');
16th Dec 2018, 3:19 PM
Azad Ad
Azad Ad - avatar
+ 1
Tnx, for all your help , now I know 😇
23rd Dec 2018, 11:19 AM
Rovie M. Francisco
Rovie M. Francisco - avatar
0
https://code.sololearn.com/WFDiadEBecAj/?ref=app Minh Dang Ngoc please delete this spam. We only post on topic posts here.
15th Dec 2018, 10:29 PM
Minh Dang Ngoc
Minh Dang Ngoc - avatar