Substring

  • Thread starter Dirk Bruere at NeoPax
  • Start date
S

Seamus MacRae

Eric said:
Yes. `myString.length() == 0' gives an equivalent result
(because "" is the only String value with zero length), but is
more roundabout.

More roundabout to write, but perhaps less to execute, as it becomes

count == 0

rather than

(this == otherString) || (count == otherString.count &&
contentsEqual(otherString))

when the logic is expanded.
 
S

Seamus MacRae

Lew said:
Dirk said:
Lew said:
Dirk Bruere at NeoPax wrote:
In String class
public String substring(int beginIndex,
int endIndex)

What happens if beginIndex==endIndex ?

Eric Sosman wrote:
If beginIndex is valid, you get a String whose length is
zero, which you would write as "" in Java source code. If
beginIndex is invalid you get IndexOutOfBoundsException.

This follows directly from the Javadocs for the method,
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#substring(int, int)>

as a moment's investigation reveals.

I asked the Q [sic] because it was *not* obvious to me, and I read it
before posting here.

They have magical-seeming powers in the Q dimension [sic], but being
fictional, they are unlikely to answer you.

It's "Q continuum", actually. :)
 
D

Dirk Bruere at NeoPax

Seamus said:
Lew said:
Dirk said:
Lew wrote:
Dirk Bruere at NeoPax wrote:
In String class
public String substring(int beginIndex,
int endIndex)

What happens if beginIndex==endIndex ?

Eric Sosman wrote:
If beginIndex is valid, you get a String whose length is
zero, which you would write as "" in Java source code. If
beginIndex is invalid you get IndexOutOfBoundsException.

This follows directly from the Javadocs for the method,
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#substring(int, int)>

as a moment's investigation reveals.

I asked the Q [sic] because it was *not* obvious to me, and I read it
before posting here.

They have magical-seeming powers in the Q dimension [sic], but being
fictional, they are unlikely to answer you.

It's "Q continuum", actually. :)

But is it really a continuum? Could it be quantised?

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
M

Mike Schilling

Seamus said:
Personally, I'd have designed the language to have constructors
automatically private. Static factory methods, with actual NAMES,
would be the way to return new instances. String's could share an
instance, and inefficient instance creation would rarely become
locked in by changing it breaking the API. Changing a ctor to a
static factory method breaks your API, whereas changing the
implementation of an existing static factory method may avoid doing
so.

I concur. The "new" syntax taken from C++ is far less compelling in
Java, where

1. There's no need to emphasize the fact that dynamic memory is
allocated, because that's how it *always* works.
2. The overloaded "new" used e.g. to allocate an object at a fixed
address is absent.
3. There's no "delete" to be the obverse of "new"

I'm every bit as happy to say

FileInputStream = FileInputStream.open(fname);

as

FileInputStream = new FileInputStream.(fname);
 
S

Seamus MacRae

Mike said:
I concur. The "new" syntax taken from C++ is far less compelling in
Java, where

1. There's no need to emphasize the fact that dynamic memory is
allocated, because that's how it *always* works.
2. The overloaded "new" used e.g. to allocate an object at a fixed
address is absent.
3. There's no "delete" to be the obverse of "new"

I'm every bit as happy to say

FileInputStream = FileInputStream.open(fname);

as

FileInputStream = new FileInputStream.(fname);

Agreed, and FileInputStream.openStream(fname) would be especially nice
and readable.

However, the "new" keyword would still be needed, for use in those
factory methods.
 
M

Mike Schilling

Seamus said:
Agreed, and FileInputStream.openStream(fname) would be especially
nice
and readable.

However, the "new" keyword would still be needed, for use in those
factory methods.

Naah.

public class FileInputStream
{
private native FileInputStream(String fname);

public static open (String fname)
{
return FileInputStream(fname);
}
}
 
S

Seamus MacRae

Mike said:
Naah.

public class FileInputStream
{
private native FileInputStream(String fname);

public static open (String fname)
{
return FileInputStream(fname);
}
}

There must be some reason why this syntax isn't already legal, likely
involving the parser. That is, why we have the "new" keyword at all.

The special case of omitting it in return statements is certainly feasible.

But factories sometimes will want to assign to a local variable, do
stuff, and then return it in some cases. Builder pattern, for one, with
mutable "under construction" type passed to factory method of immutable
"use this one" type which has to copy fields. (Wrapping the "under
construction" type is horrible -- hanging onto the "mold" and changing
it would allow to mutate the "immutable" thing cast from the mold.)
 
M

Mike Schilling

Seamus said:
There must be some reason why this syntax isn't already legal,
likely
involving the parser. That is, why we have the "new" keyword at all.

AFAICT, it's to look like C++.
 
M

Mike Schilling

Eric said:
Seamus said:
Personally, I'd have designed the language to have constructors
automatically private. Static factory methods, with actual NAMES,
would be the way to return new instances. [...]

Seems like this would demolish class inheritance. How is a
subclass supposed to initialize its own superclass' aspects?
Calling
one of the superclass' factory methods and getting back an
unextended
instance of the superclass doesn't seem helpful ...

You're right, private doesn't work. Protected (in the narrow sense of
only being callable from subclasses, what was called "private
protected " in Java 1.0) would.
 
S

Seamus MacRae

Eric said:
Thesis: A questioner who is still trying to get his head around
the notion of an empty substring should not burden said head with
concerns of this sort. That's not just premature optimization, it's
premature optimization with a vengeance, with aggravating circumstances,
and with oak leaf clusters.

It isn't a premature optimization; it is a clarification of the
ambiguous claim that "myString.length() == 0" is "more roundabout".
 
S

Seamus MacRae

Eric said:
Lew was right. >PLONK<

Excuse me? You said "myString.length() == 0" was "more roundabout". I
pointed out that it was more roundabout to type but probably less
roundabout for the machine to actually execute. You then blew up like
Vesuvius. What gives?
 
G

ghanoz 2480

In String class
public String substring(int beginIndex,
                         int endIndex)

What happens if beginIndex==endIndex ?

Nothing happen, it means will "cut" nothing. You should try it out for
yourself.
 

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

Similar Threads

Official Java Classes 10
Can an Applet beep? 4
ListModel name 10
Accessing static field 21
Sorting a JList 4
File over network timeout 3
Free keyboard applet 5
Change character in string 105

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,195
Latest member
tewan

Latest Threads

Top