Sie sind auf Seite 1von 3

7/9/13

Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? | Explain Java Tutorial - Free Online Core Java J2EE Programming Train

Explain Java Tutorial


Free Online Core Java J2EE Programming Training Course for Beginners Categories Basics of Java (1) Core Java Training (8) Exception Handling (2) String Handling (1) Wrapper Classes (4) Explain-Java on Facebook
Find us on Facebook Mar 06

Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
String Handling, Wrapper Classes

Share:
This tutorial explains Mutability in Java Programming and why Strings & Wrapper classes are immutab le and final in java.

I love Java
Like 77 people like I love Java.

Mutable vs Immutable objects in java


Any object in java is said to be mutab le if its value can be changed. Immutable objects content can not be changed.

Is String mutable class in java?


No. String is an immutable class in java because we can not change the value of any string object. Lets go in detail with the following example coding:

L i n e1 : L i n e2 :

S t r i n gs t r=" H e l l o " ; s t r=s t r+"W o r l d " ;

F acebook social plugin

Recent Posts Java Integer Wrapper Class Tutorial: API Methods and Constructors Java Primitive Wrapper Class Tutorial: API Methods and Constructors Java Wrapper Class Tutorial: Explain Java Wrapper Classes with Examples Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2 Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1 Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? Explain Most Common Exceptions and Errors in Java with Examples What is a Java Exception? Explain Exception Handling in Java with Examples Program What is Java? How to download Java (J2SE/JDK/JRE), IDE, Javadocs? Recent Comments Prefabrykaty Betonowe on Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1

In the above example java program, after Line 1 executes, one String object will be created with value Hello and its reference will be assigned to reference variable called str. Later in Line 2, two string objects would be created with values World and Hello World, hence totally 3 String objects would be created after Line 2 gets executed. The third string object will be assigned to the String type reference variable str as shown in below figure and the remaining two string objects will be left unused (thus creating memory leaks).

