Cloneable interface and clone() method in Java

Cloneable Interface   Cloneable is a marker interface. It does not define any members. This interface indicates that a class implementing it allows clone(bit-wise copy()) of its object to be made.     clone() method   Cloneable interface does not define the clone() method. Implementing Cloneable changes the behavior of the protected Object.clone() method, which […]

Reading a File in Java

This articles shows various ways of reading the following file in Java:   C:/blogs/temp.txt     Read a File using Files.lines(..).forEach(..) in Java 8   In Java 8, we can use Files.lines to read file as Stream.   package com.topjavatutorial.files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class FileReadJava8 {   public […]

Java 8

Interface with default methods vs Abstract class in Java 8

Starting with Java 8, interfaces can define default method implementations. As of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface.   Interface Default method advantage   The constraint on the default method is that it can be implemented only in the terms […]

Write a UTF-8 file with Java using OutputStreamWriter

OutputStreamWriter   An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset like UTF-8.   So, we can create a FileOutputStream and then wrap it in an OutputStreamWriter, which allows us to pass an encoding in the constructor.   Example   package […]

HashMap, ConcurrentHashMap, HashTable and Collections.synchronizedMap differences

Question 1   Differences between HashMap and Hashtable ? (OR) What are the differences between a HashMap and a Hashtable in Java?   Answer   There are several differences between HashMap and Hashtable in Java: – Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform […]