Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Java String

created by JeffMagnus

(idea) by jbarn (6.4 y) (print)   ?   (I like it!) Mon Oct 15 2001 at 22:14:27

General Notes

String is a sneaky member of the Java standard library:

  1. Instances can be created as literals (without a constructor). For example, a new String s can be created by writing:
    String s = "Foo!";
    
    rather than using a constructor, as in:
    String s = new String("Foo!");
    

    Readers will note the recursive nature of the last example, as the constructor's argument ("Foo!") is itself a string literal. In most implementations of String, calling new String(String s) will copy the parameter String's implementing character array using native array copy methods, making sure that the array isn't longer than the stored String.

  2. The '+' and '+=' operators are overloaded for String concatenation, e.g.

    String s = "Foo!" + "Bar!";
    // s == "Foo!Bar!"
    
    s += "Baz!";
    // s == "Foo!Bar!Baz!"
    

    The fact that operator overloading is available (in a limited fashion) for use with the String class but not in user-defined classes is often cited as a reason why Java sucks.

Comparing With String Literals

When comparing String variables to String literals, many Java novices munge together something like this:

String foo = getARandomStringFromElsewhere();

if (foo.equals("Foo!")) {
    // do something
}

"Ah," you say, "But foo could be null!"

The next iteration in the novice's journey usually looks a bit like this:

if (foo != null && foo.equals("Foo!")) {
    // do something
}

This will work, but exceptionally lazy typists (like myself) object on moral (read: keystroke count) grounds. One of two approaches will make the world Safe for DemocracyTM:

  1. Compare your String to a static variable you've stashed somewhere. Either a public static final in an abstract class or a simple String in an interface will work. I'm partial to the interface approach, myself. Your comparision might look like this:
    if (IConstants.FooConst.equals(foo)) { ... }
    
  2. Or you can be weird. String literals in Java ("Foo!" is a String literal) are perfectly valid instances of java.lang.String, so:
    if("Foo!".equals(foo)) { ... }
    
    will work fine.


printable version
chaos

StringBuffer String Confessions of a Former Phone Sex Operator Java
E2 HTML tags kurt the pope Performance tuning weird
Java package overview Unicode European Alphabets Take an object. Do something to it. Do something else to it. reputation
wild geese TM Java Native Interface Sex and the City
12 string Why Perl sucks foo
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Drink up!
Cryptozoology
Why are homosexuals held to different standards of conduct than heterosexuals?
Native American Church
The E2 Backstory
Derivation of Days and Months
Brahman and the fractal nature of the universe due to the chaotic nature of string theory
Mary Poppins
How to locate Polaris, the North Star
Sins of the World
An Artistic Analysis of 2001: A Space Odyssey
bits
Soap
History
New Writeups
sam512
Moon Base Shackleton, 1978(fiction)
Pavlovna
toy boy(person)
XWiz
tear jerker(review)
Heitah
Anarchy is Order(idea)
jessicaj
July 26, 2008(dream)
Berek
ABBA(person)
devolution
k-hole(place)
Nadine_2
The Sound Of Madness(review)
Twin Eclipse
Conversations with God: An Uncommon Dialogue(idea)
SwimmingMonkey
Conversations with Fo Fo- the Loneliest dog in Purgatory(fiction)
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
antigravpussy
I fall silent, listening. The breadcrumbs are talking about us(person)
Everything 2 is brought to you by the letter C and The Everything Development Company