SortedMap in Java

The SortedMap in Java. The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key order. Introduction to sorted Map Keypoint: SortedMap interface extends Map. entries are maintained in an ascending key order. Methods of sorted Map : Comparator comparator( ).Object firstKey( ).SortedMap headMap(Object end).Object lastKey( ).SortedMap subMap(Object start, Object end).SortedMap tailMap(Object start). Example: public … Read more

Reference Data Types & Java Compiler – ‘javac’

Java-programming-tutorials

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Dereferencing In Java Dereferencing happens with the . operator: Object obj = new Object();String text = obj.toString(); // ‘obj’ is dereferenced. The Dereferencing follows the memory address stored in a reference, to the place in … Read more

Arrays in Java

Arrays in Java

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the arrays are created. After creation, its length is fixed. Parameter Details ArrayType Type of the array. This can be primitive (int, long, byte) or Objects (String, MyObject, etc). index Index refers to the position of … 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

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

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

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

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

How To Split Strings in Java

Split Strings in Java

The string split() method in Java splits a given string around matches of the given regular expression. Example Java StringTokenizer, and String Split. The StringTokenizer class allows us to break a string into tokens in an application. This class is a legacy class retained for purposes of consistency although its use is discouraged in new … Read more

Substrings in Java

Substrings in Java

A segment of the string is called substring. To put it another way, substring is a subset of another string. StartIndex is inclusive and endIndex is exclusive when substring. Substrings String s = “this is an example”; String a = s.substring(11); // a will hold the string starting at character 11 until the end (“example”) … Read more

StringBuffer and StringBuilder Classes in Java

StringBuffer and StringBuilder Classes in Java

Introduction to Java StringBuffer class. In Java StringBuffer class. StringBuffer is a peer string class that provides much of the string functionality. String represents fixed-length, immutable sequences of characters while StringBuffer represents growable, writable sequences of characters. StringBuffer class Key Points: used to created mutable (modifiable) string. Mutable: Which can be changed. is thread-safe i.e. … Read more

Getting Started with Java Language

Java SE Version Code Name End-of-life (free1) Release Date Java SE 10 (Early Access) None future 2018-03-20 Java SE 9 None future 2017-07-27 Java SE 8 Spider future 2014-03-18 Java SE 7 Dolphin 2015-04-14 2011-07-28 Java SE 6 Mustang 2013-04-16 2006-12-23 Java SE 5 Tiger 2009-11-04 2004-10-04 Java SE 1.4 Merlin prior to 2009-11-04 2002-02-06 … Read more