The Set Interface in Java

The Set Interface in Java

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Initialization A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. Set have its implementation in various classes like HashSet, TreeSet, LinkedHashSet. For example: HashSet: Set set = new HashSet(); Here … 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

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

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