I believe there is no "empty" representation for the type char
which is primitive, like ''
, then you have to look at it differently. I looked at the class documentation Character
and I found the MIN_VALUE which is a constant for the value '\u0000'
which in this case is 0
(zero).
Take an example:
import java.lang.*;
class Main
{
public static void main(String[] args)
{
char[] arr = new char[] {'a','b', Character.MIN_VALUE};
if (arr[1] == Character.MIN_VALUE)
{
System.out.println("vazio");
}
else
{
System.out.println("nao vazio");
}
}
}
Exit:
nonempty
Therefore, to fix better see this table with default values for all primitive types:
Table source
If you want "empty" values in the array you can use this constant. There is this question also. Always seeks to prioritize documentation in your searches, if there is not there you can ask here.
You need to define what is empty first. The two cases do not exist, one is asking if there is a null reference, which is impossible since one
char
is not a reference. The second is checking if it has 0 Charas, but it is certain that it has 1char
there, you have reserved space for it, then there is no way not to have. So what is empty? These two conditions are impossible.– Maniero
Check
arr[i] == 0
you check if that position is being used, but you need to define better even what you want as the Maniero said.– user28595
You did
char[] arr = new char[10];
then your array has 10 characters and each position starts with the character0
('\0'
) not to be confused with the character'0'
. And you always have to have a character in every position.– Isac
I changed the question... I want to know if it contains any characters in that position or if nothing has been inserted yet.
– Lucas Pletsch
As already spoken 3 times, it contains a character obligatory. You must then determine what you want. If you just want to know if it contains any characters, you don’t have to do anything, you already know it contains them. Now if you want to know if you have certain specific characters you need to define this.
– Maniero
@Isac, if you post your comment as reply I register as solved...
– Lucas Pletsch
This here is a XY problem. Why do you want "check whether a given position of an array of chars actually contains some character"? Where are you going with this?
– Victor Stafusa
In an array of chars, all the positions at all times has a character. This happens with any array of primitive types. So, why do you want to do this?
– Victor Stafusa
Thank you for clarifying that Victor. I didn’t know. I thought that every space in the array would be filled with "nothing". I decided to check if the position had the character I seek. (in this array would only have that character that interests me, what you have in the rest doesn’t matter).
– Lucas Pletsch