java jshell and var are too recommended!

I recently went to a Java event, so that's a note.

jshell

Who?

Who! It's a tool that allows you to easily execute Java commands. In Ruby, there is "irb".

In Java, start Eclipse and run it? Or compile with javac and run with java? It was a lot of work.

You can check it quickly by using jshell. Like this. It is convenient to be able to store automatically with tabs.

D:\Program Files>jshell
|Welcome to JShell--Version 13
|For an overview, type:: /help intro

jshell> System
Signature:
java.lang.System

<Press the tab again to view the document>
jshell> System.out.p
print(     printf(    println(
jshell> System.out.println("Hello World!!");
Hello World!!

jshell>

What version is the feature from?

By the way, it is a function installed from Java9. I should have known earlier. ..

var

What version is the feature from?

It's a feature from Java 10. I knew it, but I didn't use it by mistake.

What I misunderstood

I thought it was a var like javascript. In short, is it a type that does not have "type guarantee"? ..

When I heard the story at the event, it was different.

In short, you can set "numerical value" to "var variable" with "String" set! I thought, I got an error properly.

jshell> var testVal = "test data";
testVal ==> "test data"

jshell> testVal = 1;
|error:
|Incompatible type:int java.lang.Cannot convert to String:
|  testVal = 1;
|            ^

jshell>

How var was born

This story was the most eye-opening.

When there was no var

jshell> class Customer {
   ...>   void test(){
   ...>     System.out.println("Hello World");
   ...>   }
   ...> }
|Created the following:Class Customer

jshell> Customer customer = new Customer();
customer ==> Customer@5025a98f

jshell>

I wrote it like this and wrote "customer" three times in the form of "Customer customer = new Customer ();". Some people want to see "Customer cu = new Customer ();" and omit the variable name. I would like to stop this because it reduces readability.

So, I thought of "var".

When using var

jshell> var customer = new Customer();
customer ==> Customer@20322d26

jshell>

In the form of "var customer = new Customer ();", I was able to reduce "customer" to 2 times without omitting the variable name! In short, you don't have to omit the variable name and write what you have written repeatedly.

This is the purpose of the person who thought about "var".

It was a scale from my eyes. From now on, we will use "var".

Recommended Posts

java jshell and var are too recommended!
[Java] What are overrides and overloads?
Studying java9 (jShell)
Java and JavaScript
XXE and Java
About java var
[Java] Sorting tips when strings and numbers are mixed
[Java] When var should be used and when it should not be used
[Java10] Be careful of using var and generics together
[Java Silver] What are class variables instance variables and local variables?
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override