What I researched about Java 5

Overview

My knowledge of Java has almost stopped at "1.4". In order to change the current situation, I will upgrade it little by little.

Currently, the apps that I mainly touched in business are running at "1.5", Originally it was an app made before "1.4", and the old description remains. I will describe what I investigated about the functions added in "1.5" through the version upgrade of this application.

What I looked up

The variable name has map, lst, str, etc., which may make you feel uncomfortable, but Don't worry, it's just written according to your company's naming convention (which is different now).

Generic type (generics)

Since there was a place where I could use the generic type (generics), I modified it as follows.

private Map mapPref = new HashMap();
public String getPrefID(String strName) {
  if (strName == null) {
    return "";
  }
  return mapPref.get(strName) == null ? "" : (String)mapPref.get(strName);
}

The prefecture name (Tokyo, etc.) is stored in the key, and the prefecture ID (01 in Hokkaido, 13 in the case of Tokyo, etc.) is stored in the value.

private Map<String, String> mapPref = new HashMap<String, String>();
public String getPrefID(String strName) {
  if (strName == null) {
    return "";
  }
  return mapPref.get(strName) == null ? "" : mapPref.get(strName);
}

By using generics, you can explicitly see the type of value that goes into a collection such as Map. Also, if you try to enter a value other than the specified type, an error will occur, making it easier to find compilation errors. This time, we use generics for collections, but you can also use generics to implement classes and methods.

Reference: [Generics](https://github.com/travitu/Java-Tips/wiki/%E3%82%B8%E3%82%A7%E3%83%8D%E3%83%AA%E3%82 % AF% E3% 82% B9% EF% BC% 88% E7% B7% 8F% E7% A7% B0% E5% 9E% 8B% EF% BC% 89)

MakerCode.java


public class MakerCode<T> {
  public void show(T[] id) {
    for (T v : id) {
      System.out.println(v);
    }
  }
}

Car.java


public class Car {
  private String[] id = {"TY", "NS", "HN"};
  public String[] getId() {
    return id;
  }
}

Bike.java


public class Bike {
  private Integer[] id = {10, 20, 30}; //Auto boxing
  public Integer[] getId() {
    return id;
  }
}

Main.java


public class Main {
  public static void main(String[] args) {
    Car car = new Car();
    MakerCode<String> carMakerCode = new MakerCode<String>();
    carMakerCode.show(car.getId());

    Bike bike = new Bike();
    MakerCode<Integer> bikeMakerCode = new MakerCode<Integer>();
    bikeMakerCode.show(bike.getId());
  }
}

In addition, it seems that ** wildcards ** can be used, but it's difficult ... I think I need to find out more. Reference: Wildcard

Extended for statement

Since there was a place where I could use the extended for statement, I modified it as follows.

List<String> lstMaker = new ArrayList<String>();
SQLParameters sqlParams = new SQLParameters();
for (int i = 0; i < lstMaker.size(); i++) {
  sqlParams.add(lstMaker.get(i));
}

lstMaker contains the ID assigned to the manufacturer (such as 1 for company A).

List<String> lstMaker = new ArrayList<String>();
SQLParameters sqlParams = new SQLParameters();
for (String id : lstMaker) {
  sqlParams.add(id);
}

The description is simpler than the normal for statement. The amount of coding has been slightly reduced. It looks refreshing!

Auto boxing

It is used in Bike.java described in the above" Generic type (generics) ". For up to "1.4"

private Integer[] id = {new Integer(10), new Integer(20), new Integer(30)};

If you don't write it like this, it will be " type mismatch: cannot convert from int to Integer ", but From "1.5", conversion from primitive type to wrapper class, conversion from wrapper class to primitive type Is now done automatically.

Variadic

I couldn't find a place that could be used in the app, It can be defined as a variable argument by writing "..." after the variable type.

public void print(String... value) {
  for (String v : value) {
    System.out.println(v);
  }
}
public static void main(String[] args) {
  (new Main()).print("Tokyo", "Kanagawa Prefecture", "Saitama", "Chiba");
}

You can also pass an array.

public static void main(String[] args) {
  String[] pref = {"Hokkaido", "Aomori Prefecture", "Yamagata Prefecture", "Iwate Prefecture"};
  (new Main()).print(pref);
}

Only one variable argument can be specified for each method & only the last argument can be specified.

Enum

A type that can manage multiple constants in one The following description can be combined into one type.

public static final String STATUS_SUCCESS  = "1";
public static final String STATUS_SHORTAGE = "2";
public static final String STATUS_MISMATCH = "3";

--After modification (using enumeration)

public enum Status {
  SUCCESS,
  SHORTAGE,
  MISMATCH,
}

It can have a specific value as shown below.

public enum Status {
  SUCCESS  ("1"),
  SHORTAGE ("2"),
  MISMATCH ("3"),
  ;
  private final String value;
  private Status(final String value) {
    this.value = value;
  }
}

You can also have a method.

public enum Status {
  SUCCESS  ("1"),
  SHORTAGE ("2"),
  MISMATCH ("3"),
  ;
  private final String value;
  private Status(final String value) {
    this.value = value;
  }
  public String getValue() {
    return this.value;
  }
}

Other

There were other added functions, but I'd like to move on to "1.6" once for "1.5". Let's look a little deeper into annotations.

Reference: New and extended features

Recommended Posts

What I researched about Java 8
What I researched about Java 6
What I researched about Java 9
What I researched about Java 5
What I learned about Kotlin
What I learned with Java Gold
What I learned with Java Silver
[Java] About Java 12 features
What is java
[Java] About arrays
Something about java
Where about java
What is Java <>?
[Java] About interface
What is Java
What i learned
About Java class
About Java arrays
About java inheritance
About interface, java interface
What I learned from Java monetary calculation
About java var
About Java literals
What I thought about when I started migrating from Java to Kotlin
Summary of what I learned about Spring Boot
[Java] What should I use for writing files?
What I learned in Java (Part 2) What are variables?
About Java log output
About Java functional interface
About class division (Java)
About [Java] [StreamAPI] allMatch ()
I first touched Java ②
I first touched Java ③
About Java method binding
I first touched Java ④
[Java] About anonymous classes
About method splitting (Java)
What is Java Encapsulation?
What I learned ② ~ Mock ~
About Java Array List
About Java Polymorphism super ()
What I learned ① ~ DJUnit ~
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
I first touched Java
About Java entry points
About Java 10 Docker support
Personal summary about Java
What is Java API-java
[Java] About enum type
All about Java programming
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
About java abstract class
I wrote about Java downcast in an easy-to-understand manner
I will write what I learned about docker anyway (second)
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java