Why is String the only class for which Java literals exist?

Why is the java.lang.String

only class for which there are two ways to create:

1) With the usual way with the keyword <<21>.

String s = new String("abc");

      

2) With a String

literal (which is only available with a class String

)

String s = "abc";

      

So why are there String

literals and literals for any other classes?

+2


source to share


10 replies


A string, being an ordinary class, represents one of the main elements of programming (the rest are numbers and booleans). Therefore, Java considered String objects to be semi-primitive variables from day one and allowed it.

Later, when autoboxing became part of the standard compiler, you could also create numeric wrapper objects in this way, i.e.



Integer i = 123;

      

as Tom Hawtin-tackline mentions in the comments to the question.

+2


source


Short answer because language designers felt like that.



I am guessing String is such a generic class that they made an exception. I'm sure there are a few more similar exceptions (see comment by Tom Hotins)

+10


source


Autoboxing aside (which IMO ignores), literals are primitive types, not classes, just implicit conversions to a wrapper class), the constructor is String(String)

really useful. It takes a copy of the character data, which means that if the original string is small but with a lot of support char[]

, the newline is independent of it.

This can be important if you are reading short lines from a file, eg.

And why there are string literals: because they are useful. What would you have instead? You want to force developers to use:

String x = new String(new char[] { 'h', 'e', 'l', 'l', 'o' });

      

and at the same time not receive internment?

What other class do you need for a literal? Literals for maps and lists would probably be useful - I can't remember if they come in Java 7 or not. You can think of array initializers as "a bit like a literal", since they are shorthand for object initialization.

+5


source


I suppose because String is such a fundamental data type that they treat it at the same level as primitives.

Btv,

new String("abc");

      

actually creates 2 lines:

  • internee from which you lose link
  • a new copy with the same content

Personally, I never need to use the String constructor.

+3


source


The types of objects that have literals Strings , null and class literals .

It is also possible to use initializers for arrays , and primitive literals can also be used to create objects as long as they are used as initializers which fit "two ways to create" in simple syntax without being true literals (they can only be used to initialize a value, not in all places that might result from an object expression).

If you use other techniques like serialization and reflection, you can also break the "two ways of creation".

+1


source


Java 7 is slated to provide native collections capability through literals similar to massive literals:

+1


source


Last thing I checked:

Integer wInt = 1;

      

Works well.

0


source


The latter form of creation also includes string interning. That is, if you have code:

String s = "abc";
String t = "abc";

      

the compiler will optimize this and you will end up with two references to the same String object. (s == t will be true).

Working with strings for string expressions too.

0


source


There are, of course, literals for all primitive numeric types, and booleans β€” true and false β€” and symbols. I suppose you could answer that in your question you technically said "no literals for other classesOkay. I think someone pointed out that 'null' is a literal object, so your statement is technically imprecise, but I think that's the only other example.

But then, the next obvious question is, what other classes would you like to have literals? If you were designing a language, what would a BufferedReader literal look like? Or a literal ResultSet? Or JCheckBox?

In theory, we can say that, for example, "File literal" is a filename enclosed in some special delimiter, for example "$ / home / bob / myfile.txt $". But then someone might say, "What about the JButton, could we make a literal for that, like the text of the button and the ActionListener that it triggers and the appropriate delimiters, like"% Submit, SubmitListener% ". And then what about Sockets, could we put the hostname in some special delimiter, etc. I think it's pretty clear that we quickly ended up with punctuation marks to be used as delimiters, so we need to write some text to define the class we are talking about, for example, "File (" /home/bob/myfile.txt ") or Socket (" www.example.com ", 631).But it looks very much like a constructor, and we'll go back to what the Java authors actually did.

The number of "things" that can be expressed purely by the nature of the meaning (for example, all numbers are a numeric literal) or with punctuation is rather limited by the number of punctuation marks that can reasonably be placed on the keyboard. And do you want a keyboard with 600 special icons for all the different things you might want to talk about? Words are much simpler and more flexible.

The string is used so often and the alternatives would be so awkward that it makes sense to make it a special case.

0


source


String literals are a convenient short cut with well-defined precedence (in C) and syntax. If the Java developers wanted to lure C ++ programmers, string literals must exist. Fortunately, string handling is much clearer in Java than in C ++.

No other standard objects had actual syntax - how would you write an alphabetic character?

0


source







All Articles