Beruflich Dokumente
Kultur Dokumente
Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java? | Explain Java Tutorial - Free Online Core Java J2EE Programming Train
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.
L i n e1 : L i n e2 :
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).
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.
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 :
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:
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 ) ; }
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!!
Nice article.
www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/
3/3