PDA

View Full Version : On primitives


kiruthikalakha
Oct 28, 2015, 06:52 AM
public static void main(String[] args) {
// TODO code application logic here
char a='H';
int b=3110;
String c="WOR";
int d=1;
char e ='d';
double f=2.0;
boolean g=true;
String s =a+b+c+d+e+f+g;
System.out.println(s);
}


I want to display H3110WOR1d2.0true
but the o/p displayed is 3182WOR1d2.0true.

Why and how to correct it??

CravenMorhead
Oct 28, 2015, 07:45 AM
First off, look at the ascii code value for H. I will give you a hint. It is 72 decimal. Char are often used as single byte numerical values so the initial two terms is being treating as an addition instead of appending. So what you need to do is figure out how to do the string addition you're doing without causing summation but rather appending.