2

hi there now I got a situation here.

String myStrings = new String();
myStrings = "10.42.0.1";
char[] charNum = myStrings.toCharArray(); //convert string to char array
char checkSum = 0;

A:

for(int i = 0; i <= 8; i++) { 
    checkSum += charNum[i]; 
}

B:

for(int i = 0; i <= 8; i++) { 
     checkSum = checkSum +charNum[i]; 
}

compile A: works just what I expect

compile B: Type mismatch: cannot convert from int to char

Since I know java will convert a+=b to a=char(a+b) implicitly, that why A works fine. But what really concern me is what's wrong with charNum[i]. ok, HeadFirstJava tells me that array is an object, so charNum[i] is a reference variable(4bytes), and I accept that. And charNum[i](reference variable) points to an char(2bytes), and I want to use that char(2bytes) to do arithmetical operation, but it looks like JAVA says NO. When I type

checkSum = checkSum+charNum[i];
( 2bytes )   =  ( 2bytes )  +      ( 4bytes )

it means that charNum[i] have 4bytes in memory, and it really confuse me, because I use char(2bytes) to do the math, not charNum[i](reference variable). And if it really use charNum[i](4bytes, reference variable) to do the math, why do I get the right value of checksum? SO can anyone tell me do I mistaken HeadFirstJava or do I miss some concept here?

And if you can explain this in detail, that would be highly appreciated.

thanks in advance.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
Lion Lai
  • 1,862
  • 2
  • 20
  • 41
  • Sotirios Delimanolis - before you comment, you should read what he is asking for (he pointed out he knows about it) – libik Feb 27 '14 at 01:11
  • 1
    The diadic `+` operator always returns at least an `int`-wide result. – Hot Licks Feb 27 '14 at 01:11
  • (This has nothing to do with the fact that one operand is an array element. The same would happen with two `char` variables.) – Hot Licks Feb 27 '14 at 01:13
  • @libik But he doesn't understand why `checkSum = checkSum+charNum[i];` doesn't work and that is explained in the various answers. – Sotirios Delimanolis Feb 27 '14 at 01:17
  • Sotirios Delimanolis - the question you are pointing him out is NOT what he is asking for, therefore you are wrong and you should not point him to it. – libik Feb 27 '14 at 01:20
  • @libik To respond to someone use `@their-name`. I'm not here to hold anyone's hand. The linked answer holds or links to all the information needed to answer this question. – Sotirios Delimanolis Feb 27 '14 at 01:22

2 Answers2

2

It is more simple than you think :).

If you add two char's, it creates int. Always.


And you are not right in few things : The values of cells in array of char is char, not reference, therefore charNum[i] is char and it is only 2 bytes width.

libik
  • 22,239
  • 9
  • 44
  • 87
2

The answer lies in binary numeric promotion. All operand are promoted to the type of the wider operand, and up to at least int. This is described by the JLS, Section 5.6.2:

2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

  • If either operand is of type double, the other is converted to double.

  • Otherwise, if either operand is of type float, the other is converted to float.

  • Otherwise, if either operand is of type long, the other is converted to long.

  • Otherwise, both operands are converted to type int.

(emphasis mine)

Therefore a char (checkSum) plus a char (charNum[i]) is an int.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357