website on Java 5 (J2SE 5.0/JDK www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/

1/3

7/9/13

Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? | Explain Java Tutorial - Free Online Core Java J2EE Programming Train website on Java 5 (J2SE 5.0/JDK
1.5) New Features with Examples Tutorial 1 tn requin pas cher on What is Java? How to download Java (J2SE/JDK/JRE), IDE, Javadocs? Indian job links on Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? vinayak on Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2 claindycreali on Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1 Sachin Tendulkar Centuries on Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1 Muneeb on Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? airmaxs53837 on Explain Most Common Exceptions and Errors in Java with Examples louboutin pumps on What is a Java Exception? Explain Exception Handling in Java with Examples Program Here, we are able to change the value of String reference variable str but not the actual string object Hello. When we tried to append some text World to string object, JVM creates new string object with combined text instead of changing the original string object. Because of this, lots of string objects will be created and left unused in memory every time we use + operator for appending strings. Hence, in order to avoid these kinds of memory leaks with immutable strings, we should always use mutable string classes such as StringBuffer or StringBuilder when we need multiple concatenations of strings.

What is meant by a wrapper class in java?


In Java, a wrapper class is nothing but a class in which a primitive type value is wrapped up. These primitive wrapper classes are used to represent primitive data types values as objects. For example, Integer is a primitive wrapper class which holds primitive int type value. Similarly, Float wrapper objects contain float primitive values, Character wrapper class holds a char, and Boolean wrapper class represents boolean primitive type value (true/false). To learn more about java wrapper classes, read Java Wrapper Class Tutorial: Explain Java Wrapper Classes with Examples.

Are wrapper classes mutable in java?


Once again, a big No. Like Strings, primitive wrapper classes in java such as Integer, Float, Long, Byte, Short, and Double are also immutab le classes. Once created, the numbers wrapped within the objects of wrapper classes in java.lang package cant be changed. Example java coding to show that primitive wrapper classes are immutable:

L i n e1 : L i n e2 :

I n t e g e ri=9 9 ; i=i+1 0 ;

/ /A u t o b o x i n g( A u t oB o x i n g ) / /U n b o x i n g

The above code is written in Java 5 (Java 2 Platform Standard Edition 5.0 or J2SE 5.0 or JDK 1.5). Here we have used a new feature of Java 5 called Autoboxing & Unboxing. Autoboxing is nothing but automatic conversion of primitive type values to wrapper class objects. You can learn more about this autoboxing feature in the tutorial Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial. In earlier versions of Java (JDK 1.4 or less), we can write the above code as follows:

L i n e1 : L i n e2 :

/ /I n t e g e rw r a p p e rc l a s si n s t a n t i a t i o n I n t e g e ri=n e wI n t e g e r ( 9 9 ) ; i n ti 2=i . i n t V a l u e ( )+1 0 ; i=n e wI n t e g e r ( i 2 ) ;

In either case, JVM creates an Integer object with value 99 and assigns object reference to Integer reference variable i. In order to add, subtract, or do some other operation on Integer object, first JVM extracts the primitive int value from wrapper object and then performs required operation, in this case addition. Later, the result will be wrapped in a new Integer wrapper object and assigned to Integer reference variable. That means we cant change the value of wrapper class objects. I hope by now you understand what is meant by mutability in java programming and that the Strings and Wrapper classes in Java are immutable. Let me explain the reason behind designing Strings and Wrapper classes as immutable and final.

Why Strings and Wrapper classes are immutable and final in java?
Well, we know that Strings and Wrapper classes are used as variables to store basic data. If those classes are mutable, there are plenty of chances for accidental unwanted changes to the basic data. For example, consider the following java program:

c l a s sW h y M u t a b l e { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { S t r i n gn a m e=" P a r k e r " ; www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/

2/3

7/9/13

Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? | Explain Java Tutorial - Free Online Core Java J2EE Programming Train S t r i n gn a m e=" P a r k e r " ; D o u b l es a l=6 0 0 0 0 . 0 0 ; d i s p l a y T a x ( n a m e ,s a l ) ; }

s t a t i cv o i dd i s p l a y T a x ( S t r i n gn a m e ,D o u b l en u m ){ n a m e=" H e l l o"+n a m e . c o n c a t ( " ! " ) ; n u m=n u m*3 0/1 0 0 ; S y s t e m . o u t . p r i n t l n ( n a m e+"Y o uh a v et op a yt a x$ "+n u m ) ; } } R e s u l t :H e l l oP a r k e r !Y o uh a v et op a yt a x$ 1 8 0 0 0 . 0


In the above java program, it is visible that the parameters name and sal were modified within the method displayTax(). Now, can you guess what could be wrong if Strings and Wrapper classes are mutable? Yes, you are right. Parkers name would be Parker! (worst case might be Hello Parker!) and his salary would become 18000.0. Come on! we dont have rights to reduce parkers salary. As we dont have control on others code, we cant guarantee that the data we are passing is safe and wouldnt be modified if Strings and Wrapper classes are mutable. This is the case with pass by reference of wrapper class parameters as well. And, if strings and wrapper classes are non-final, anybody can extend those classes and write their own code to modify the wrapped primitive data. So, in order to maintain Data Integrity, the variables which we are using for data storage must be read-only, i.e., Strings and Wrapper classes must be final & immutable and pass by reference feature should not be provided.

Share:
Related posts: 1. 2. 3. 4. 5. Java Wrapper Class Tutorial: Explain Java Wrapper Classes with Examples Java Primitive Wrapper Class Tutorial: API Methods and Constructors Java Integer Wrapper Class Tutorial: API Methods and Constructors Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1 Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2

w ritten by Seetha Ram Janapala Tags: application of w rapper classes in java, autoboxing, automatic w rapper, character w rapper class in java, data integrity, difference betw een primitive data types and w rapper classes in java, features of w rapper class in java, how to create a w rapper class in java, immutable, immutable strings, Integer, integer w rapper class java example, java 2 platform standard edition 5.0, java 5, java primitive w rapper types, java strings immutable, java w rapper class example, list of w rapper classes in java, mutability in java programming, mutable, mutable and immutable in java, mutable strings, Mutable vs Immutable, primitive w rapper classes in java, purpose of w rapper class in java, string w rapper class in java, StringBuffer, StringBuilder, unboxing, usage of w rapper classes in java, w hat is meant by w rapper class in java explain w ith example, w hy are w rapper classes needed, w hy do w e need w rapper classes, w hy do w e use w rapper classes in java, w rapper class methods in java, w rapper classes immutable, w rapper classes in java w ith example, w rapper classes tutorial, w rapper data types in java, w rapper objects in java, w rapper types in java

2 Responses to Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
1. Muneeb Says:
May 24th, 2013 at 11:09 am

Excellent explanation!!

2. Indian job links Says:


June 3rd, 2013 at 8:17 pm

Nice article.

Copyright 2013 Explain-Java.com. All rights reserved.

www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/

3/3

Das könnte Ihnen auch gefallen