Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete List of immutable JDK classes?

is there a list of de-facto immutable classes in the jdk?

technically Immutable classes include the obvious Integer, Double etc..

de-facto immutable will include for example java.lang.String - it might technically be mutable but de-facto it is not.

Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?

if you cannot provide a complete List, i would already be happy if you know a bunch of classes which state immutability in its javadoc..

like image 642
Andreas Petersson Avatar asked May 13 '11 21:05

Andreas Petersson


People also ask

How many immutable classes are there in Java?

In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable.

What are all immutable classes?

Immutable class means once the object of the class is created its fields cannot be modified or changed. In Java, all the wrapper classes like Boolean, Short, Integer, Long, Float, Double, Byte, Char, and String classes are immutable classes.

What is immutable list in Java?

ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown.

Can we use list in immutable class in Java?

The immutable list is thread-safe because Making it immutable will create a “read-only” version of our set. As we already discuss the immutable list is ready only. Because if the user tries to modify(add or remove from list ) the list then the compiler will throw UnsupportedOperationException.


2 Answers

You can use MutabilityDetector and feed it JARs from the JDK to enumerate most immutable classes. It's "most" because it's so strict that even the tiny bit of state change in java.lang.String is enough to make it considered mutable, but the goal is to account for this by 1.0 release.

You can download the latest JAR here: https://github.com/MutabilityDetector/MutabilityDetector/releases

Here's an example of it's use. This is what I used to get most of the immutable JDK 1.7 classes on OSX:

java -jar MutabilityDetector-0.9.5.jar -r IMMUTABLE -cp /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar

Here's the output (slightly cleaned up): https://gist.github.com/CalebFenton/85fc87edf64033afe110

I needed to do this for Android framework classes. The tricky part was finding a JAR with Android classes, and not just stubs which is the one included in the SDK. The good people at Robolectric make one, which you can download here: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22android-all%22

You can see the list of immutable Android classes I use, which includes many JDK classes here: https://github.com/CalebFenton/simplify/blob/master/smalivm/src/main/resources/immutable_classes.cfg

like image 190
Caleb Fenton Avatar answered Oct 19 '22 15:10

Caleb Fenton


I try to compile the list as much as I can:

  1. java.lang.String The wrapper classes for the primitive types:
  2. java.lang.Integer
  3. java.lang.Byte
  4. java.lang.Character
  5. java.lang.Short
  6. java.lang.Boolean
  7. java.lang.Long
  8. java.lang.Double
  9. java.lang.Float
  10. java.lang.StackTraceElement (used in building exception stacktraces)
  11. Most of the enum classes
  12. java.math.BigInteger
  13. java.math.BigDecimal
  14. java.io.File
  15. java.awt.Font
  16. java.awt.BasicStroke
  17. java.awt.Color
  18. java.awt.GradientPaint,
  19. java.awt.LinearGradientPaint
  20. java.awt.RadialGradientPaint,
  21. java.awt.Cursor
  22. java.util.Locale
  23. java.util.UUID
  24. java.util.Collections
  25. java.net.URL
  26. java.net.URI
  27. java.net.Inet4Address
  28. java.net.Inet6Address
  29. java.net.InetSocketAddress
  30. most subclasses of java.security.Permission
like image 25
kavi temre Avatar answered Oct 19 '22 17:10

kavi temre