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

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

Classes and Objects in Java

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below: Objects have states and behaviors. Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of … 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