Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Are Strings automatically null terminated?

Reply
Thread Tools

Are Strings automatically null terminated?

 
 
Paul.Lee.1971@gmail.com
Guest
Posts: n/a
 
      07-09-2008
Hi,
I'm very new to Java and would like to ask a newbie question: are
strings automatically null terminated? If I invoke
a class of my own as follows;
MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
"S265W");
are the arguments in parentheses in the constructor automatically
appended implicitly with \0 ?

The reason why I ask is that the first argument in the constructor can
be either a set of points or an area,
and I'm trying to populate a String array of size [2][2], all
initialised to 0. If the first argument is a set of points, I need
only populate
the first two elements of the array. If not, all 4 must be populated.
Ideally, I'd loop through the first argument in
the constructor until \0 is reached, but I don't know if this is added
to the end.

Many thanks

Paul
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-09-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
> I'm very new to Java and would like to ask a newbie question: are
> strings automatically null terminated? If I invoke
> a class of my own as follows;
> MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
> "S265W");
> are the arguments in parentheses in the constructor automatically
> appended implicitly with \0 ?


No, Java strings are not \0 terminated. Instead, the String object
remembers the length.

....
> Ideally, I'd loop through the first argument in
> the constructor until \0 is reached, but I don't know if this is added
> to the end.


Use String's length() method to control the loop, instead of looking for
a termination character. See
http://java.sun.com/j2se/1.5.0/docs/...g.html#length()

Patricia
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      07-09-2008
(E-Mail Removed) wrote:
> Hi,
> I'm very new to Java and would like to ask a newbie question: are
> strings automatically null terminated? If I invoke
> a class of my own as follows;
> MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
> "S265W");
> are the arguments in parentheses in the constructor automatically
> appended implicitly with \0 ?


No, Java strings are more of a pascal string than a C string: they know
their own length. In fact, they are also UTF-16 and can have null
characters within them normally.

> The reason why I ask is that the first argument in the constructor can
> be either a set of points or an area,
> and I'm trying to populate a String array of size [2][2], all
> initialised to 0. If the first argument is a set of points, I need
> only populate
> the first two elements of the array. If not, all 4 must be populated.
> Ideally, I'd loop through the first argument in
> the constructor until \0 is reached, but I don't know if this is added
> to the end.


I don't know enough about what you're doing, but I'd say that passing
around String arguments is a bad idea in favor of proper objects, e.g.:

class Point {
// stuff
}
class Area {
// stuff
}

class MyCoords {
public MyCoords(Point[] points, String plane, String heading) {
// stuff
}
public MyCoords(Area area, String plane, String heading) {
// stuff
}
}

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      07-09-2008
(E-Mail Removed) <(E-Mail Removed)> wrote:
>I'm very new to Java and would like to ask a newbie question: are
>strings automatically null terminated?


Nope. Java String is a different beast than a C string.

>If I invoke
>MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747", "S265W");
>are the arguments in parentheses in the constructor automatically
>appended implicitly with \0 ?


No, and they shouldn't be. You could just as well ask "will the arguments be
automatically appended with the letter Q?". The answer is the same: no, and
if you add one yourself it will be a literal part of the string, not a
terminator.

>The reason why I ask is that the first argument in the constructor can
>be either a set of points or an area,
>and I'm trying to populate a String array of size [2][2], all
>initialised to 0.


They can be set to null, "0" or "" (the empty string)? A numeric 0 is not
a String in Java. The default will be that all four elements in the 2x2
array contain null.

>If the first argument is a set of points, I need only populate
>the first two elements of the array. If not, all 4 must be populated.
>Ideally, I'd loop through the first argument in
>the constructor until \0 is reached, but I don't know if this is added
>to the end.


You are confused. And now so am I! Are you asking if the parameter list is
null-terminated, or if each string is null-terminated? Both answers are "no",
by the way.

And the reason you shouldn't care is that Java arrays and strings know their
length. If the constructor for MyCoords is a varargs (String... args), then
"args" will be an array, and you can check its length. If it's actually
specified (String arg0, String arg1, String arg2), then there _IS_ no arg3 or
any way to find a null "after" arg2. There's no "after" to look at, the args
are assigned to the formal parameters by the VM.

