O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating String Objects

Strings are objects. Their superclass is java.lang.Object (they inherit all of the properties and methods defined in the Object class, and then define some of their own). As we know, everything in Java is an object (except for the primitive types such as int, boolean, and char), and they all have corresponding object types that are often called “wrappers”.

If you want to be a big nerd and make everyone hate you, create a String like you would a regular object:

String s = new String("Sucker!!!");

If, for some reason, you do not want to be a big gigantic nerd and force everyone to hate you, use the following simpler, more common, and more efficient syntax to create a String:

String s = "All the cool kids do it";

If Java ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required