4

In object oriented languages, creation of new object is by using the new keyword (since memory allocation in java done dynamically).

Even though String is a class how its object is created without the new Keyword?

Even though it uses string pooling I am not able to understand it clearly: "It is possible to create a user defined class where we can initialize variable directly like String"

Rotem
  • 1,381
  • 1
  • 11
  • 23
gajapathy p
  • 113
  • 1
  • 13
  • 13
    No it's not. The compiler treats `String literals` in a special way not possible for other objects (except for autoboxed primitives). – Kayaman Feb 02 '17 at 13:14
  • You cannot, please go through these threads : [Difference between string object and string literal](http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal) [What is the difference between “text” and new String(“text”)?](http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext) for detailed info. – Rohit Gulati Feb 02 '17 at 13:17
  • If you *really* wanted to do something like this, you could switch to Scala (another JVM language), and create an implicit class that automatically turns strings in certain contexts into a class. I'd question why this is a need though. – Carcigenicate Feb 02 '17 at 13:18
  • 1
    @Kayaman (nit-picking) also possible for Lambdas... [:-) – user85421 Feb 02 '17 at 13:29
  • @CarlosHeuberger True, I was going for the "it's not possible", but then I started thinking, and well, thinking has gotten me into trouble before. – Kayaman Feb 02 '17 at 13:34

2 Answers2

5

The mechanism enabling you to create String objects with string literals is built into the compiler and JVM. It is not available for use with objects of user-defined types.

When you write for the first time

String s = "sometext";

the compiler emits two things:

  • A constant pool entry with "sometext" in it, and
  • An instruction that sets s to reference to the entry in the constant table.

If you write

String t = "sometext";

in the same class, the compiler will reuse an existing constant for "sometext", rather than creating a new one.

At runtime, JVM creates a new String object for each entry from the constant table, and gives your program access to them. Essentially, JVM invokes new on your program's behalf, and hands it a ready-to-use object.

Similar system is in play when you create instances of primitive wrappers with autoboxing. The common thing, however, is that it requires support from the compiler, and is not available for user-defined types.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @ΦXocę웃Пepeúpaツ This depends on JVM. "Same class" is the required minimum, but [other JVMs](http://stackoverflow.com/q/16690815/335858) can share strings more aggressively. – Sergey Kalinichenko Feb 02 '17 at 13:45
0

In Java, Strings are immutable and optionally pooled ("interned").

"sometext" is just an instance of a String that comes from the static pool.

You cannot create a user-defined String because java.lang.String is a final class, exactly for the reason of immutability (you can share duplicates by pointing them to a single instance).

rustyx
  • 80,671
  • 25
  • 200
  • 267