I am really new to Java.I read about String data type, all books told me that String is class.But why we could use String variables without creating the instance of theclass ?String myString;myString = "Ini string loh...";As a class, String variables should be created as the instance before wecould use them, right ?String myString;myString = new String("Ini string loh...");So, I assume the first one....String is a primitive data type not class.Please, explain me about this situation ?
Java is a true Object Oriented Language. Please checkout the information at:http://java.sun.comStudy the online tutorial too.
When you state :String myString;myString = "Ini string loh...";java compiler turns it in :String myString;myString = new String("Ini string loh...");Because the "Ini string loh..." is read as it werenew String("Ini string loh...") ....
Well, if it's the fact...I agree, but it's not good for newbie, I think :).(Because, primitive data type totally different from object.)Ok, thanks for all responds, I accepted it.
String is an Object as is everything in Java. Youcould write "new String(...)", but if you don't thisis essentially what the compiler does.// These statements are essentially the same, sincethe compiler handles this for youString str = new String( "This is a string" );String str = "This is a strint";
I'm not sure everything in Java is object.There are data-types, like boolean and int, thataren't objects ...for that sake there the static classes like Integer .
I believe that the use of strings in to form of "string" is a shortcutbecause of how often strings are used.
Okay I stand corrected, those are called primatives.I think the word you are trying to find instead ofstatic classes are non mutable classes which anexample is String, and all of your primative wrapperssuch as Integer, Long, Float, Char, etc.
That's waht I meant and couldn't express corectly inEnglish.