What I researched about Java 7

Overview

To upgrade Java knowledge that stopped at "1.4" I am learning about Java7,8,9. This time, I will only describe what I investigated about the functions added in "7".

What I looked up

Diamond operator

You can now omit generics when instantiating.

List<Map<String, List<String>>> gyunyu = new ArrayList<>();

NIO2 A file system related API has been added.

Read file

try {
  Path path = Paths.get("gyunyu.txt");
  List<String> list = Files.readAllLines(path, Charset.forName("UTF-8"));

  for (String gyunyu : list) {
    System.out.println(gyunyu);
  }
} catch(IOException e) {
  e.printStackTrace();
}

Writing a file

try {
  List<String> list = new ArrayList<>();
  list.add("Lemon milk");

  Path path = Paths.get("gyunyu.txt");
  Files.write(path, list, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
  e.printStackTrace();
}

try-with-resources By using the try-with-resources statement, you no longer have to write theclose ()that was described in accessing the file database. Preventing memory leaks due to forgetting to write, it has become possible to save a little time and effort.

try(BufferedReader reader = new BufferedReader(new FileReader("gyunyu.txt"))) {
			String line = null;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

You can use the try-with-resources statement for any class that implements the java.lang.AutoCloseable or java.io.Closeable interface.

Gyunyu.java


public class Gyunyu implements AutoCloseable {
  public void make() {
    System.out.println("coffee");
  }	
  @Override
  public void close() throws Exception {
    System.out.println("milk!!!");
  }
}
try (Gyunyu gyunyu = new Gyunyu()) {
  gyunyu.make();
} catch (Exception e) {
  e.printStackTrace();
}

Clipboard02.png

close () is being executed without calling it like gyunyu.close ().

Catch multiple exceptions

Up to Java 6, I wrote as follows,

try {	
} catch (IllegalArgumentException e) {		
} catch (NullPointerException e) {
}

From Java7, it can be described collectively as follows.

try {
} catch (IllegalArgumentException | NullPointerException e) {
}

Judgment of String in switch statement

You can now use String in switch statements. Up to "1.4", only ʻint can be used, and from "5" to ʻenum can also be used for judgment. From "7", it is now possible to judge by String. I've always thought that it can be judged by String ...

String s = "coffee";
switch (s) {
  case "ichigo":
    System.out.println("ICHIGO GYUNYU !!!");
    break;
  case "coffee":
    System.out.println("COFFEE GYUNYU !!!");
    break;
}

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 7
What I researched about Java 5
What I researched about Java learning
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 List [Java]
About java var
About Java literals
About Java commands
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?
What I did when I converted java to Kotlin
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
What is Java technology?
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?