I've been working mainly on Scala for about two years, but due to various reasons, I'm developing it in Java for the first time in a few years, but I would like to summarize the functions that I missed Scala.
Scala is developed using Scala 2.12.x
and Java is developed using Java 8
.
Then it will come quickly. (Since it is in the order that I came up with, there is no particular meaning in the order)
When I was writing Scala, I omitted the types of variables and methods that are not exposed to the outside, but Java never allows it. At first, I couldn't grasp the rhythm of writing at all. From Java 10, it can be omitted in certain rules (?). I didn't know Java at all because time had stopped since Java 7.
I was very grateful to go back to Java. Java has a ternary operator, but it was generally prohibited by convention in the projects I was working on.
I wrote it like this in Scala
val mode = if (Conditional expression) "xxx" else "yyy"
Since Java cannot return a value
String mode;
if (Conditional expression) {
mode = "xxx";
} else {
mode = "yyy";
}
Is it like this? I didn't care at all in the past, but it was very unpleasant to leave the culture of reassignment.
You can omit various things in Scala, but the following three are the ones I forgot to write the most after writing Java after a long time.
;
On each line
The last return
of the method
A method with no arguments and no side effects ()
Java cannot be renamed when importing. So it's a shame when using the same class in another package. If I thought the class name was something unpleasant, I couldn't change the name, so I thought it was a painstaking measure and decided to read it.
This is the one. (It's called an interpolator)
val name = "James"
println(s"Hello, $name")
I'm not sure what kind of string will be generated after concatenating strings and wrapping them with formatter, or I'm using format, but one argument isn't enough? Isn't there a lot? I had a lot of troubles such as forgetting the format.
It's called raw string literal, which is a kind of interpolator. I often deal with Json, which is my job, but I can write it as Scala.
val json = """
{
"id": "12345",
"name": "James"
}
"""
In Java ...
String json = "{\n"
+ " \"id\": \"12345\",\n"
+ " \"name\": \"James\"\n"
+ "}";
It's hard. .. .. There is no choice but to write Json in an editor and paste it into Intellij. (It was saved because it escaped automatically when I pasted it with no use)
There were many other things, but I forgot because there were too many side effects. I will continue to work with Java, thinking that I can write like this with Scala.
Recommended Posts