简体   繁体   中英

Why Char won't print, and counter exceeded my loop;

I am trying to run this three simultaneous threads, but when I do system.print Char won't come out, and the counter "i" went over bound.

somehow, I add an string in front of Char, it print out correctly, anyone can explain to me why is this happening?

    public class Part2 {
    public static void main(String[] args) {            
        Runnable printA = new PrintChar('a');
        Runnable printB = new PrintChar('b');
        Runnable printC = new PrintChar('c');

        Thread t1 = new Thread(printA);
        Thread t2 = new Thread(printB);
        Thread t3 = new Thread(printC);

        t1.start();
        t2.start();
        t3.start();     
    }

    private static class PrintChar implements Runnable { 
        private char c;
        public PrintChar(char c) {
            this.c = c;
        }

        public void run() 
        { 
            for(int i = 1; i<=100; i++) {
                System.out.print(c + i + ", ");
            }
        } 
    } 
    }

/*this is the output of this code: 98, 100, 101, 102, 103, 104, 105, 99, 99, .... 198, */

/*if I add a String before Char like this This is the output I expected; */

        public void run() 
        { 
            for(int i = 1; i<=100; i++) {
                System.out.print("" + c + i + ", ");
            }
        } 

/* b1, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,.... a1 ~ a100 b1 ~ b100 and c1 ~ c100 runs and finish simultaneously */

When you use the + operator on a char and an int , it performs arithmetic addition, not string concatenation. Putting "" + first means you're first doing "" + c , which is concatenation into a String , then adding that String to an int , which is another concatenation.

+ operator has two meanings in Java. It can represent:

  • addition if both operands are numeric types, and char is considered as numeric type because internally its value is number representing position of character in Unicode Table like for 'a' it would be 97 (as decimal, or 61 as hexadecimal).

  • concatenation if at least one operand is String.

Also since evaluation goes from left to right c + i + ", " is equivalent of (c + i) + ", " . At (c + i) both c and i are numeric types so + represents addition and calculates 'a'+1 where 'a' as earlier mentioned is treated as 97, so you are seeing result of 97+1 which is 98.

After that next operation is 98 + ", " and here second operand is String, so here + represents concatenation. So 98 + ", " results in String "98, " .


In case of "" + c + i + ", " because of left-to-right evaluation order we can express it as (("" + c) + i) + ", " .

  • At first ("" + c) would "calculated" and since first operand is String + represents concatenation, so we end up with "" +'a' results in String "a" .
  • Next "a" + i is executed, and here also first operand is of type String so + also represents concatenation which results in String "a1"
  • And finally "a1" + ", " is executed. Here both operands are Strings so + is concatenation which results in "a1, " string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM