Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Is it a bug or featuere

Is it a bug or featuere

Scheduled Pinned Locked Moved Java
help
6 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amarap
    wrote on last edited by
    #1

    In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

    M G L T G 5 Replies Last reply
    0
    • A Amarap

      In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

      M Offline
      M Offline
      Manfred Rudolf Bihy
      wrote on last edited by
      #2

      The plus operator is left associative so I'd expect 21 as the result. 22 does not make any sense whatsoever. What happens when you put i + 1 in parenthesis like this: (i + 1). Try that and think about associativity of operators and what it means to the process of evaluation/compilation. Cheers!

      "With sufficient thrust, pigs fly just fine."

      Ross Callon, The Twelve Networking Truths, RFC1925

      1 Reply Last reply
      0
      • A Amarap

        In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

        G Offline
        G Offline
        Gerben Jongerius
        wrote on last edited by
        #3

        The reason you are not getting 'Run 3' is because Java is thinking about string concatination. As it is processing from left to right you would see: "Run 2" + 1 + "" But this is where it gets a bit strange, I would expect to see the number 1 printed next giving you: "Run 21" If you want to add two numbers you must use brackets to indicate priority: System.out.print("Run "+ (i+1));

        1 Reply Last reply
        0
        • A Amarap

          In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          ..if the system had these kind of "bugs" or "features", they would have been found by now, don't you think? :)

          1 Reply Last reply
          0
          • A Amarap

            In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

            T Offline
            T Offline
            TorstenH
            wrote on last edited by
            #5

            The others have already explained why it is so. I would like to add into, that the + is an operator and is overloaded in the String class and also used different in the System.out. Operators in Java[^]

            regards Torsten When I'm not working

            1 Reply Last reply
            0
            • A Amarap

              In this code int i =2; System.out.print(" Run " + i+1 + " "); It prints Run 21 Where I would have expected to print Run 3 Amara 'Now I understand how stupid I am, thanks for everybody's contribution.

              G Offline
              G Offline
              Gowtham Gutha
              wrote on last edited by
              #6

              int i =2; System.out.print(" Run " +(i+1)+ " "); This is the code. Keep the brackets.

              Gowtham Gutha (Java-Demos.Blogspot.Com)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups