Home » Java » Core Java » Page 34

Core Java

Nested classes and private methods

When you have a class inside another class they can see each others private methods. It is not well known among Java developers. Many candidates during interviews say that private is a visibility that lets a code see a member if that is in the same class. This is actually true, but it would be more precise to say that ...

Read More »

String.valueOf(Object) versus Objects.toString(Object)

The handy method String.valueOf(Object) has been around since JDK 1.0 and is one of the JDK-provided methods that I use on a frequent basis. I have blogged on its value before. The Objects class was introduced with JDK 1.7 and included the Objects.toString(Object) method. This post compares these two similar-sounding methods. Both methods String.valueOf(Object) and Objects.toString(Object) essentially do the same ...

Read More »

Embracing modular Java platform: Apache CXF on Java 10

It’s been mostly a year since Java 9 release finally delivered Project Jigsaw to the masses. It was a long, long journey, but it is there, so what has changed? This is a very good question and the answer to it is not obvious and straightforward. By and large, Project Jigsaw is a disruptive change and there are many reasons ...

Read More »

Builders and Manipulators

Here is a simple principle for naming methods in OOP, which I’m trying to follow in my code: it’s a verb if it manipulates, it’s a noun if it builds. That’s it. Nothing in between. Methods like saveFile() or getTitle() don’t fit and must be renamed and refactored. Moreover, methods that “manipulate” must always return void, for example print() or ...

Read More »

Apache Commons ArrayUtils.toString(Object) versus JDK Arrays.toString(Object)

Apache Commons Lang provides an ArrayUtils class that includes the method toString(Object) that “Outputs an array as a String.” In this post, I look at situations in which this method might still be useful when the JDK provides the Arrays.toString(Object[]) method [and several overloaded versions of that method on the Arrays class for arrays of primitive types]. At one point, ...

Read More »

JDK 11: Release Candidate Update and OpenJDK JDK 11 LTS

JDK 11 is scheduled to be released for General Availability on Tuesday, 25 September 2018. A 16 August 2018 Mark Reinhold message on the OpenJDK jdk-dev mailing list announced that “JDK 11 is now in the Release Candidate Phase.” However, Reinhold provided updated details in a 17 August 2018 message on that same mailing list in which he stated, “We ...

Read More »

Carefully Specify Multiple Resources in Single try-with-resources Statement

One of the more useful new features of Java 7 was the introduction of the try-with-resources statement [AKA Automatic Resource Management (ARM)]. The attractiveness of the try-with-resources statement lies in its promise to “ensure that each resource is closed at the end of the statement.” A “resource” in this context is any class that implements AutoCloseable and its close() method ...

Read More »

Java Iterator to Java 8 Stream

Sometimes during my work, I need to integrate with other libraries which they return an Iterator object instead of a list. This is fine from the point of view of libraries but it might be a problem when you want to use Java 8 streams on the returned iterator. There is one way to transform the Iterator to Iterable and then ...

Read More »