Home » Java » Core Java (page 40)

Core Java

Prefer System.lineSeparator() for Writing System-Dependent Line Separator Strings in Java

JDK 7 introduced a new method on the java.lang.System class called lineSeparator(). This method does not expect any arguments and returns a String that represents “the system-dependent line separator string.” The Javadoc documentation for this method also states that System.lineSeparator() “always returns the same value – the initial value of the system property line.separator.” It further explains, “On UNIX systems, ...

Read More »

Solid Principles: Dependency inversion principle

Up until now we had a look on the single responsibility, open/closed, liskov substitution and interface segregation principles. Dependency Inversion is one of the last principle we are gone look at. The principle states that A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on ...

Read More »

Solid Principles: Interface segregation principle

Previously we examined the liskov substitution principle. Next principle is the interface-segregation. The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. Imagine an interface with many methods in our codebase and many of our classes implement this interface although only some of its methods are implemented. In our case the ...

Read More »

Solid Principles: Liskov substitution principle

Previously we took a dive into solid principles including the single responsibility and the open/closed principle. The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping, Supposing object S is a subtype of object T, then objects of type T may be replaced with objects of type S without altering any of the ...

Read More »

Solid Principles: Open/closed principle

Previously we talked about the single responsibility principle. The open/closed principle is the second principle in the row regarding the solid principles acronym. “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” By employing that principle the goal is to extend a module’s behaviour without modifying its source code. Imagine a scenario of applying ...

Read More »

Solid Principles: Single responsibility principle

The single responsibility principle is the first principle from the solid acronym. “A class should have only one reason to change.” Every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. For example imagine the scenario of a navigation software. We have ...

Read More »

Java 9: Enhancements to the Process API

Java 9 brings various improvements to the Process API, used for controlling and managing operating system processes. Getting information about a process There is a new ProcessHandle class which provides the process’s pid, parent and descendants, as well as information about the start time and accumulated CPU time. jshell> Process p = new ProcessBuilder("stress", "--cpu", "4", "--timeout", "5").start(); p ==> ...

Read More »

Async await in Java

Writing asynchronous code is hard. Trying to understand what asynchronous code is supposed to be doing is even harder. Promises are a common way to attempt to describe the flow of delayed-execution: first do a thing, then do another thing, in case of error do something else. In many languages promises have become the de facto way to orchestrate asynchronous ...

Read More »