You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dan Allen <da...@mojavelinux.com> on 2003/03/10 01:55:08 UTC
purpose of Token Constants
I have seen this used in several places and I have to ask, why? Ted
Husted uses it in Scaffold and there are several examples around the
web that use it as well. What am I talking about?
consider this short code snippet
return mapping.findForward(Tokens.SUCCESS_KEY);
How is that any different than
return mapping.findForward("success");
Both are in english, one just resolves to a variable the other is
static. I guess I could understand the use of tokens for placing
keys into the request scope since you might one day find you have a
conflict, but besides that, what is the purpose?
Dan
--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umm... i guess this is my signature. 8-}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: purpose of Token Constants
Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Scott" == Scott Barr <sb...@chariot.net.au> writes:
Scott> Hi David
Scott> Is that all unique occurences of each unique String within the JVM? If
Scott> so, there is still creation, and garbage disposal once the String goes
Scott> out of scope, yes?
If we're just talking literals, they never go out of scope. All references to
the same string literal all refer to the same object, and they're never
collected. You can create a String object that contains the same contents, but
those are normal collectable objects, unless they're manually interned, which
is another story.
--
===================================================================
David M. Karr ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net ; SCJP; SCWCD
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: purpose of Token Constants
Posted by Scott Barr <sb...@chariot.net.au>.
Hmm... Reading that after I sent it, I think I secretly have a thing for
the
word, "unique" :)
Scott
On Mon, 2003-03-10 at 14:45, Scott Barr wrote:
> Hi David
>
> Is that all unique occurences of each unique String within the JVM? If
> so, there is still creation, and garbage disposal once the String goes
> out of scope, yes?
>
> Scott Barr
> www.exergonic.com.au
>
>
> On Mon, 2003-03-10 at 14:39, David M. Karr wrote:
>
> > >>>>> "Scott" == Scott Barr <sb...@chariot.net.au> writes:
> >
> > Scott> Also, every time you reference a String literal, an object is created.
> > Scott> Using the static constant avoids this. Just good practice.
> > Scott> Saving the creation of a couple of objects is moot, but on large
> > Scott> projects on stressed servers, every little bit counts.
> >
> > Actually, all occurrences of the same unique string constant reference the same
> > interned String object, whether it's referenced from a constant definition, or
> > the raw string itself (you can verify this by comparing two identical strings
> > with "=="). Nevertheless, referencing string constants is still better than
> > referencing the raw string.
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: purpose of Token Constants
Posted by Scott Barr <sb...@chariot.net.au>.
Hi David
Is that all unique occurences of each unique String within the JVM? If
so, there is still creation, and garbage disposal once the String goes
out of scope, yes?
Scott Barr
www.exergonic.com.au
On Mon, 2003-03-10 at 14:39, David M. Karr wrote:
> >>>>> "Scott" == Scott Barr <sb...@chariot.net.au> writes:
>
> Scott> Also, every time you reference a String literal, an object is created.
> Scott> Using the static constant avoids this. Just good practice.
> Scott> Saving the creation of a couple of objects is moot, but on large
> Scott> projects on stressed servers, every little bit counts.
>
> Actually, all occurrences of the same unique string constant reference the same
> interned String object, whether it's referenced from a constant definition, or
> the raw string itself (you can verify this by comparing two identical strings
> with "=="). Nevertheless, referencing string constants is still better than
> referencing the raw string.
Re: purpose of Token Constants
Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Scott" == Scott Barr <sb...@chariot.net.au> writes:
Scott> Also, every time you reference a String literal, an object is created.
Scott> Using the static constant avoids this. Just good practice.
Scott> Saving the creation of a couple of objects is moot, but on large
Scott> projects on stressed servers, every little bit counts.
Actually, all occurrences of the same unique string constant reference the same
interned String object, whether it's referenced from a constant definition, or
the raw string itself (you can verify this by comparing two identical strings
with "=="). Nevertheless, referencing string constants is still better than
referencing the raw string.
--
===================================================================
David M. Karr ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net ; SCJP; SCWCD
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: purpose of Token Constants
Posted by Scott Barr <sb...@chariot.net.au>.
Also, every time you reference a String literal, an object is created.
Using the static constant avoids this. Just good practice.
Saving the creation of a couple of objects is moot, but on large
projects on stressed servers, every little bit counts.
Scott Barr
www.exergonic.com.au
On Mon, 2003-03-10 at 12:50, Paul Linden wrote:
> Dan Allen wrote:
>
> >I have seen this used in several places and I have to ask, why? Ted
> >Husted uses it in Scaffold and there are several examples around the
> >web that use it as well. What am I talking about?
> >
> >consider this short code snippet
> >
> > return mapping.findForward(Tokens.SUCCESS_KEY);
> >
> >How is that any different than
> >
> > return mapping.findForward("success");
> >
> >Both are in english, one just resolves to a variable the other is
> >static. I guess I could understand the use of tokens for placing
> >keys into the request scope since you might one day find you have a
> >conflict, but besides that, what is the purpose?
> >
> >Dan
> >
> >
> >
> You should always use static constants when referencing Strings that are
> used in more than one place - if you had "sucess", your program would
> compile ok, but you would get a runtime exception when your action is
> accessed and Struts looked for the mapping for "sucess" - possibly after
> deployment if your test coverage is inadequate, but at the earliest once
> you've dropped your app into your webapps directory. If you have
> Tokens.SUCESS_KEY, your program wouldn't compile and you could fix the
> error immediately.
>
> Paul
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
Re: purpose of Token Constants
Posted by Paul Linden <pa...@openwave.com>.
Dan Allen wrote:
>I have seen this used in several places and I have to ask, why? Ted
>Husted uses it in Scaffold and there are several examples around the
>web that use it as well. What am I talking about?
>
>consider this short code snippet
>
> return mapping.findForward(Tokens.SUCCESS_KEY);
>
>How is that any different than
>
> return mapping.findForward("success");
>
>Both are in english, one just resolves to a variable the other is
>static. I guess I could understand the use of tokens for placing
>keys into the request scope since you might one day find you have a
>conflict, but besides that, what is the purpose?
>
>Dan
>
>
>
You should always use static constants when referencing Strings that are
used in more than one place - if you had "sucess", your program would
compile ok, but you would get a runtime exception when your action is
accessed and Struts looked for the mapping for "sucess" - possibly after
deployment if your test coverage is inadequate, but at the earliest once
you've dropped your app into your webapps directory. If you have
Tokens.SUCESS_KEY, your program wouldn't compile and you could fix the
error immediately.
Paul
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org