1

Just comparing some behaviors accross different languages and in java (java 8) I have tried something and I got weird results:

here I iterate over a small String array, and display the iterator along with the value (nothing fancy, we agree...). however when I change in the sysout concatenation between single quote ' ' and double quote " " the behavior is completely different. 1- displays the end result of the iteration (3 as I increment through the loop) and it seems that it calculates + 1 from the last iteration so in this case the end result is:

33Foo 34Bar 35Baz

public static void main(String[] args) {

    int y = 0;
    
    String[] arr= {"Foo", "Bar", "Baz"};    
    for(int i = 0; i < arr.length; i++) {
    
        y++;
        System.out.println(y +' '+ arr[i]);
    }

}

while changing the above to double quotes:

 public static void main(String[] args) {

    int y = 0;
    
    String[] arr= {"Foo", "Bar", "Baz"};    
    for(int i = 0; i < arr.length; i++) {
    
        y++;
        System.out.println(y +" "+ arr[i]);
    }

}

will have the expected result:

1 Foo 2 Bar 3 Baz

Anyone knows why? Just interested to know the reason of the impact using single vs double quote...

Polygnome
  • 7,639
  • 2
  • 37
  • 57
kicksdev
  • 11
  • 3

2 Answers2

4

Single quotes are used for the char type. Since this is a numerical type, doing int + char will result in an int value.

A space character (' ') has a numerical value of 32, so:

1 + ' ' == 33; 
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Ok however it takes the end result of the iteration i.e. 3 and iterates from there so 3,4,5 because the end result is 33Foo then 34Bar then 35Baz, do you see what I mean? – kicksdev Jun 25 '20 at 09:02
  • 2
    It does not take the end result of the iteration. 1 + space = 33, 2 + space = 34, 3 + space = 35. – Robby Cornelissen Jun 25 '20 at 09:04
  • Ok I see it takes the ascii value for it...so first question answered thanks very much for that :) but why doesn't it iterate and give the last value instead? – kicksdev Jun 25 '20 at 09:06
  • @kicksdev You start with `y=0`, then do `y++`. Now `y` is `1`. `1 + 32` (32 being the value of the char `' '`) is 33. `33 + arr[0]` = `33Foo` and so on. – Polygnome Jun 25 '20 at 09:06
  • @kicksdev What do you mean it doesn't iterate? it prints three lines. – Polygnome Jun 25 '20 at 09:07
  • Well, it goes beyond ASCII. It's a 16-bit unicode character. The lower range of that indeed corresponds to ASCII. – Robby Cornelissen Jun 25 '20 at 09:08
  • ok let's reformulate, it does iterate but it doesn't give the index value or in this case the variable value while iterating as it gives the last value i.e 3 instead of having for each line the actual variable value 1,2,3 – kicksdev Jun 25 '20 at 09:11
  • You're adding the index value to 32. That's what is printed. – Robby Cornelissen Jun 25 '20 at 09:13
1

What I believe is happening is Java is treating the space as its ASCII number and adding the index value of the actual array. This is because Java allows for type conversion. So when you concatenate a number with say ' ' it actually treats them as an adding operation instead of a concatenation.

So for example,

  1. 33 Foo is actually 32+1 34
  2. Bar is actually 32+2
  3. 35 Baz is actually 32+3

enter image description here

alfie
  • 157
  • 10