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.
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).
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
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!
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.
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.
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;
}
}
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