Alternative Collections in Java Tutorial

Multimap in Guava, Apache and Eclipse Collections This multimap allows duplicate key-value pairs. JDK analogs are HashMap, HashMap and so on. Examples using Multimap Task: Parse “Hello World! Hello All! Hi World!” string to separate words and print all indexes of every word using MultiMap (for example, Hello=[0, 2], World!=[1, 5] and so on) MultiValueMap … Read more

Documenting Java Code Tutorial

Documentation for java code is often generated using Javadoc. Javadoc was created by Sun Microsystems for the purpose of generating API documentation in HTML format from Java source code. Using the HTML format gives the convenience of being able to hyperlink related documents together. Building Javadocs From the Command Line Many IDEs provide support for … Read more

Regular Expressions in Java Tutorial with Examples

A regular expression is a special sequence of characters that helps in matching or finding other strings or sets of strings, using a specialized syntax held in a pattern. Java has support for regular expression usage through the java.util.regex package. This topic is to introduce and help developers understand more with examples on howRegular Expressions … Read more

Interfaces In Java Tutorial

Java Interfaces Tutorial – An interface is a reference type, similar to a class, which can be declared by using the interface keyword. Interfaces can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Like abstract classes, Interfaces cannot be instantiated—they can … Read more

Scanner In Java Tutorial

In this Java Scanner Tutorial, we will learn about Java Scanner and its methods with the help of examples. Parameter Details Source Source could be either one of String, File or any kind of InputStream General Pattern that does most commonly asked about tasks The following is how to properly use the java.util.Scanner class to … Read more

New File I/O in Java with Examples

Creating paths The Path class is used to programmatically represent a path in the file system (and can therefore point to files as well as directories, even to non-existent ones) A path can be obtained using the helper class Paths: Path p1 = Paths.get(“/var/www”);Path p2 = Paths.get(URI.create(“file:///home/testuser/File.txt”));Path p3 = Paths.get(“C:\Users\DentAr\Documents\HHGTDG.odt”);Path p4 = Paths.get(“/home”, “arthur”, “files”, … Read more

Basic Control Structures & Switch Statements in Java

The switch statement is Java’s multi-way branch statement. It is used to take the place of long if-else if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each valuemust be concretely defined. Switch statements There are three critical components to the switch statement: case: This is the value … Read more

Lambda Expressions in Java

Lambda expressions provide a clear and concise way of implementing a single-method interface using an expression. They allow you to reduce the amount of code you have to create and maintain. While similar toanonymous classes, they have no type information by themselves. Type inference needs to happen. Method references implement functional interfaces using existing methods … Read more

The Java Exception Hierarchy – Unchecked and Checked Exceptions

All Java exceptions are instances of classes in the Exception class hierarchy. This can be represented as follows: java.lang.Throwable – This is the base class for all exception classes. Its methods and constructors implement a range of functionality common to all exceptions. java.lang.Exception – This is the superclass of all normal exceptions. various standard and … Read more

Exceptions and Exception Handling in Java Tutorial

The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. Objects of type Throwable and its subtypes can be sent up the stack with the throw keyword and caught with try…catch statements. Catching an exception with try-catch An exception can be caught and … Read more

Using the Static Keyword In Java Tutorial

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. Reference to non-static member from static context Static variables and methods are not part of an instance, There will always be a single copy of … Read more

Calendar and its Subclasses with Examples in Java

Creating Calendar objects Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar. It’s important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as Calendar.JANUARY to avoid … Read more

Super Keyword in Java

Super keyword use with examples super keyword performs important role in three places Constructor Level Method Level Variable Level Constructor Level super keyword is used to call parent class constructor. This constructor can be default constructor or parameterized constructor. Default constructor : super(); Parameterized constructor : super(int no, double amount, String name); class Parentclass { … Read more