Likewise for String. "S265W".length() tells you there are 5 characters, and
there _IS NO_ '\0' character in addition. "S265W".charAt(5) will throw
IndexOutOfBoundsException, not give you a null.
--
Mark Rafn (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-09-2008
(E-Mail Removed) wrote:
> I'm very new to Java and would like to ask a newbie question: are
> strings automatically null terminated? If I invoke
> a class of my own as follows;
> MyCoords location = new MyCoords("21.5 N 57.0", "Boeing 747",
> "S265W");
> are the arguments in parentheses in the constructor automatically
> appended implicitly with \0 ?
>
> The reason why I ask is that the first argument in the constructor can
> be either a set of points or an area,
> and I'm trying to populate a String array of size [2][2], all
> initialised to 0. If the first argument is a set of points, I need
> only populate
> the first two elements of the array. If not, all 4 must be populated.
> Ideally, I'd loop through the first argument in
> the constructor until \0 is reached, but I don't know if this is added
> to the end.


A Java String is a class that has both data and length. It does not
use C style zero termination.

If you pass "" then you can test with if(v.equals("")) or if
you pass null then you can test with if(v == null).

Or you could pass a String array and have the constructor
loop over the array (in Java you can also get the length of an
array).

Arne
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-09-2008
(E-Mail Removed) wrote:


> and I'm trying to populate a String array of size [2][2], all
> initialised to 0. If the first argument is a set of points, I need
> only populate
> the first two elements of the array. If not, all 4 must be populated.
> Ideally, I'd loop through the first argument in



In addition to what the others said:

I don't think \0 is a legal character representation in Java. Did you
try it? It should give an illegal escape sequence error. (I didn't try
it either, however.) \u0 I think is the null character.

However, the idea of populating a string array as you mention really
weirds me out. Java has the capability to overload methods. I think
you should be using overloaded methods rather than trying to stuff
different numbers of arguments into an array.


So for example, in the first case, you take two points:

void someMethod( Point p1, Point p2 ) ...

and the second case you need "all 4" so:

void someMethod( float x1, float y1, float x2, float y2 ) ...

These two methods can happily co-exist in the same class as either
static or instance methods, the compiler will figure out which one to
call based on the number and type of arguments you supply.

It's hard to tell with out an SSCCE though.

*cough*

http://mindprod.com/jgloss/sscce.html
 
Reply With Quote
 
Paul.Lee.1971@gmail.com
Guest
Posts: n/a
 
      07-10-2008
Thanks for your help everyone, I've realised that I can do everything
I need to using regular expressions and split(...);
the ultimate problem I had was that I might have a argument of 41.44 N
49.57 W 41.46 N 50.14 W
or 41.44 N 49.57 W

all I want in my array is 41.44 49.57 41.46 50.14 and 41.44 49.57
respectively.

Best wishes

Paul

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-10-2008
Mark Space wrote:
> I don't think \0 is a legal character representation in Java. Did you
> try it? It should give an illegal escape sequence error. (I didn't try
> it either, however.) \u0 I think is the null character.


I believe Java insist on \u0000.

Arne
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-10-2008
Arne Vajhøj wrote:
> Mark Space wrote:
>> I don't think \0 is a legal character representation in Java. Did you
>> try it? It should give an illegal escape sequence error. (I didn't
>> try it either, however.) \u0 I think is the null character.

>
> I believe Java insist on \u0000.


Yeah, probably. The OP seems to have fixed his issue, turns out it was
a parsing problem. Curiouser and curiouser.
 
Reply With Quote
 
Paul.Lee.1971@gmail.com
Guest
Posts: n/a
 
      07-10-2008
Thanks for your help everyone, I've realised that I can do everything
I need to using regular expressions and split(...);
the ultimate problem I had was that I might have a argument of 41.44 N
49.57 W 41.46 N 50.14 W
or 41.44 N 49.57 W

all I want in my array is 41.44 49.57 41.46 50.14 and 41.44 49.57
respectively.

Best wishes

Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
automatically initialized to null? bob smith Java 8 08-10-2012 03:53 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
Why compilers do not "p = NULL" automatically after programs do "delete p"? gary C++ 13 11-05-2005 12:27 AM
RAILS: edit turns null strings into empty strings Wybo Dekker Ruby 1 07-23-2005 05:30 PM



Advertisments