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

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.