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...
>
> 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).
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 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.
> 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