1. Home
  2. Computing & Technology
  3. Java

Immutable

By Paul Leahy, About.com

Definition:

Immutable means unchangeable. In Java, when an object is defined as being immutable it means that once it has been initialized its state cannot be changed.

Primitive data types (i.e., int, short, long, byte, char, float, double, boolean) can be made immutable by using the "final" keyword. Once they have been assigned a value it cannot be changed.

Examples:

Consider this code:

String changeString = "hello ";
changeString = changeString + "world";
System.out.println(changeString);

The output as expected is:

hello world

You might think the program is simply changing the state of the String object to append "world" but it's not that simple. As Strings are immutable and cannot have their values changed, what happens is a new String object is created with a state of "hello world" and assigned to the changeString variable.

Glossary:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Java Glossary
  5. I
  6. Definition for the Term: Immutable>

©2009 About.com, a part of The New York Times Company.

All rights reserved.