Variable Declaration VS Object Creation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Variable Declaration VS Object Creation

I don't Really understand when we can do String A = "Hello"; then why need String A = new String("Hello"); both seems to doing same thing to me ,,,, Please Explain me

26th Jun 2022, 4:38 PM
Aman Modanwal
Aman Modanwal - avatar
3 Answers
+ 1
Hope it helps.. https://www.sololearn.com/Discuss/3033964/?ref=app edit: String in java is not primitive type.
26th Jun 2022, 5:07 PM
Jayakrishna 🇮🇳
0
From my understanding string, char, int, float, double, and boolean are primitive types in Java that do not require explicit instiantiation.
26th Jun 2022, 5:01 PM
Charles Jones
Charles Jones - avatar
0
as you can see a b are same object in memory, but c not String a = "Hello"; String b = "Hello"; String c = new String("Hello"); System.out.println( System.identityHashCode( a) +"\n"+ System.identityHashCode( b) +"\n"+ System.identityHashCode( c) ); also String is Object and all public objects can be created by new keywords
27th Jun 2022, 1:48 AM
zemiak