StringBuilder Difficulties

Discussion in 'Java' started by Gene Wirchenko, Jun 29, 2011.

  1. Dear Java'ers:

    I am working with StringBuilder now. I grant that it is faster
    in execution, but it is taking a bunch of my time to get it straight.
    I decided to change my VRString class (call-by-value-result) to
    VRStringB. Complications ensued.

    The amount of ornamentation required in my code was nasty, so I
    did some simplifying.

    How does one assign a String value to a StringBuilder variable?
    For the class below (before I defined .Set()), I needed
    cParsedWord.Value.replace(
    0,cParsedWord.Value.length(),cScan.substring(xStart,xEnd));
    With .Set(), I just need
    cParsedWord.Value.Set(cScan.substring(xStart,xEnd));

    For value equality to a String value, one needs something like
    cParsedWord.Value.toString().equals("some value")
    (because without the .toString(), the comparison will fail) whereas
    with .equals() below, this will do it
    cParsedWord.equals("some value")

    ***** Start of Code *****
    // VRStringB Class
    // StringBuilder Value-Result Parameter Handling
    // Last Modification: 2011-06-28

    class VRStringB
    {
    StringBuilder Value;

    VRStringB()
    {
    this.Value=new StringBuilder("");
    }

    VRStringB
    (
    StringBuilder Init
    )
    {
    this.Value=Init;
    }

    boolean equals
    (
    String theString
    )
    {
    return this.Value.toString().equals(theString);
    }

    void Set
    (
    String theString
    )
    {
    this.Value.replace(0,this.Value.length(),theString);
    }
    }
    ***** End of Code *****

    Am I missing something about StringBuilder, or is it really this
    difficult to play with? It would make a lot more sense to me if
    StringBuilder worked more like String does.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 29, 2011
    #1
    1. Advertising

  2. Gene Wirchenko

    Eric Sosman Guest

    On 6/28/2011 8:54 PM, Gene Wirchenko wrote:
    > Dear Java'ers:
    >
    > I am working with StringBuilder now. I grant that it is faster
    > in execution, but it is taking a bunch of my time to get it straight.
    > I decided to change my VRString class (call-by-value-result) to
    > VRStringB. Complications ensued.
    >
    > The amount of ornamentation required in my code was nasty, so I
    > did some simplifying.
    >
    > How does one assign a String value to a StringBuilder variable?


    One does not "assign" a reference of type T1 to a reference
    of unrelated type T2. One can, however, create a StringBuilder
    whose initial content is the same as that of a given String:

    String str = ...; // non-null
    StringBuilder sbd = new StringBuilder(str);

    These are "the same" in the sense that str.length() == sbc.length()
    and str.charAt(k) == sbd.charAt(k) for all 0 <= k < str.length().
    Also, str.equals(sbd.toString()) returns true.

    > [...]
    > Am I missing something about StringBuilder, or is it really this
    > difficult to play with?


    You've been advised to read some Java tutorials or textbooks,
    and (it seems) have chosen not to follow that advice. Under the
    circumstances, then, I'd have to say StringBuilder is "really this
    difficult to play with."

    Perhaps you're "too pressed for time" to learn Java before writing
    it. Okay, yeah, that's reality, shit happens. But if so, you've been
    "pressed for time" for some weeks now, and (I'll wager) have already
    wasted more time chasing blind alleys than you would have spent learning
    the rudiments of the language. (A potential infinite regress looms.)

    > It would make a lot more sense to me if
    > StringBuilder worked more like String does.


    StringBuilder is only of interest because it is mutable, while
    String is immutable. From this fact flow many of the necessary
    differences that give you so much trouble, but that others take in
    stride.

    Summary: Fer Crissakes, Gene, read the damn' book!

    --
    Eric Sosman
    d
    Eric Sosman, Jun 29, 2011
    #2
    1. Advertising

  3. Gene Wirchenko

    Lew Guest

    Eric Sosman wrote:
    > Summary: Fer Crissakes, Gene, read the damn' book!


    And don't go plonking everyone who gives you good advice, either, Gene. Eric,
    like markspace before him, is giving you good advice. Follow it instead of
    copping an attitude like you did before.

    Straight, unvarnished, blunt truth can feel bad at first, but it helps you.
    It really does.

    Also, refusal to learn harms you.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 29, 2011
    #3
  4. On Tue, 28 Jun 2011 21:29:00 -0400, Eric Sosman
    <> wrote:

    >On 6/28/2011 8:54 PM, Gene Wirchenko wrote:
    >> Dear Java'ers:
    >>
    >> I am working with StringBuilder now. I grant that it is faster
    >> in execution, but it is taking a bunch of my time to get it straight.
    >> I decided to change my VRString class (call-by-value-result) to
    >> VRStringB. Complications ensued.
    >>
    >> The amount of ornamentation required in my code was nasty, so I
    >> did some simplifying.
    >>
    >> How does one assign a String value to a StringBuilder variable?

    ^^^^^
    > One does not "assign" a reference of type T1 to a reference

    ^^^^^^^^^
    I wrote "value".

    >of unrelated type T2. One can, however, create a StringBuilder
    >whose initial content is the same as that of a given String:
    >
    > String str = ...; // non-null
    > StringBuilder sbd = new StringBuilder(str);


    I have found that, but I was wondering about how to do it with
    the same StringBuilder object. I thought that part of the advantage
    of using StringBuilder was that the amount of object creation got cut
    down.

    >These are "the same" in the sense that str.length() == sbc.length()
    >and str.charAt(k) == sbd.charAt(k) for all 0 <= k < str.length().
    >Also, str.equals(sbd.toString()) returns true.


    IOW, a String value assigned to a StringBuilder variable.

    >> [...]
    >> Am I missing something about StringBuilder, or is it really this
    >> difficult to play with?

    >
    > You've been advised to read some Java tutorials or textbooks,
    >and (it seems) have chosen not to follow that advice. Under the
    >circumstances, then, I'd have to say StringBuilder is "really this
    >difficult to play with."


    Which I have. I have been referring to the docs for
    StringBuilder, but the docs are rather incomplete, and I have to
    guess. This takestime.

    > Perhaps you're "too pressed for time" to learn Java before writing
    >it. Okay, yeah, that's reality, shit happens. But if so, you've been
    >"pressed for time" for some weeks now, and (I'll wager) have already


    A few and part-time.

    >wasted more time chasing blind alleys than you would have spent learning
    >the rudiments of the language. (A potential infinite regress looms.)


    I know the rudiments. I am having trouble with the next level.

    I have been asking here to try to knock out some of those blind
    alleys. I can get something to work, but I would prefer to have
    something work well.

    >> It would make a lot more sense to me if
    >> StringBuilder worked more like String does.

    >
    > StringBuilder is only of interest because it is mutable, while
    >String is immutable. From this fact flow many of the necessary
    >differences that give you so much trouble, but that others take in
    >stride.


    Why is the syntax so different? It does not need to be, but it
    is. In order to change from String to StringBuilder, I had to modify
    a bunch of code apart from changing variable types.

    > Summary: Fer Crissakes, Gene, read the damn' book!


    I have been RTFM. The docs often say what, but without an
    example and usually not why. I am paddling the best I can. Please do
    not cannonball next to me.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 29, 2011
    #4
  5. On Wed, 29 Jun 2011 00:48:40 -0400, Lew <> wrote:

    >Eric Sosman wrote:
    >> Summary: Fer Crissakes, Gene, read the damn' book!

    >
    >And don't go plonking everyone who gives you good advice, either, Gene. Eric,


    I do not. I plonked one person who called my sanity into
    question. As I like to say, disagreeing one thing, but being
    disagreeable is quite another. Snarking on sanity is the latter.

    >like markspace before him, is giving you good advice. Follow it instead of
    >copping an attitude like you did before.
    >
    >Straight, unvarnished, blunt truth can feel bad at first, but it helps you.
    >It really does.


    The attitude of some posters is that they are Keepers of the
    Truth. I suggest that there are different realities. I have
    programmed in quite a few programming languages. I see no need to
    jettison what I have learned, because some practice is different with
    Java. If it is better, I will consider using it. If it does not meet
    my needs, I see no need to use it regardless of what Java practice is.

    >Also, refusal to learn harms you.


    I do not refuse to learn, but I know what I want to accomplish
    with my program. A number of the suggestions have been plain
    irrelevant. Some of them have been quite useful, but those subthreads
    tend to die, because there is no need for further discussion. Thus,
    discussion tends to continue only on points of contention. They are
    not the only points. There is more agreement than disagreement, but
    you would not know that the way some posters keep coming at me.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 29, 2011
    #5
  6. Gene Wirchenko

    Guest

    In article <>,
    Gene Wirchenko <> wrote:
    > Dear Java'ers:
    >
    > I am working with StringBuilder now. I grant that it is faster
    > in execution, but it is taking a bunch of my time to get it straight.
    > I decided to change my VRString class (call-by-value-result) to
    > VRStringB. Complications ensued.
    >
    > The amount of ornamentation required in my code was nasty, so I
    > did some simplifying.
    >
    > How does one assign a String value to a StringBuilder variable?
    > For the class below (before I defined .Set()), I needed
    > cParsedWord.Value.replace(
    > 0,cParsedWord.Value.length(),cScan.substring(xStart,xEnd));
    > With .Set(), I just need
    > cParsedWord.Value.Set(cScan.substring(xStart,xEnd));
    >
    > For value equality to a String value, one needs something like
    > cParsedWord.Value.toString().equals("some value")
    > (because without the .toString(), the comparison will fail) whereas
    > with .equals() below, this will do it
    > cParsedWord.equals("some value")
    >
    > ***** Start of Code *****
    > // VRStringB Class
    > // StringBuilder Value-Result Parameter Handling
    > // Last Modification: 2011-06-28
    >
    > class VRStringB
    > {
    > StringBuilder Value;
    >
    > VRStringB()
    > {
    > this.Value=new StringBuilder("");
    > }
    >
    > VRStringB
    > (
    > StringBuilder Init
    > )
    > {
    > this.Value=Init;
    > }
    >
    > boolean equals
    > (
    > String theString
    > )
    > {
    > return this.Value.toString().equals(theString);
    > }
    >
    > void Set
    > (
    > String theString
    > )
    > {
    > this.Value.replace(0,this.Value.length(),theString);
    > }
    > }
    > ***** End of Code *****
    >
    > Am I missing something about StringBuilder, or is it really this
    > difficult to play with? It would make a lot more sense to me if
    > StringBuilder worked more like String does.



    Well ....

    If all you need is something that's like a String but whose value
    can change, it seems to me [*] that you might be better off just
    writing a simple wrapper class for String -- I'm thinking a class
    with one variable of type String, which could even be public if
    you don't want to fool with writing getter/setter methods.

    [*] The experts may disagree. I'm a journey(wo)man at best
    with Java.

    Where StringBuilder is useful is in, well, building strings;
    a typical use case is a situation in which you want to build
    up a string piece by piece. You *could* write something like

    String a = "first";
    a += " second";
    a += " etc";

    and my *guess* is that this is not horribly inefficient if the
    number of concatenation operations is small. (The conventional
    wisdom, as I understand it, is that the Java runtime is pretty
    good at managing short-lived objects, so creating new objects is
    not invariably something to avoid, though as with anything else
    one shouldn't get carried away, maybe. Again the experts may
    disagree.)

    But if there are a lot of concatenation operations it's said to be
    more efficient to use a StringBuilder, e.g.:

    StringBuilder sb = new StringBuilder();
    sb.append("first");
    sb.append(" second");
    sb.append(" etc");
    String a = sb.toString();

    The above is what I mostly use StringBuilder for; there are
    undoubtedly other things one can do with it as well, some of
    which may be useful to you (I haven't followed carefully all the
    threads you've started).

    (I'm not optimistic that this commentary will be helpful, or
    even that it will be read [*], given that you didn't reply to
    my posts in the threads about passing method names to methods,
    but I guess I'll try again .... <shrug> )

    [*] That's not actually meant as snark; not long ago I changed
    the e-mail address I use to post, to a GMail one, and I'm under
    the impression that some Usenet participants routinely filter out
    anything from a GMail address, so I worry just a bit ....

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Jun 29, 2011
    #6
  7. On Wed, 29 Jun 2011 14:38:04 -0700, Patricia Shanahan <>
    wrote:

    >On 6/28/2011 5:54 PM, Gene Wirchenko wrote:
    >...
    >> Am I missing something about StringBuilder, or is it really this
    >> difficult to play with? It would make a lot more sense to me if
    >> StringBuilder worked more like String does.

    >...
    >
    >The main thing you seem to be missing about StringBuilder is that it is
    >not a string, it is a tool for building strings.


    Well, once I have built a string, I want to use it.

    >Here is some sample code demonstrating comparing a String to the current
    >contents of a StringBuilder, and replacing the entire contents with a
    >different String.
    >
    >public class StringBuilderExample {
    > public static void main(String[] args) {
    > StringBuilder sb = new StringBuilder();
    > sb.append("aaa");
    > System.out.println(sb);
    > System.out.println("aaa".equals(sb.toString()));
    > sb.replace(0, Integer.MAX_VALUE, "bbbbbbb");
    > System.out.println(sb);
    > }
    >}


    I find the .replace() line rather ornamental. I have even more
    complex parameters so I came up with my .Set() method. I am surprised
    that there is no such functionality in StringBuilder.

    I understand your example. Either I have learned, you write good
    examples, or both.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 29, 2011
    #7
  8. Gene Wirchenko

    Roedy Green Guest

    On Tue, 28 Jun 2011 17:54:38 -0700, Gene Wirchenko <>
    wrote, quoted or indirectly quoted someone who said :

    >
    > How does one assign a String value to a StringBuilder variable?


    see http://mindprod.com/jgloss/stringbuilder.html

    There is a ton of source code on the net. Googling it looking for
    various classes and methods can help you find how other programmers
    use them which can help you make sense of the JavaDoc.

    I use FastCat now for 95% of my string building work. It is aims to
    use memory precisely to reduce the amount of GC. I only use
    StringBuilder for character-by-character building.

    see http://mindprod.com/products1.html#FASTCAT

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One of the curses of the computer age is manufacturers now design
    home appliances to die on the very day the warranty expires.
    It is deliberate waste in the service of mindless profit.
    Roedy Green, Jun 30, 2011
    #8
  9. On 29 Jun 2011 20:17:37 GMT,
    <> wrote:

    >In article <>,
    >Gene Wirchenko <> wrote:


    [snip]

    >> Am I missing something about StringBuilder, or is it really this
    >> difficult to play with? It would make a lot more sense to me if
    >> StringBuilder worked more like String does.


    [snip]

    >up a string piece by piece. You *could* write something like
    >
    >String a = "first";
    >a += " second";
    >a += " etc";
    >
    >and my *guess* is that this is not horribly inefficient if the
    >number of concatenation operations is small. (The conventional


    IOW, who cares if it is only a bit of inefficiency? Agreed.

    >wisdom, as I understand it, is that the Java runtime is pretty
    >good at managing short-lived objects, so creating new objects is
    >not invariably something to avoid, though as with anything else
    >one shouldn't get carried away, maybe. Again the experts may
    >disagree.)
    >
    >But if there are a lot of concatenation operations it's said to be
    >more efficient to use a StringBuilder, e.g.:
    >
    >StringBuilder sb = new StringBuilder();
    >sb.append("first");
    >sb.append(" second");
    >sb.append(" etc");
    >String a = sb.toString();


    It is. My test code appends one character at a time. Switching
    from String to StringBuilder cut the execution time by about 40%.

    >The above is what I mostly use StringBuilder for; there are
    >undoubtedly other things one can do with it as well, some of
    >which may be useful to you (I haven't followed carefully all the
    >threads you've started).


    I have managed to get to the point where I have found out what
    you have posted here, and I agree with it, too.

    >(I'm not optimistic that this commentary will be helpful, or
    >even that it will be read [*], given that you didn't reply to
    >my posts in the threads about passing method names to methods,
    >but I guess I'll try again .... <shrug> )
    >
    >[*] That's not actually meant as snark; not long ago I changed
    >the e-mail address I use to post, to a GMail one, and I'm under
    >the impression that some Usenet participants routinely filter out
    >anything from a GMail address, so I worry just a bit ....


    Not me. I killfile only the deserving (meaning those undeserving
    of my time).

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 30, 2011
    #9
  10. On Wed, 29 Jun 2011 15:52:46 -0700, Patricia Shanahan <>
    wrote:

    [snip]

    >It is relatively rare to decide, part way through a build, to throw away
    >the work so far and begin again with entirely different contents.


    I am going to be doing it in a loop. why create a new object
    each iteration?

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 30, 2011
    #10
  11. Gene Wirchenko

    Eric Sosman Guest

    On 6/29/2011 3:11 PM, Gene Wirchenko wrote:
    > On Tue, 28 Jun 2011 21:29:00 -0400, Eric Sosman
    > <> wrote:
    >
    >> On 6/28/2011 8:54 PM, Gene Wirchenko wrote:
    >>> Dear Java'ers:
    >>>
    >>> I am working with StringBuilder now. I grant that it is faster
    >>> in execution, but it is taking a bunch of my time to get it straight.
    >>> I decided to change my VRString class (call-by-value-result) to
    >>> VRStringB. Complications ensued.
    >>>
    >>> The amount of ornamentation required in my code was nasty, so I
    >>> did some simplifying.
    >>>
    >>> How does one assign a String value to a StringBuilder variable?

    > ^^^^^
    >> One does not "assign" a reference of type T1 to a reference

    > ^^^^^^^^^
    > I wrote "value".


    You also wrote "assign." The only values Java can "assign"
    are primitives and references -- in particular, Java cannot "assign"
    the sequence of characters that make up the content of a String or
    a StringBuilder.

    >> String str = ...; // non-null
    >> StringBuilder sbd = new StringBuilder(str);

    >
    > I have found that, but I was wondering about how to do it with
    > the same StringBuilder object. I thought that part of the advantage
    > of using StringBuilder was that the amount of object creation got cut
    > down.


    Not sure what the "it" you mention is. The efficiency "advantage,"
    to the extent that there is one, is illustrated by

    String result = "";
    for (String s : bigBunchOfStrings)
    result += s;

    vs.

    StringBuilder buff = new StringBuilder();
    for (String s : bigBunchOfStrings)
    buff.append(s);
    String result = buff.toString();

    >> These are "the same" in the sense that str.length() == sbc.length()
    >> and str.charAt(k) == sbd.charAt(k) for all 0<= k< str.length().
    >> Also, str.equals(sbd.toString()) returns true.

    >
    > IOW, a String value assigned to a StringBuilder variable.


    See above; this is not "assignment."

    >> You've been advised to read some Java tutorials or textbooks,
    >> and (it seems) have chosen not to follow that advice. Under the
    >> circumstances, then, I'd have to say StringBuilder is "really this
    >> difficult to play with."

    >
    > Which I have. I have been referring to the docs for
    > StringBuilder, but the docs are rather incomplete, and I have to
    > guess. This takestime.


    Elsethread you mention that you've programmed in "quite a few
    programming languages." Did you study them as diligently as you're
    studying Java, or are you like the Real Programmer who "can write
    FORTRAN in any language?"

    Okay, if you've got a distaste for Java and are reluctant to
    learn it, that's the way it goes. I've resisted C++ for years and
    years on equally flimsy grounds. But if you're just trying to "get
    by" and don't intend to learn, then at the very least stop whining!

    > I know the rudiments. I am having trouble with the next level.


    IMHO you do not yet know the rudiments.

    --
    Eric Sosman
    d
    Eric Sosman, Jun 30, 2011
    #11
  12. Gene Wirchenko

    Guest

    In article <>,
    Gene Wirchenko <> wrote:
    > <> wrote:
    >
    > >In article <>,
    > >Gene Wirchenko <> wrote:


    [ snip ]

    > >You *could* write something like
    > >
    > >String a = "first";
    > >a += " second";
    > >a += " etc";
    > >
    > >and my *guess* is that this is not horribly inefficient if the
    > >number of concatenation operations is small. (The conventional

    >
    > IOW, who cares if it is only a bit of inefficiency? Agreed.
    >
    > >wisdom, as I understand it, is that the Java runtime is pretty
    > >good at managing short-lived objects, so creating new objects is
    > >not invariably something to avoid, though as with anything else
    > >one shouldn't get carried away, maybe. Again the experts may
    > >disagree.)
    > >
    > >But if there are a lot of concatenation operations it's said to be
    > >more efficient to use a StringBuilder, e.g.:
    > >
    > >StringBuilder sb = new StringBuilder();
    > >sb.append("first");
    > >sb.append(" second");
    > >sb.append(" etc");
    > >String a = sb.toString();

    >
    > It is. My test code appends one character at a time. Switching
    > >from String to StringBuilder cut the execution time by about 40%.


    So okay, you probably *do* want to use StringBuilder, and having
    that confirmed by experiment is good.

    [ snip ]

    > >(I'm not optimistic that this commentary will be helpful, or
    > >even that it will be read [*], given that you didn't reply to
    > >my posts in the threads about passing method names to methods,
    > >but I guess I'll try again .... <shrug> )
    > >
    > >[*] That's not actually meant as snark; not long ago I changed
    > >the e-mail address I use to post, to a GMail one, and I'm under
    > >the impression that some Usenet participants routinely filter out
    > >anything from a GMail address, so I worry just a bit ....

    >
    > Not me. I killfile only the deserving (meaning those undeserving
    > of my time).


    More useful information; thanks for the reply.

    A general comment: I'm inclined to agree with the people who are
    saying that in general it seems like you're trying to write [name
    of your favorite language] programs in Java, and in the long term
    that seems less optimal than trying to grok the Java mindset.
    I think part of it may be struggling with the object-oriented
    paradigm, but part of it may just be coming to terms with the fact
    that Java is, as I think Patricia Shanahan said not long ago
    (possibly in another thread), that Java is just plain verbose.

    But I have some sympathy with the desire just to get something
    running: I spent a number of hours a while back trying to teach
    myself some Scheme and in the process trying make it conform to
    my strongly-typed-languages-trained mindset, and I'd probably
    have done better to get a good introductory book and try to grok
    the no-types(?) mindset. (Maybe I'll try again at some point.)

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Jun 30, 2011
    #12
  13. On 30 Jun 2011 20:30:00 GMT,
    <> wrote:

    [snip]

    >A general comment: I'm inclined to agree with the people who are
    >saying that in general it seems like you're trying to write [name
    >of your favorite language] programs in Java, and in the long term
    >that seems less optimal than trying to grok the Java mindset.


    My mindset is that I want to get my work done. I do not care
    about the Java mindset except as it helps me get my work done.

    >I think part of it may be struggling with the object-oriented
    >paradigm, but part of it may just be coming to terms with the fact


    No, I am experienced with OOP.

    >that Java is, as I think Patricia Shanahan said not long ago
    >(possibly in another thread), that Java is just plain verbose.


    Well, I posted about the verbosity earlier and got flak over it.

    >But I have some sympathy with the desire just to get something
    >running: I spent a number of hours a while back trying to teach


    And without having to buy into a language religion.

    >myself some Scheme and in the process trying make it conform to
    >my strongly-typed-languages-trained mindset, and I'd probably
    >have done better to get a good introductory book and try to grok
    >the no-types(?) mindset. (Maybe I'll try again at some point.)


    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jun 30, 2011
    #13
  14. Gene Wirchenko

    Eric Sosman Guest

    On 6/30/2011 5:17 PM, Patricia Shanahan wrote:
    > On 6/30/2011 1:30 PM, wrote:
    > ...
    >> But I have some sympathy with the desire just to get something
    >> running: I spent a number of hours a while back trying to teach
    >> myself some Scheme and in the process trying make it conform to
    >> my strongly-typed-languages-trained mindset, and I'd probably
    >> have done better to get a good introductory book and try to grok
    >> the no-types(?) mindset. (Maybe I'll try again at some point.)
    >>

    >
    > I've tried several approaches to learning programming languages, and the
    > one that works best for me is to get an introductory book, and work
    > through it, reading each chapter and doing the exercises.


    To this I'd add (and perhaps this is reflective only of my
    own modes of learning): Get hold of some samples of good code in
    the language (or "believed to be good," since the learner is in
    a poor position to judge), and read the code. With the tutorials
    and reference works at one's elbow, of course. "Why didn't he need
    to grab a lock here?" "Oh, *that's* how you handle file-not-found!"
    And so on, and so on.

    I still recall learning SNOBOL years and years ago by studying
    the SNOBOL source of the SNOBOL compiler while frantically flipping
    pages in the SNOBOL book. My grasp of the language SNOBOLed.

    --
    Eric Sosman
    d
    Eric Sosman, Jul 1, 2011
    #14
  15. In article <iuj8bk$v5i$>,
    Eric Sosman <> wrote:

    > I still recall learning SNOBOL years and years ago by studying
    > the SNOBOL source of the SNOBOL compiler while frantically flipping
    > pages in the SNOBOL book. My grasp of the language SNOBOLed.


    You might enjoy looking at the SPITBOL operations that are part of the
    GNAT Ada Library in the package, GNAT.Spitbol:

    <http://gcc.gnu.org/onlinedocs/gnat_rm/The-GNAT-Library.html#The-GNAT-Library>

    Much of the documentation is in the corresponding specification (.ads)
    files:

    <http://gcc.gnu.org/viewcvs/trunk/gcc/ada/>

    GNAT.Spitbol.Patterns is an example:

    <http://gcc.gnu.org/viewcvs/trunk/gcc/ada/g-spipat.ads?revision=139296&view=markup>

    The GPL version is available here:

    <http://libre.adacore.com/libre/>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Jul 1, 2011
    #15
  16. Gene Wirchenko

    Guest

    In article <>,
    Gene Wirchenko <> wrote:
    > On 30 Jun 2011 20:30:00 GMT,
    > <> wrote:
    >
    > [snip]
    >
    > >A general comment: I'm inclined to agree with the people who are
    > >saying that in general it seems like you're trying to write [name
    > >of your favorite language] programs in Java, and in the long term
    > >that seems less optimal than trying to grok the Java mindset.

    >
    > My mindset is that I want to get my work done. I do not care
    > about the Java mindset except as it helps me get my work done.


    Yes, and if you were going to do a lot of programming in Java it
    would seem to make sense to adapt to the local customs, so to speak.
    Not to do so seems to me like fighting with your tools, which, well,
    I do it too sometimes, but it does get in the way of getting stuff
    done.

    > >I think part of it may be struggling with the object-oriented
    > >paradigm, but part of it may just be coming to terms with the fact

    >
    > No, I am experienced with OOP.


    Huh. Well, with all due respect ....

    I'd have said otherwise given that all of the variables and methods
    in your TimingTesting program (the version I tried revising) seem
    to be static (except the local variables). I'm also puzzled by why
    that program duplicates so much code, when you could have factored
    out the parts that are different using objects-as-code-wrappers.
    But maybe the O-O languages you've used before don't make you do
    that, and adapting to that particular Java idiom seemed not worth
    the trouble.

    > >that Java is, as I think Patricia Shanahan said not long ago
    > >(possibly in another thread), that Java is just plain verbose.

    >
    > Well, I posted about the verbosity earlier and got flak over it.
    >
    > >But I have some sympathy with the desire just to get something
    > >running: I spent a number of hours a while back trying to teach

    >
    > And without having to buy into a language religion.


    Hm. I wouldn't say that adapting to local customs constitutes
    buying into a language religion. YMMV, I suppose.

    > >myself some Scheme and in the process trying make it conform to
    > >my strongly-typed-languages-trained mindset, and I'd probably
    > >have done better to get a good introductory book and try to grok
    > >the no-types(?) mindset. (Maybe I'll try again at some point.)


    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Jul 1, 2011
    #16
  17. On 1 Jul 2011 20:47:47 GMT,
    <> wrote:

    >In article <>,
    >Gene Wirchenko <> wrote:
    >> On 30 Jun 2011 20:30:00 GMT,
    >> <> wrote:
    >>
    >> [snip]
    >>
    >> >A general comment: I'm inclined to agree with the people who are
    >> >saying that in general it seems like you're trying to write [name
    >> >of your favorite language] programs in Java, and in the long term
    >> >that seems less optimal than trying to grok the Java mindset.

    >>
    >> My mindset is that I want to get my work done. I do not care
    >> about the Java mindset except as it helps me get my work done.

    >
    >Yes, and if you were going to do a lot of programming in Java it
    >would seem to make sense to adapt to the local customs, so to speak.
    >Not to do so seems to me like fighting with your tools, which, well,
    >I do it too sometimes, but it does get in the way of getting stuff
    >done.


    My tools include manyyears of experience programming. I do not
    think that Java is such a precious snowflake -- the same is true of
    any language -- that I should have to throw all that experience away
    in order to use the language.

    >> >I think part of it may be struggling with the object-oriented
    >> >paradigm, but part of it may just be coming to terms with the fact

    >>
    >> No, I am experienced with OOP.

    >
    >Huh. Well, with all due respect ....
    >
    >I'd have said otherwise given that all of the variables and methods
    >in your TimingTesting program (the version I tried revising) seem
    >to be static (except the local variables). I'm also puzzled by why
    >that program duplicates so much code, when you could have factored
    >out the parts that are different using objects-as-code-wrappers.
    >But maybe the O-O languages you've used before don't make you do
    >that, and adapting to that particular Java idiom seemed not worth
    >the trouble.


    Oh, I asked about that. One apparently can not pass a function
    pointer parameter as in C. The ways that were posted involved lookup
    every time AFIACS and I judged that it might swamp what I was
    measuring (checking if a character were in a set). So, to my chagrin,
    I had to go with cut-and-paste.

    >> >that Java is, as I think Patricia Shanahan said not long ago
    >> >(possibly in another thread), that Java is just plain verbose.

    >>
    >> Well, I posted about the verbosity earlier and got flak over it.
    >>
    >> >But I have some sympathy with the desire just to get something
    >> >running: I spent a number of hours a while back trying to teach

    >>
    >> And without having to buy into a language religion.

    >
    >Hm. I wouldn't say that adapting to local customs constitutes
    >buying into a language religion. YMMV, I suppose.


    Some of the posters have been quite vociferous about it.

    >> >myself some Scheme and in the process trying make it conform to
    >> >my strongly-typed-languages-trained mindset, and I'd probably
    >> >have done better to get a good introductory book and try to grok
    >> >the no-types(?) mindset. (Maybe I'll try again at some point.)


    I am pretty much past the intro stage and into the pain stage
    where there is not so much help.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Jul 2, 2011
    #17
  18. On 07/02/2011 02:29 AM, Gene Wirchenko wrote:
    > On 1 Jul 2011 20:47:47 GMT,
    > <> wrote:
    >
    >> In article<>,
    >> Gene Wirchenko<> wrote:
    >>> On 30 Jun 2011 20:30:00 GMT,
    >>> <> wrote:
    >>>
    >>> [snip]
    >>>
    >>>> A general comment: I'm inclined to agree with the people who are
    >>>> saying that in general it seems like you're trying to write [name
    >>>> of your favorite language] programs in Java, and in the long term
    >>>> that seems less optimal than trying to grok the Java mindset.
    >>>
    >>> My mindset is that I want to get my work done. I do not care
    >>> about the Java mindset except as it helps me get my work done.

    >>
    >> Yes, and if you were going to do a lot of programming in Java it
    >> would seem to make sense to adapt to the local customs, so to speak.
    >> Not to do so seems to me like fighting with your tools, which, well,
    >> I do it too sometimes, but it does get in the way of getting stuff
    >> done.

    >
    > My tools include manyyears of experience programming. I do not
    > think that Java is such a precious snowflake -- the same is true of
    > any language -- that I should have to throw all that experience away
    > in order to use the language.


    As far as I can see nobody asked you to do that. If adjusting to a new
    language's mindset requires you to throw away everything you've learned
    so far then you probably better stick with the previous experience and
    tools. That will be much more efficient and beneficial.

    If, on the other hand, you want to use a new language then you typically
    get best results (or results at all) if you adjust to the environment
    you find. You may have noticed that your issues with StringBuilder seem
    to be quite unique - others posting here do not seem to have those
    issues. In my experience this is usually an indication that I am doing
    something wrong or haven't properly understood the new environment yet.

    >>>> I think part of it may be struggling with the object-oriented
    >>>> paradigm, but part of it may just be coming to terms with the fact
    >>>
    >>> No, I am experienced with OOP.

    >>
    >> Huh. Well, with all due respect ....
    >>
    >> I'd have said otherwise given that all of the variables and methods
    >> in your TimingTesting program (the version I tried revising) seem
    >> to be static (except the local variables). I'm also puzzled by why
    >> that program duplicates so much code, when you could have factored
    >> out the parts that are different using objects-as-code-wrappers.
    >> But maybe the O-O languages you've used before don't make you do
    >> that, and adapting to that particular Java idiom seemed not worth
    >> the trouble.

    >
    > Oh, I asked about that. One apparently can not pass a function
    > pointer parameter as in C. The ways that were posted involved lookup
    > every time AFIACS and I judged that it might swamp what I was
    > measuring (checking if a character were in a set). So, to my chagrin,
    > I had to go with cut-and-paste.


    The usual solution in Java is to factor out an API into an interface and
    have several implementations of that interface. See Callable for
    example - this basically encapsulates a "function" with no arguments and
    a single return value:

    http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Callable.html

    In your TimingTesting program you could define a private interface like this

    private interface Search {
    boolean search(char c);
    }

    Then you create inner classes

    private final class SequentialSearch implements Search {
    @Override
    boolean search(char c) {
    for ( int i = 0; i < chars.length(); ++i ) {
    if (chars.charAt(i) == c) {
    return true;
    }
    }

    return false;
    }
    }

    public void parseSequentialSearch() {
    parse(new SequentialSearch());
    }

    private void parse(Search s) {
    int xScan=0;
    boolean fBuildingIdent=false;
    boolean fInIdentChars;
    String cIdent=""; // fussy init
    while (xScan<cParseString.length())
    {
    char CurrChar=cParseString.charAt(xScan);
    fInIdentChars=s.search(CurrChar);
    ....
    }

    etc.

    This would be the _minimal_ refactoring to get what you want but you
    also need to switch from having all the state static in TimingTesting to
    instance. Chances are that a more thorough refactoring yields a
    significantly better (in terms of OO and modularity) solution.

    Btw, if you want to create a parser in Java then I recommend looking
    into ANTLR. There is even a graphical UI (ANTLRWorks) which helps get
    results quickly and even has grammar debugging. Cool stuff!

    http://www.antlr.org/works/index.html

    Cheers

    robert
    Robert Klemme, Jul 2, 2011
    #18
  19. Gene Wirchenko

    Guest

    In article <>,
    Gene Wirchenko <> wrote:
    > On 1 Jul 2011 20:47:47 GMT,
    > <> wrote:
    >
    > >In article <>,
    > >Gene Wirchenko <> wrote:
    > >> On 30 Jun 2011 20:30:00 GMT,
    > >> <> wrote:
    > >>
    > >> [snip]
    > >>
    > >> >A general comment: I'm inclined to agree with the people who are
    > >> >saying that in general it seems like you're trying to write [name
    > >> >of your favorite language] programs in Java, and in the long term
    > >> >that seems less optimal than trying to grok the Java mindset.
    > >>
    > >> My mindset is that I want to get my work done. I do not care
    > >> about the Java mindset except as it helps me get my work done.

    > >
    > >Yes, and if you were going to do a lot of programming in Java it
    > >would seem to make sense to adapt to the local customs, so to speak.
    > >Not to do so seems to me like fighting with your tools, which, well,
    > >I do it too sometimes, but it does get in the way of getting stuff
    > >done.

    >
    > My tools include manyyears of experience programming. I do not
    > think that Java is such a precious snowflake -- the same is true of
    > any language -- that I should have to throw all that experience away
    > in order to use the language.


    As another poster has responded, I'm not convinced that's what
    you're being asked to do (though you obviously feel otherwise).

    It seems to me that one of the things one learns from doing a lot
    of coding in a lot of languages is recognizing when something is
    the same as what you've done before, just with different syntax,
    and when it's different.

    <shrug>

    > >> >I think part of it may be struggling with the object-oriented
    > >> >paradigm, but part of it may just be coming to terms with the fact
    > >>
    > >> No, I am experienced with OOP.

    > >
    > >Huh. Well, with all due respect ....
    > >
    > >I'd have said otherwise given that all of the variables and methods
    > >in your TimingTesting program (the version I tried revising) seem
    > >to be static (except the local variables). I'm also puzzled by why
    > >that program duplicates so much code, when you could have factored
    > >out the parts that are different using objects-as-code-wrappers.
    > >But maybe the O-O languages you've used before don't make you do
    > >that, and adapting to that particular Java idiom seemed not worth
    > >the trouble.

    >
    > Oh, I asked about that. One apparently can not pass a function
    > pointer parameter as in C. The ways that were posted involved lookup
    > every time AFIACS and I judged that it might swamp what I was
    > measuring (checking if a character were in a set). So, to my chagrin,
    > I had to go with cut-and-paste.


    Without experimenting to find out, of course ....

    It seems to me that virtual method invocations are so common in
    Java that they would be well-optimized. But if you want to claim
    that the code you eventually hope to produce won't have one, well,
    yeah, that's true, so maybe it matters. Then again, wouldn't a
    similar argument apply to C with function pointers? Maybe not.
    I don't seem to be able to think this through as carefully as
    I'd like.

    Anyway, I was curious, so I ran your code and my revision [1], and
    the results were -- surprising [2]. I noticed, by the way, that
    all three of your parse methods make a call to SequentialSearch,
    assigning the result to a variable that apparently isn't used.
    Thinking that *might* be a mistake, I also tried your code and my
    revision with that possibly-extra call removed.

    [1] Message-ID: <>

    [2] I tried this on several different systems, all Fedora Linux
    running Java 1.6.0_21 but different hardware and different releases
    of Fedora. I was going to include results here, but in trying
    the experiment on additional systems I'm less and less convinced
    the results would mean anything without my putting more effort
    into it than I'm up for. (Usually when I'm timing something
    I try it twice, and if results are close enough I don't bother
    with additional trials. In this case results of two trials were
    just different enough for me to think I'd need to do more to get
    meaningful results.) Briefly, though .... :

    Your code was fairly (but not 100%!) consistent in showing
    treeset-based search to be fastest, followed by binary search and
    then sequential search, though sometimes the difference between
    sequential and binary was small. My code was -- well, this is where
    it's surprising. On most of the systems where I tried it, treeset
    search was fastest, but sequential search was faster than binary
    search; on one system, however, the order was as for your code.
    Your code was pretty consistently faster than mine, though usually
    not by a lot (less than 1%).

    > >> >that Java is, as I think Patricia Shanahan said not long ago
    > >> >(possibly in another thread), that Java is just plain verbose.
    > >>
    > >> Well, I posted about the verbosity earlier and got flak over it.
    > >>
    > >> >But I have some sympathy with the desire just to get something
    > >> >running: I spent a number of hours a while back trying to teach
    > >>
    > >> And without having to buy into a language religion.

    > >
    > >Hm. I wouldn't say that adapting to local customs constitutes
    > >buying into a language religion. YMMV, I suppose.

    >
    > Some of the posters have been quite vociferous about it.


    I've noticed a certain amount of, oh, "contentiousness" maybe, on both
    sides. Sort of a :), sort of not.

    (Aside: I recognize your name from alt.folklore.computers, where
    you seem, oh, much more mild-mannered. Hm.)

    > >> >myself some Scheme and in the process trying make it conform to
    > >> >my strongly-typed-languages-trained mindset, and I'd probably
    > >> >have done better to get a good introductory book and try to grok
    > >> >the no-types(?) mindset. (Maybe I'll try again at some point.)

    >
    > I am pretty much past the intro stage and into the pain stage
    > where there is not so much help.


    That people are not willing to offer further help when many of their
    previous suggestions have been rejected should not be a total surprise.
    <shrug>

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Jul 2, 2011
    #19
  20. On 02/07/2011 2:33 PM, wrote:
    > (Aside: I recognize your name from alt.folklore.computers, where
    > you seem, oh, much more mild-mannered. Hm.)


    CLJP seems to bring out a contentious streak in people.
    supercalifragilisticexpialadiamaticonormalizeringe, Jul 2, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.G.Srivatsa

    string vs stringbuilder

    H.G.Srivatsa, Aug 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    17,047
    Richard Grimes [MVP]
    Aug 25, 2005
  2. =?Utf-8?B?SGFuZGk=?=

    no stringbuilder in C#2.0?

    =?Utf-8?B?SGFuZGk=?=, Sep 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    992
    =?Utf-8?B?SGFuZGk=?=
    Sep 11, 2004
  3. ESPN Lover

    How to reset a StringBuilder?

    ESPN Lover, Oct 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    6,954
    ESPN Lover
    Oct 14, 2004
  4. darrel
    Replies:
    2
    Views:
    472
    Juan T. Llibre
    Jan 28, 2005
  5. =?Utf-8?B?SnVzdGlu?=

    Double Quotes in a stringbuilder

    =?Utf-8?B?SnVzdGlu?=, Mar 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,889
    Mythran
    Mar 18, 2005
Loading...

Share This Page