6 features I missed after returning to Java from Scala

Introduction

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)

Type inference

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.

Returns a value with if etc.

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.

Various can be omitted

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 ()

Type alias

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.

Are you embedding variables in a string

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.

There is no need to escape strings

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)

Finally

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

6 features I missed after returning to Java from Scala
New features from Java7 to Java8
Run R from Java I want to run rJava
I want to write quickly from java to sqlite
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
[Java] I want to calculate the difference from the date
How to write Scala from the perspective of Java
Migration from Cobol to JAVA
I went to Scala Fukuoka 2019!
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
[JDBC] I tried to access the SQLite3 database from Java.
I tried to interact with Java
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize Java learning (1)
The road from JavaScript to Java
Call TensorFlow Java API from Scala
I tried to summarize Java 8 now
[Java] Conversion from array to List
Introduction to Scala from a Java perspective, decompiled and understood (basic)
What I thought about when I started migrating from Java to Kotlin
Introduction to Scala from a Java perspective (Class-Tuple edition) to decompile and understand
I translated the grammar of R and Java [Updated from time to time]
I tried to summarize Java lambda expressions
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
I want to stop Java updates altogether
Introduction to monitoring from Java Touching Prometheus
I tried to build Ruby 3.0.0 from source
Precautions when migrating from VB6.0 to JAVA
I went to the Java Women's Club # 1
[* Java *] I went to JJUG CCC 2017 Fall
What I learned from Java monetary calculation
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Summary of points I was worried about when migrating from java to kotlin
Java features
Java features
I want you to use Scala as Better Java for the time being
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
I tried to make Basic authentication with Java
Use native libraries from Scala via Java CPP + Java
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to send an email in Java.
Connect to Aurora (MySQL) from a Java application
java I tried to break a simple block
To become a VB.net programmer from a Java shop
Migrate from Java to Server Side Kotlin + Spring-boot
I tried to implement deep learning in Java
I want to use java8 forEach with index
How to get Class from Element in Java