Queues and Deques Interfaces in Java with Examples

Queues and Deques Interfaces in Java

The current Deques interface in java. Utility Package is a Queue interface subtype. The Deque is related to the double-ended queue that supports elements from either end of the data structure being added or removed. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/LIFO). The usage of the PriorityQueue PriorityQueue is a … Read more

Bit Manipulation in Java Tutorial

Bit Manipulation in Java Tutorial

Bit Manipulation in Java – Bitwise and Bit Shift operations. Java enables you to manipulate integers on a bit level, which means operating on specific bits, which represent an integer number. In some cases, it can be really handy. Bit Manipulation Checking, setting, clearing, and toggling individual bits. Using long as bit mask Assuming we want to modify bit n of an integer primitive, i (byte, … Read more

Comparing BigIntegers in Java

Comparing BigIntegers in Java

You can compare BigIntegers the same as you compare String or other objects in Java. For example: BigInteger one = BigInteger.valueOf(1); BigInteger two = BigInteger.valueOf(2); if(one.equals(two)){ System.out.println(“Equal”); } else{ System.out.println(“Not Equal”); } Output: Not Equal Note: In general, do not use use the == operator to compare BigIntegers == operator: compares references; i.e. whether two … Read more

NumberFormat in Java

Number Format in Java

Java NumberFormat tutorial shows how to format numbers in Java.  Different countries have different number formats and considering this we can have different formats using Locale of java. Using locale can help in formatting Locale locale = new Locale(“en”, “IN”);NumberFormat numberFormat = NumberFormat.getInstance(locale); using above format you can perform various tasks Format Number numberFormat.format(10000000.99); Number … Read more

BigInteger in Java

BigInteger in Java

The BigInteger class is used for mathematical operations involving large integers with magnitudes too large for primitive data types. For example 100-factorial is 158 digits – much larger than a long can represent. BigInteger provides analogues to all of Java’s primitive integer operators, and all relevant methods from java.lang.Math as well as few other operations. … Read more

Mathematical operations with BigDecimal in Java

Mathematical Operations in Java

This example shows how to perform basic mathematical operations using BigDecimal in Java. 1.Addition BigDecimal a = new BigDecimal(“5”);BigDecimal b = new BigDecimal(“7”);//Equivalent to result = a + bBigDecimal result = a.add(b);System.out.println(result); Result : 12 2.Subtraction BigDecimal a = new BigDecimal(“5”);BigDecimal b = new BigDecimal(“7”);//Equivalent to result = a – bBigDecimal result = a.subtract(b);System.out.println(result); Result … Read more

BigDecimal in Java

Bigdecimal in Java

The BigDecimal in java class provides operations for arithmetic (add, subtract, multiply, divide), scale manipulation, rounding, comparison, hashing, and format conversion. The BigDecimal represents immutable, arbitrary-precision signed decimal numbers. This class shall be used in the necessity of high-precision calculation. Comparing BigDecimals The method compareTo should be used to compare BigDecimals: BigDecimal a = new … Read more

Java LocalTime

Java LocalTime

Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface. Method Output LocalTime.of(13, 12, 11) 13:12:11 LocalTime.MIDNIGHT 00:00 LocalTime.NOON 12:00 LocalTime.now() Current time from system clock LocalTime.MAX The maximum supported local time 23:59:59.999999999 LocalTime.MIN The minimum supported local time 00:00 … Read more

Usage of various classes of Date Time API

Usage of various classes of Date Time API

The following example also has an explanation required for understanding the example within it. import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.TimeZone; public class SomeMethodsExamples { /** * Has the methods of the class {@link LocalDateTime} / public static void checkLocalDateTime() { LocalDateTime localDateTime = … Read more

Dates and Time (java.time.*)

Dates And Time in Java

Calculate Difference between 2 LocalDates Use LocalDate and ChronoUnit: LocalDate d1 = LocalDate.of(2017, 5, 1);LocalDate d2 = LocalDate.of(2017, 5, 18); now, since the method between of the ChronoUnit enumerator takes 2 Temporals as parameters so you can pass without a problem the LocalDate instances long days = ChronoUnit.DAYS.between(d1, d2);System.out.println( days ); Related Article: How to … Read more

Primitive Data Types

Primitive Data Types in Java

The 8 primitive data types byte, short, int, long, char, boolean, float, and double are the types that store most raw numerical data in Java programs. The char primitive data types A char can store a single 16-bit Unicode character. A character literal is enclosed in single quotes char myChar = ‘u’;char myChar2 = ‘5’;char … Read more

Date class in Java

Date Class in Java

The Date class of java. util package implements Serializable, Cloneable, and Comparable interface. It provides constructors and methods to deal with date and time with java. Date(): Creates date object representing the current java date and time. Parameter Explanation No parameter Creates a new Date object using the allocation time (to the nearest millisecond) long date Creates a new Date object with the time set to … Read more

Literals in Java

Literals in Java

Java is an Object-Oriented Program. Literals are a representation of a fixed value by source code. They ‘re explicitly interpreted without any computation in the code. Every primitive form variable can be assigned literal. Java literal is a syntactic element (i.e. something you find in the source code of a Java program) that represents a … Read more

String Tokenizer

String Tokenizer in Java

The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way to break string. The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a pertoken basis. StringTokenizer Split by space import java.util.StringTokenizer; public class Simple{ public static void main(String args[]){ StringTokenizer st … Read more

Strings in Java

Strings in Java

Strings (java.lang.String) are pieces of text stored in your program. Strings are not a primitive data type in Java, however, they are very common in Java programs. In Java, Strings are immutable, meaning that they cannot be changed. (Click here for a more thorough explanation of immutability.) Comparing Strings In order to compare Strings for … Read more

Java Data Types

Data Types in Java

There are two types of java data types: Primitive data types: The primitive java data types include boolean, char, byte, short, int, long, float, and double. Non-primitive java data types: The non-primitive java data types include Classes, Interfaces, and Arrays. The Double Primitive Java Data Type A double is a double-precision 64-bit IEEE 754 floating point number. double example = -7162.37; double myDouble = 974.21; … Read more

String pool and heap storage

String pool and heap storage

String is a sequence of characters, for e.g. “Hello” is a string of 5 characters. In java, string is an immutable object which means it is constant and can cannot be changed once it has been created. Finding a String Within Another String To check whether a particular String a is being contained in a … Read more

Java Language – Case insensitive switch

Case Insensitive Switch in Java

Case insensitive switch Version ≥ Java SE 7 switch itself can not be parameterized to be case insensitive, but if absolutely required, can behave insensitively to the input string by using toLowerCase() or toUpperCase: switch (myString.toLowerCase()) { case “case1” : … break; case “case2” : … break;} Beware Locale might affect how changing cases happen! … Read more