Results 1 to 3 of 3
  1. #1
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default string addition??

    class stringadd{
    public static void main(String ar[]){

    String a="10";
    string b="20";

    System.out.println(a+b);

    }

    }

    ................................

    how to print 30 instead of 1020
    .........................................

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Integer.parseInt(a) + .....

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    One of the confusing things for beginners. The meaning of the + operator depends on where/how it's used. When used with primiitives like int or long it means: arithmetic sum ie add the values of the two numbers and return their sum.
    When used between Strings, it means: concatenate the two strings and return a new String with the 2 strings concatentated. That is what you observed.

Similar Threads

  1. jTextField Simpe Addition - how to ? - Netbeans 6.0
    By jesicapalma in forum NetBeans
    Replies: 4
    Last Post: 05-24-2010, 09:21 AM
  2. Replies: 2
    Last Post: 06-02-2008, 05:30 PM
  3. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 AM
  4. Replies: 0
    Last Post: 03-28-2008, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •