About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.


https://blog.jetbrains.com/kotlin/2017/04/kotlin-1-1-2-is-out/ When I saw the news, I remembered the story that Kotlin's val, which was taught by a lead engineer in the field who had participated before, is an Immutable object in Java. At that time, I was told about each classification of Java and Kotlin's ʻImmutable / Mutable / Const / Variable`, but I forgot until I saw the news w ~~

So, what I remembered is not a memory but a record while it is hot, and it is likely to be completely forgotten during Golden Week, so Java and Kotlin's ʻImmutable / Mutable / Const / Variable` classifications I'll write it down.

It's a long time ago, so there may be some mistakes, so I'd appreciate it if you could point it out.

Kotlin's Immutable / Mutable / Const / Variable classification

- Const ........... companiaon object { const val }
- Variable
  - Immutable ..... val
  - Mutable ....... var

It will be.

However, as a Kotlin language specification,

- Variable
  - Immutable ..... val
    - Const ....... companiaon object { const val }
  - Mutable ....... var

However, it will be correct.

By the way, Kotlin's constant expression method

companion object {
    const val HOGE = "hoge"

I write it like this.

Java Immutable / Mutable / Const / Variable Classification

Classification as language use

- Variable
  - Immutable ..... final
    - Const ....... static final
  - Mutable .......Ordinary variables

It will be.

However, the idea is the same as Kotlin,

- Const
- Variable
  - Immutable
  - Mutable

Since the classification of is correct, I think it is better to be aware of this.

If this classification is in Japanese

-constant...........The same value is always entered (≒ literally named)
  -Immutable.........It doesn't change from the first one. What comes in first will change.
  -variable.........It changes from the first one. When, how many times it changes depends on the implementation.

It will be. I personally find this classification fairly easy to understand and remember.

in conclusion

To be honest, until I learned this, I've heard the term ʻImmutable, but I don't use it. With that feeling, I was conscious of only two types, Const / Variable`. This kind of classification is not included in reference books (I'm sorry if I did), so it was very helpful. The fact that I was taught to use the correct language but forgot it is proof that I haven't learned it yet. Be careful ^ ^

Recommended Posts

About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
Java language from the perspective of Kotlin and C #
I tried to summarize the basics of kotlin and java
[Java] I thought about the merits and uses of "interface"
About fastqc of Biocontainers and Java
About the operation of next () and nextLine ()
About the mechanism of the Web and HTTP
Think about the combination of Servlet and Ajax
About the idea of anonymous classes in Java
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
[Java] Classification memo of compilation error and run-time error
About next () and nextLine () of the Scanner class
[Java / Kotlin] Resize considering the orientation of the image
[Java] Difference between "final variable" and "immutable object"
A memo about the types of Java O/R mappers and how to select them
CI the architecture of Java / Kotlin applications with ArchUnit
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
[Java] About Objects.equals () and Review of String comparisons (== and equals)
I summarized the types and basics of Java exceptions
[Technical memo] About the advantages and disadvantages of Ruby
[Java] [Kotlin] Generically call valueOf and values of Enum
About synchronized and Reentrant Lock (Java & Kotlin implementation example)
mutable and immutable objects
[Note] Java Output of the sum of odd and even elements
I didn't understand the behavior of Java Scanner and .nextLine ().
[Java] Set structure of collection class (about HashSet and TreeSet)
Command to check the number and status of Java threads
Java beginners briefly summarized the behavior of Array and ArrayList
[Ruby] Creating code using the concept of classes and instances
About the operation of Java reading party BOF-held monthly since 1998
Traps brought about by the default implementation of the Java 8 interface
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Think about the differences between functions and methods (in Java)
About the handling of Null
About an instance of java
About the description of Docker-compose.yml
About Java variable declaration statements
[Java] About String and StringBuilder
About the same and equivalent
Advantages and disadvantages of Java
About Java Packages and imports
Let's talk about the passing experience of Java SE 8 Programmer II
[Java] Various summaries attached to the heads of classes and members
About miscellaneous impressions of "Testing Java Microservices" and Consumer Driven Contract
[Java] Get the dates of the past Monday and Sunday in order
I want to return to the previous screen with kotlin and java!
The design concept of Java's Date and Time API is interesting
A story about hitting the League Of Legends API with JAVA
About the relationship between the Java String equality operator (==) and initialization. Beginners
Read the first 4 bytes of the Java class file and output CAFEBABE
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder