Ask Experts Questions for FREE Help !
Ask
    kiruthikalakha's Avatar
    kiruthikalakha Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 28, 2015, 06:52 AM
    On primitives
    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's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.



View more questions Search