Are Strings automatically null terminated?

P

Paul.Lee.1971

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
 
P

Patricia Shanahan

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/api/java/lang/String.html#length()

Patricia
 
J

Joshua Cranmer

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
}
}
 
M

Mark Rafn

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

Arne Vajhøj

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
 
M

Mark Space

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
 
P

Paul.Lee.1971

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
 
A

Arne Vajhøj

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

I believe Java insist on \u0000.

Arne
 
M

Mark Space

Arne said:
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.
 
P

Paul.Lee.1971

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
 
T

Thomas Schodt

Arne said:
I believe Java insist on \u0000.

Unicode translation is done before lexical analysis
and you get a literal ASCII NUL in the String at compile time.

Causes problems if we were talking about LF or CR.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209933

For characters in the control character range
prefer the octal escape sequence "\000" through "\37"
over the unicode encoding "\u0000" through "\u001f".

Well, I do anyway.
 
A

Arne Vajhøj

Thomas said:
Unicode translation is done before lexical analysis
and you get a literal ASCII NUL in the String at compile time.

Causes problems if we were talking about LF or CR.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209933

For characters in the control character range
prefer the octal escape sequence "\000" through "\37"
over the unicode encoding "\u0000" through "\u001f".

Well, I do anyway.

I would use \r and \n for CR and LF.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top