Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

622
Views
In Java, why can we use the String class as if it were a primitive?

In Java we usually do:

Class myObject = new Class();

because the new keyword returns an address.

But why can we do this?:

String myString = "Hello";

as if String were a primitive?

I asked this to my teacher and he replied that it is because what is in quotes is equivalent to an address, but he is not sure. Can you confirm?

Is "Hello" stored in an instance variable of the class String?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

...as if String were a primitive?

As Jon points out in a comment, "has a literal form" != "is a primitive type." :-) Strings have a literal form in Java because the spec defines one for them. String is still an object type.

Some languages have other object types that also have literal forms (JavaScript, for instance, has literal forms for regular expressions, arrays, and non-array objects [its literal form for strings does actually define what it calls a string primitive rather than string object — JavaScript has both primitive and object versions of strings]). I think strings are the only one in Java (other than null, which is the literal for "no object"), although of course there are array initializers, which are similar but not quite the same as literals.

about 4 years ago · Santiago Trujillo Report

0

The fact that you can use a literal expression doesn't mean that the expression results in a primitive type! That is simply a misconception on your end.

You see:

 Integer[] numbers = { 1, 2, 3 };

is also a literal (well, "almost"; as you can only use it for such kind of array init statements); but it is not a primitive type!

And to ask the why part in your question: chances are - simply for convenience. When Java was created, it was meant to be "better" than the C and C++ of those days. And guess what: dealing with strings was pretty annoying and error prone in those languages.

In that sense: introducing string literals could be seen as unique selling point to differentiate Java from its competition!

about 4 years ago · Santiago Trujillo Report

0

Other answers are pretty good. Some specific points:

I asked this to my teacher and he replied that it is because what is in quotes is equivalent to an address, but he is not sure. Can you confirm?

Yes. He is right. As others have pointed out, a string literal, i.e. a valid Unicode character sequence enclosed inside a pair of "s is recognized by the Java Language Specification. When the Java compiler (the javac program) encounters such a literal (e.g. "Hello") in your Java source file, it takes it literally and places it in a specific section inside the compiled class file. This section is called the runtime constant pool. It then creates a particular instruction called ldc that it places in the class file where the compiled code for your initialization is stored. For instance, the body of the main method in the following Java source code:

public class StringAsPrimitive {
    public static void main(String[] args) {
        String s = "Hello";
    }
}

gets compiled to:

     0: ldc           #2                  // String Hello
     2: astore_1
     3: return

(There is no magic here. Just compile your code using javac program and view the class file using the javap program. You can even see the contents of the .class file using a hex viewer and you will see the String Hello literally).

This code is then interpreted by the JVM when you ask the java program to run this class file.

The ldc instruction to the JVM is documented here. This is what the JVM then does when it interprets #2 which is a reference to the actual location where the literal string "Hello" is stored in the computer's memory (RAM):

Otherwise, if the run-time constant pool entry is a reference to an instance of class String representing a string literal (§5.1), then a reference to that instance, value, is pushed onto the operand stack.

So, what your teacher said turns out to be right, although we don't say it quite that way. In Java, there are two kinds of types: primitive and reference. So, it would be more appropriate to say that the value of the String literal is a reference (and not address).

Is "Hello" stored in an instance variable of the class String?

No. The String variable initialized to a String literal could be anything, a static or class variable, an instance variable or a local variable (my code above). It is stored in the class file: StringAsPrimitive.class and later it is available to the JVM in a particular memory location. This happens when the JVM loads the class.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!