Skip to main content

Understanding Immutability in Java: Strings and Other Immutable Objects

In the Java programming language, an object is considered immutable if its state cannot be changed after it is created. Immutable objects are useful for several reasons, including thread safety, security, and ease of reasoning about code. In this blog post, we will discuss immutability in Java and focus on two examples of immutable objects: strings and wrapper classes.

Strings

Strings are perhaps the most well-known example of immutable objects in Java. Once a string object is created, its value cannot be changed. This is because strings are implemented as an array of characters, and the contents of an array cannot be changed after it is created. Here is an example to illustrate this:

String s = "hello";
s.toUpperCase(); // returns "HELLO"
System.out.println(s); // prints "hello"

In this example, the "toUpperCase" method returns a new string object with all of the characters in uppercase. However, the original string object "s" remains unchanged. This is because the "toUpperCase" method does not modify the existing string object, but rather creates a new one.

Wrapper Classes

Wrapper classes are another example of immutable objects in Java. Wrapper classes are used to represent primitive data types (such as int, double, and boolean) as objects. For example, the "Integer" wrapper class is used to represent integer values as objects. Once an object of a wrapper class is created, its value cannot be changed. Here is an example:

Integer i = 5;
i++; // i is now 6
System.out.println(i); // prints 6

In this example, the "++" operator increments the value of "i" by 1. However, because "i" is an object of the "Integer" wrapper class, a new object is created with the value of 6. The original object with the value of 5 is discarded.

Other Examples of Immutable Objects

There are several other examples of immutable objects in Java, including:

  • The "BigDecimal" class, which is used to represent arbitrary-precision decimal numbers
  • The "LocalDate" and "LocalTime" classes, which are used to represent dates and times without time zone information
  • The "Duration" and "Period" classes, which are used to represent time spans and periods of time, respectively

Conclusion

Understanding immutability in Java is important for writing code that is thread-safe, secure, and easy to reason about. In this blog post, we discussed two examples of immutable objects in Java (strings and wrapper classes) and highlighted several other examples. We hope this blog post has been helpful in understanding the concept of immutability in Java and its practical applications. By using immutable objects, you can ensure that your code is more robust and easier to maintain.

Comments

Popular posts from this blog

JSP page directives

A jsp page directive looks like this: <%@ directive attribute="value" %> I am not gonna explain each and every page directives here . I would like to discuss about two page directives  which is import and include. First of all consider the import directive . The following simple program illustrate the use of import page directive: The output would be something looks like this: <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%>  <%@ page import="java.util.Date" %>   //page directive <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Jsp Basics</title> </head> <body> <%=new Date() %> </body> </html> Tue Nov 12 17:42:34 I...

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...