Immutable Law of Java #20: The new Keyword

Learning Java? Do you know someone that is? There are plenty of areas to get tripped up when you're learning the language. That's why we have the immutable laws. They help clear up the confusion.

Immutable Rule of Java # 20

im•mu•ta•ble [him-moo-tribble]
  – an adjective: not mutable; unchangeable; changeless.

If you’re learning Java, it’s important to know some of the immutable rules of the language. I’m going to discuss about a dozen of them in a series of articles, with the first one being discussed today: immutable rule #20. You may be wondering why are we starting at rule #20? You’ll have to wait for immutable rule #37 to find that out, so be patient.

So here’s the first immutable rule of Java, also known as immutable rule #20:

In Java, all objects are created using the keyword new, except for the objects that are created without using the keyword new.

You know, when you create an instance of an object, you need to do something like this:

Byte me = new Byte(1); or Long johns = new Long(2);

So, whenever you create an instance of an object in Java, you always use the keyword new; always; every time...except that is, for the times when you don’t.

Stringing us along.

One of those times you don't need to use the keyword new is with String objects. String objects don’t need to be created with the keyword new. You can create a String like this:

String bean = “lima”;

Of course, Strings really sit on the fence with regards to this issue, as you can use the keyword new to create them if you really want. So, you could actually do this:

String lima = new String(“Peru”);

Of course, you shouldn’t do that, unless of course, you think you should (another immutable rule, I guess). It has something to do with the String pool or something. If you use the keyword new when creating a String, that instance gets its own, unique, special memory location, even if there are a hundred other Strings that use the exact same set of characters in their composition. If you don’t use the keyword new, two Strings that are the same, such as “Peru” and “Peru” will end up sharing the same memory location. It’s a great optimization to slap two Strings that are the same into the same chunk of memory, especially if you’re using plenty of String objects in your application.

I need arrays

Arrays are those other objects you can create without the new keyword. You can create an array using the new keyword of course:

Short[] circuit = new Short[2];

Of course, you can do it the following way, which doesn’t use the new keyword at all:

int[] eresting = {1,2};

I think this way of declaring and initializing arrays was kept in the language to keep really old people happy, because it looks like an ancient hieroglyphic language like Cobol or Fortran to me. But it works, and it’s valid, and it creates an instance of an array without using the keyword new.

"I asked my boss for a raise, but he told me to use ArrayLists instead."

So, that’s about it. If you’re learning Java, you need to know this type of stuff. You need to know that in order to create an instance of a class, you need to use the keyword new, that is, with the exception of those times that you don’t need to use the keyword new. Remember that. It will help you master Java.

Dig Deeper on Software development techniques and Agile methodologies

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.