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

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...

Server-Side Pagination with React-Table and Spring Boot JPA with H2 Database

Pagination is a common technique used to split large amounts of data into smaller, more manageable chunks. With server-side pagination, data is retrieved from the server in smaller batches, reducing the amount of data transferred over the network and improving application performance. React-Table provides a wide range of built-in features such as sorting, filtering, pagination, row selection, and column resizing. These features can be easily configured and customized to fit specific requirements. For example, you can customize the sorting behavior to handle multiple sorting criteria, or you can add custom filters to the table to handle complex data filtering scenarios. Additionally, React-Table provides a flexible API that allows developers to extend its functionality with custom hooks, plugins, and components. This means that you can easily add custom functionality to the table, such as exporting data to CSV or integrating with external data sources. In terms of styl...