Java: Subtract '0' from char to get an int… why does this work?

前端 未结 9 655
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 05:04

This works fine:

int foo = bar.charAt(1) - \'0\';

Yet this doesn\'t - because bar.charAt(x) returns a char:

int foo = bar.c         


        
9条回答
  •  [愿得一人]
    2020-11-27 05:28

    I will echo what @Mark Peters has said above in case people overlook his comment.

    As I quote: " Don't make the mistake of thinking that '0' == 0. In reality, '0' == 48 "

提交回复
热议问题