Results 1 to 12 of 12
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Question Array in Java '\0' ?

    Does character array in java has '\0' by default in the end?

    For example:

    char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', ' ', ' ', ' ', ' ', ' ', ' '};

    The Length of above character arrays is 13. So, if I want to start from backwwards then can I initialize a variable calles, newlength = '\0' and start working
    backwards?

    Please clarify.

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array in Java '\0' ?

    Java is not C nor C++; Java doesn't use the '\0' character to indicate the end of string ('end of char array' actually in C and C++). Java keeps a 'length' field for arrays so it can do bound checking behind the scenes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Array in Java '\0' ?

    Thanks for your reply. Actually, I'm confused with the following program which has "str[newLength] = '\0';" . Why '\0' is mentioned here?

    The following program is replacing all the spaces in the strigng with '%20'.

    Please explain.

    ================================================== ================================================
    public class Test {
    public void replaseSpaces(char[] str, int length) {
    int spaceCount = 0, newLength = 0, i = 0;

    for(i = 0; i < length; i++) {
    if (str[i] == ' ')
    spaceCount++;
    }

    newLength = length + (spaceCount * 2);
    str[newLength] = '\0';
    for(i = length - 1; i >= 0; i--) {
    if (str[i] == ' ') {
    str[newLength - 1] = '0';
    str[newLength - 2] = '2';
    str[newLength - 3] = '%';
    newLength = newLength - 3;
    }
    else {
    str[newLength - 1] = str[i];
    newLength = newLength - 1;
    }
    }
    System.out.println(str);
    }

    public static void main(String[] args) {
    Test tst = new Test();
    char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', ' ', ' ', ' ', ' ', ' ', ' '};
    int length = 6;
    tst.replaseSpaces(ch, length);
    }
    }

    ================================================== ================================================== =

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array in Java '\0' ?

    That's the 'mechanical' way of doing things in Java; the following snippet replaces spaces with "%20" in a String:

    Java Code:
    String myString= "the dog and   the cat";
    myString= myString.replaceAll("\\s", "%20");
    System.out.println(myString);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Array in Java '\0' ?

    Thanks for your answer but could you also explain my question pertaining to my program mentioned above?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array in Java '\0' ?

    Quote Originally Posted by Jack_Tauson_Sr View Post
    Thanks for your answer but could you also explain my question pertaining to my program mentioned above?
    You mean an explanation of that program? It is incorrect; try to run it and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Array in Java '\0' ?

    Ask whoever wrote it.
    Because I suspect they simply took a C program and rewrote almost word for word into Java, without actually knowing what they were doing.
    Please do not ask for code as refusal often offends.

  8. #8
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Array in Java '\0' ?

    I didn't mean explanation of my program. The program is running perfectly fine:

    I repeat my question once again pertaining to the program I mentioned above:
    "Actually, I'm confused with the following program which has "str[newLength] = '\0';" . Why '\0' is mentioned here? "

    Please explain.

    I hope this clarifies my question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Array in Java '\0' ?

    And as I said, ask whoever wrote it about why they thought it was useful.
    As far as I can make out it's not.
    Please do not ask for code as refusal often offends.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array in Java '\0' ?

    Quote Originally Posted by Jack_Tauson_Sr View Post
    I didn't mean explanation of my program. The program is running perfectly fine:
    How strange; when I tested it the output was "the%20do" (plus an invisible '\0' character and a bunch of trailing spaces).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Array in Java '\0' ?

    Actually, the value of length should be 7 instead of 6. Then you will get "the%20dog" as your output.

    As we discussed that java doesn't' support "\0", then how come this program is not throwing any error? How come it's running fine even though chances are there that someone who wrote the program copied it from C language.

    Please comment on this.

    Thanks

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Array in Java '\0' ?

    No.
    It's not "it doesn't support it".
    It's "it doesn't do anything with it".
    '\0' is a valid character. It just doesn't mean "EOS".
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Bit Array to Byte Array in java
    By Umesh Joshi in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 08:24 AM
  2. Java 2D Array
    By The Dark Dragon in forum New To Java
    Replies: 3
    Last Post: 11-27-2011, 08:00 AM
  3. `java array
    By shane123 in forum New To Java
    Replies: 18
    Last Post: 03-22-2010, 12:42 AM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •