testing for null in a single dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veremue
    New Member
    • Mar 2007
    • 14

    testing for null in a single dimensional array

    how do we test for null values in single dimensional string array?
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by veremue
    how do we test for null values in single dimensional string array?
    Loop through the array using a for statement*, and compare each element in the array using the == operator.

    * http://java.sun.com/docs/books/tutor...bolts/for.html

    Comment

    • rsrinivasan
      New Member
      • Mar 2007
      • 221

      #3
      Originally posted by veremue
      how do we test for null values in single dimensional string array?
      Hi,

      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

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by rsrinivasan
        Hi,

        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,
        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.

        * http://java.sun.com/docs/books/tutor...datatypes.html

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by rsrinivasan
          Hi,

          Only String data type accepts null value. Integer or character does not accept null values.
          That most certainly isn't true: for any non-primitive type T and an array of type T,
          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,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            ... and who is the slowest old sod again? ;-)

            kind regards,

            Jos

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #7
              Originally posted by JosAH
              ... and who is the slowest old sod again? ;-)

              kind regards,

              Jos
              I know, I know!
              ; )

              ...

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by rsrinivasan
                Hi,

                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,
                Careful what you are saying there.
                [CODE=java]Integer number = null;[/CODE]
                compiles fine.
                int and char (primitives) are the ones that cannot have a value of null.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  ... and who is the slowest old sod again? ;-)

                  kind regards,

                  Jos
                  Yack, I was so beaten there!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Yack, I was so beaten there!
                    You can say that again; between Prometheuzz' lousy reply and my consistent,
                    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,

                    Jos

                    Comment

                    • emekadavid
                      New Member
                      • Mar 2007
                      • 46

                      #11
                      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 type

                      Comment

                      Working...