Readers and Writers in Java

Readers and Writers and their respective subclasses provide simple I/O for text / character-based data. BufferedReader Introduction The BufferedReader class is a wrapper for other Reader classes that serves two main purposes: A BufferedReader provides buffering for the wrapped Reader. This allows an application to read charactersmone at a time without undue I/O overheads. A … Read more

Preferences in Java

Using preferences Preferences can be used to store user settings that reflect a user’s personal application settings, e.g. their editor font, whether they prefer the application to be started in full-screen mode, whether they checked a “don’t show this again” checkbox and things like that. public class ExitConfirmer { private static boolean confirmExit() { Preferences … Read more

InputStreams and OutputStreams in Java

Closing Streams Most streams must be closed when you are done with them, otherwise you could introduce a memory leak or leave a file open. It is important that streams are closed even if an exception is thrown. Version ≥ Java SE 7 try(FileWriter fw = new FileWriter(“outfilename”); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out … Read more

Command Line Argument Processing in Java

A Java application can accept any number of command line argument. This allows the user to specify configuration information when the application is launched. When an application is launched, the runtime system passes the command line arguments to the application’s main method via an array of String s. Related Article: The Java Command – ‘java’ and ‘javaw’ Parameter Details … Read more

Java Commands & Options

The java commands supports a wide range of options: All options start with a single hyphen or minus-sign (-): the GNU/Linux convention of using — for “long” options is not supported. Options must appear before the or the -jar argument to be recognized. Any arguments after them will be treated as arguments to be passed … Read more

Collect Elements of a Stream into a Collection in Java

Elements from a Stream can be easily collected into a container by using the Stream.collect operation: Collect with toList() and toSet() Elements from a Stream can be easily collected into a container by using theStream.collect operation: System.out.println(Arrays .asList(“apple”, “banana”, “pear”, “kiwi”, “orange”) .stream() .filter(s -> s.contains(“a”)) .collect(Collectors.toList()) ); // prints: [apple, banana, pear, orange] Other … Read more

Finding Statistics about Numerical Streams in Java

Java 8 provides classes called IntSummaryStatistics, DoubleSummaryStatistics and LongSummaryStatistics which give a state object for collecting statistics such as count, min, max, sum, and average. Version ≥ Java SE 8 List naturalNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); IntSummaryStatistics stats = .mapToInt((x) -> x) .summaryStatistics(); System.out.println(stats); Which will result … Read more

Using Streams and Method References to Write Self-Documenting Processes in Java

Method references make excellent self-documenting code, and using method references with Streams makes complicated processes simple to read and understand. Consider the following code: public interface Ordered { default int getOrder(){ return 0; } } public interface Valued { boolean hasPropertyTwo(); V getValue(); } public interface Thing { boolean hasPropertyOne(); Valued getValuedProperty(); } public List … Read more

Streams in Java

A Streams represents a sequence of elements and supports different kind of operations to perform computations upon those elements. With Java 8, Collection interface has two methods to generate a Stream: stream() andparallelStream(). Stream operations are either intermediate or terminal. Intermediate operations return a Stream so multiple intermediate operations can be chained before the Stream … Read more

Console I/O in Java

The Java Console class is be used to get input from console. It provides methods to read texts and passwords. If you read password using Console class, it will not be displayed to the user. The class is attached with system console internally. Reading user input from the console Using BufferedReader: System.out.println(“Please type your … Read more

Reference Types in Java

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Different Reference Types java.lang.ref package provides reference-object classes, which support a limited degree of interaction with the garbage collector. Java has four main different reference types. They are: Strong Reference Weak Reference Soft Reference Phantom … Read more

Programming to an interfaces in Java

The idea behind programming to an interface is to base the code primarily on interfaces and only use concrete classes at the time of instantiation. In this context, good code dealing with e.g. Java collections will look somethinglike this (not that the method itself is of any use at all, just illustration): public Set toSet(Collection … Read more

The java.util.Objects Class & Default Methods in Java

Default Method introduced in Java 8, allows developers to add new methods to an interface without breaking the existing implementations of this interface. It provides flexibility to allow the interface to define an implementation which will be used as default when a class which implements that interface fails to provide an implementation ofthat method. Basic … Read more

Local Inner Classe in Java

Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. … These classes have access to the fields of the class enclosing it. Local inner classes are must be instantiated in the block they are defined in. Basic Object Construction and Use in Java Objects come Basic Object Construction and Use in Java in … Read more

Generics in Java

Generics are a facility of generic programming that extend Java’s type system to allow a type or method to operate on objects of various types while providing compile-time type safety. In particular, the Java collections framework supports generics to specify the type of objects stored in a collection instance. Related Article: Visibility (controlling access to … Read more

Visibility (controlling access to members of a class)

Private Visibility private visibility allows a variable to only be accessed by its class. They are often used in conjunction with public getters and setters. class SomeClass { private int variable; public int getVariable() { return variable; } public void setVariable(int variable) { this.variable = variable; } } public class SomeOtherClass { public static void … Read more