Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Saiteja Vemula
7,162 PointsWhy should we use "new" keyword to declare an instance variable? Why don't we use "new" keyword for creating a String?
For example, you've created a class named Abcd, and you use it in your main program (i.e., in the main method). We write it as : Abcd obj = new Abcd();
But for Strings, we write :
String a = "Bla Bla Bla ... ";
Why aren't we using : String a = new String();
??
1 Answer
Simon Coates
28,695 PointsString has special support. But I think you can explicitly call the string constructors. There is some slight difference in terms of how these are handled (see http://www.programcreek.com/2014/03/create-java-string-by-double-quotes-vs-by-constructor/ ), since java tries to optimize the use of strings.