how do we test for null values in single dimensional string array?
testing for null in a single dimensional array
Collapse
X
-
Loop through the array using a for statement*, and compare each element in the array using the == operator.Originally posted by veremuehow do we test for null values in single dimensional string array?
* http://java.sun.com/docs/books/tutor...bolts/for.html -
Hi,Originally posted by veremuehow do we test for null values in single dimensional string array?
Only String data type accepts null value. Integer or character does not accept null values. To test null value in string data type just use == operator in if condition like below,
if(str[3] == null)
{
}
Thanks,Comment
-
Not quite. Only Objects can point to a null reference, and java.lang.Integ er is an Object. You probably mean an int, which is true: just like any other primitive data type*, it cannot point to null.Originally posted by rsrinivasanHi,
Only String data type accepts null value. Integer or character does not accept null values. To test null value in string data type just use == operator in if condition like below,
if(str[3] == null)
{
}
Thanks,
* http://java.sun.com/docs/books/tutor...datatypes.htmlComment
-
That most certainly isn't true: for any non-primitive type T and an array of type T,Originally posted by rsrinivasanHi,
Only String data type accepts null value. Integer or character does not accept null values.
null is accepted as a valid value:[code=java]
class Person { ... }
...
Person[] persons= new Person[42];
...
// this is accepted:
persons[7]= null;[/code]
kind regards,
JosComment
-
I know, I know!Originally posted by JosAH... and who is the slowest old sod again? ;-)
kind regards,
Jos
; )
...Comment
-
Careful what you are saying there.Originally posted by rsrinivasanHi,
Only String data type accepts null value. Integer or character does not accept null values. To test null value in string data type just use == operator in if condition like below,
if(str[3] == null)
{
}
Thanks,
[CODE=java]Integer number = null;[/CODE]
compiles fine.
int and char (primitives) are the ones that cannot have a value of null.Comment
-
You can say that again; between Prometheuzz' lousy reply and my consistent,Originally posted by r035198xYack, I was so beaten there!
perfectly written article was just one single short minute but between my gem of
a reply and your short reply were five whole minutes! ;-)
kind regards,
JosComment
-
just declare any reference type and make it reference null.
TypeDeclared typevar = null;
then test for equivalence to these typevar like:
if ( x == typevar){ //statement here}
that's all.
String is also a reference typeComment
Comment