Im confused about string...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    Im confused about string...

    1. when we initialize value,

    Code:
    String st = new String("the quick brown fox jump over the lazy dog");
    and

    Code:
    String st = "the quick brown fox jump over the lazy dog";
    both of them are correct right?

    Which of the two could consume greater memory space?

    And about comparing strings.

    Code:
    st.equals(another string)
    the code executes

    if
    Code:
    st.equals(null);
    then why we cannot compare it to null? NullPointerExce ption occured,

    Since the string could be compare into null...

    by
    Code:
    st == null;
    Is the method equals in String only accepts "" instead of null?

    Is the implementation of equals method is to get the parameter value then count the value's length?

    Im getting inspired already,
    Sukatoa
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sukatoa
    1. when we initialize value,

    Code:
    String st = new String("the quick brown fox jump over the lazy dog");
    and

    Code:
    String st = "the quick brown fox jump over the lazy dog";
    both of them are correct right?

    Which of the two could consume greater memory space?

    And about comparing strings.

    Code:
    st.equals(another string)
    the code executes

    if
    Code:
    st.equals(null);
    then why we cannot compare it to null? NullPointerExce ption occured,

    Since the string could be compare into null...

    by
    Code:
    st == null;
    Is the method equals in String only accepts "" instead of null?

    Is the implementation of equals method is to get the parameter value then count the value's length?

    Im getting inspired already,
    Sukatoa
    For the first part of your question, see the specs and wiki.

    For the second part, here's the implementation of the String.equals method

    [CODE=java]public boolean equals(Object anObject) {
    if (this == anObject) {
    return true;
    }
    if (anObject instanceof String) {
    String anotherString = (String)anObjec t;
    int n = count;
    if (n == anotherString.c ount) {
    char v1[] = value;
    char v2[] = anotherString.v alue;
    int i = offset;
    int j = anotherString.o ffset;
    while (n-- != 0) {
    if (v1[i++] != v2[j++])
    return false;
    }
    return true;
    }
    }
    return false;
    }[/CODE]
    Pulled from the Java sources.

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by r035198x
      For the first part of your question, see the specs and wiki.

      For the second part, here's the implementation of the String.equals method

      [CODE=java]public boolean equals(Object anObject) {
      if (this == anObject) {
      return true;
      }
      if (anObject instanceof String) {
      String anotherString = (String)anObjec t;
      int n = count;
      if (n == anotherString.c ount) {
      char v1[] = value;
      char v2[] = anotherString.v alue;
      int i = offset;
      int j = anotherString.o ffset;
      while (n-- != 0) {
      if (v1[i++] != v2[j++])
      return false;
      }
      return true;
      }
      }
      return false;
      }[/CODE]
      Pulled from the Java sources.
      Thanks for the Wiki...

      Im on my way,
      Sukatoa...

      Comment

      Working...