1. 程式人生 > >報錯:char cannot be dereferenced

報錯:char cannot be dereferenced

error: char cannot be dereferenced

1.出錯程式碼

	if(row.charAt(m).equals('0')){
		matrix[i][j]=0;
	}

其中row是一字串。

2.錯誤原因

derefrence:被間接引用的;所指向的值

The type char is a primitive – not an object – so it cannot be dereferenced
Dereferencing is the process of accessing the value referred to by a reference

. Since a char is already a value (not a reference), it can not be dereferenced.
use Character class:
if(Character.isLetter())

以上解釋的很清楚了:
char是基本資料型別,已經是值了,這裡比較值可以直接用==。
Character是其包裝類,引用型別,才可以用equals()方法。==反而不能直接比較值了。