3

Excuse me if this question is already asked or if its very old.

String is a Class. We are able to assign the value to String without creating new instance object.

like String someString = "Value";

How this is working without creating instance for String? Is it possible to create user defined class to assign values directly without creating new object?

2
  • I think you mean, "like String something = "Value";" Commented Nov 21, 2013 at 12:54
  • String = "Value"; won't work :) Commented Nov 21, 2013 at 12:55

5 Answers 5

3

See this interesting article:

String is Really Special

The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language. Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces. For performance reason, Java's String is designed to be in between a primitive and a class.


Also:

Java has provided a special mechanism for keeping the String literals - in a so-called string common pool..

When you do:

String myStr1 = "Hello";
String myStr2 = "Hello";

Then

myStr1 == myStr2 is true, because they're both stored in the pool (Read the article).

But if you construct a new String object:

String myStr1 = new String("Hello");
String myStr2 = new String("Hello");

Then the references are not equal.

I highly recommend to visit the JLS. It says that Strings are treated in a different way, just like that :)

Sign up to request clarification or add additional context in comments.

Comments

1
String str = "abc";

is equivalent to:

 char data[] = {'a', 'b', 'c'};
 or
String str = new String(data);

For user defined classes, you have to create an object. Refer to docs please for more information: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Comments

0

Strings are a special case. The String literals are created in the string pool. After a string is added in the pool, if another with the same value will be declared, the reference will point to the same string in the pool.

And to answer your question, you cannot create user defined classes and not use new. Only if you use only static methods and fields on that class and then you use ClassName.StaticFieldOrMethod.

Comments

0

To know more about how String class works refer

http://java.dzone.com/articles/string-memory-internals

Comments

0

Let us try to understand this with a simple program;

    package program;

    public class TempList {

      public static void main(String[] args) throws Exception {
        String stringWithoutNew = "Test";
        String stringWithoutNew_temp = "Test";

        String stringWithNew = new String("Test");
        String stringWithNew_temp = new String("Test");

        if (stringWithoutNew == stringWithoutNew_temp) {
          System.out.println("This works !!");
        }

        if (stringWithoutNew == stringWithNew) {
          System.out.println("This doesn't work !!");
        }

        if (stringWithNew == stringWithNew_temp) {
          System.out.println("This doesn't work either !!");
        }

      }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.