Why String class need not use new operator to create object

Hi, i am having a doubt in core java...

For every class v r creating object using new operator...but comming to String class v need not explictly declare the new operator to create the object.

For instance :

class MyClass

{

public void hai()

{

System.out.println ("Hai");

}

}

when v are using this class v create it like this .

MyClass mc = new MyClass();

mc.hai();

But where as in Strings we can use String str="hai";

hai.trim();

with out using new operator or any factory methods.... why?

Thanks in advance...

[614 byte] By [pradeeppantulaa] at [2007-11-27 10:28:50]
# 1

>

> But where as in Strings we can use String str="hai";

> hai.trim();

> with out using new operator or any factory

> methods.... why?

> Thanks in advance...

I'm sure you could find the answer in any Java book..

It is all about "String literals". Read carefully JSL 3rd ed., chapter 3.10.5 (http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101083).

valneda at 2007-7-28 17:53:56 > top of Java-index,Java Essentials,Java Programming...
# 2

Java platform provides wrapper classes for each of the primitive data types: int, double, short, float, String, ....

Read this: http://java.sun.com/docs/books/tutorial/java/data/index.html

Hope That Helps

java_2006a at 2007-7-28 17:53:56 > top of Java-index,Java Essentials,Java Programming...
# 3

Strings are part of the language, literals are handled by the compiler.

BIJ001a at 2007-7-28 17:53:56 > top of Java-index,Java Essentials,Java Programming...
# 4

> Java platform provides wrapper classes for each of

> the primitive data types: int, double, short,

> float, String, ....

You mean String is a primitive data type ? Then you're wrong.

Moreover, this wrapper class remark is not relevant in this context.

TimTheEnchantora at 2007-7-28 17:53:56 > top of Java-index,Java Essentials,Java Programming...
# 5

> Java platform provides wrapper classes for each of

> the primitive data types: int, double, short,

> float, String, ....

>

> Read this:

> http://java.sun.com/docs/books/tutorial/java/data/inde

> x.html

>

> Hope That Helps

Don't get all upset and what-not, but String isn't a primitive type, and hence there isn't a wrapper for it

georgemca at 2007-7-28 17:53:56 > top of Java-index,Java Essentials,Java Programming...