Found 9150 Articles for Object Oriented Programming

How to prevent Cloning to break a Singleton Class Pattern in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:23

308 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance of a class. See the example below − Example - Breaking Singleton Live Demo public class Tester{ public static void main(String[] args) throws CloneNotSupportedException { A a = A.getInstance(); ... Read More

Functional Interfaces in Java Programming

Fendadis John
Updated on 30-Jul-2019 22:30:23

472 Views

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package. Given below is the list of interfaces in Java8. Sr.No. Interface & Description 1 BiConsumer Represents an operation that accepts two input arguments, and returns no result. 2 BiFunction Represents a function that accepts two arguments and produces a result. ... Read More

How to create immutable class in Java?

Rishi Raj
Updated on 28-Jan-2025 11:39:52

1K+ Views

An immutable class object's properties cannot be modified after initialization. The state of an immutable object remains constant throughout its lifetime. To achieve immutability, the class is designed such that its fields are initialized only once through a constructor, and no methods are provided to modify those fields. For example: String is an immutable class in Java. We can create an immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. ... Read More

Determine if a String is a legal Java Identifier

karthikeya Boyini
Updated on 26-Jun-2020 12:20:41

878 Views

To determine if a String is a legal Java Identifier, use the Character.isJavaIdentifierPart() and Character.isJavaIdentifierStart() methods.Character.isJavaIdentifierPart()The java.lang.Character.isJavaIdentifierPart() determines if the character (Unicode code point) may be part of a Java identifier as other than the first character.A character may be part of a Java identifier if any of the following are true.it is a letterit is a currency symbol (such as '$')it is a connecting punctuation character (such as '_')it is a digitit is a numeric letter (such as a Roman numeral character)Character.isJavaIdentifierStart()The java.lang.Character.isJavaIdentifierStart() determines if the character (Unicode code point) is permissible as the first character in a Java ... Read More

Java Program to compare two Java char Arrays

Samual Sam
Updated on 26-Jun-2020 12:21:12

386 Views

To compare two Java char arrays, use the Arrays.equals() method.Let us first declare and initialize some char arrays.char[] arr1 = new char[] { 'p', 'q', 'r' }; char[] arr2 = new char[] { 'p', 'r', 's' }; char[] arr3 = new char[] { 'p', 'q', 'r' };Now let us compare any two of the above arrays.Arrays.equals(arr1, arr2));In the same way, work it for other arrays and compare them.The following is an example.Example Live Demoimport java.util.*; public class Demo {    public static void main(String []args) {       char[] arr1 = new char[] { 'p', 'q', 'r' };     ... Read More

Convert string to char array in Java

karthikeya Boyini
Updated on 26-Jun-2020 12:21:40

4K+ Views

The following is our string.String str = "Tutorial";Now, use the toCharArray() method to convert string to char array.char[] ch = str.toCharArray();Now let us see the complete example.Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "Tutorial";       System.out.println("String: "+str);       char[] ch = str.toCharArray();       System.out.println("Character Array...");       for (int i = 0; i < ch.length; i++) {          System.out.print(ch[i]+" ");       }    } }OutputString: Tutorial Character Array... T u t o r i a l

Java Program to determine a Character\'s Unicode Block

Alshifa Hasnain
Updated on 14-Feb-2025 18:59:17

559 Views

In this article, we will learn to represent the Unicode block containing the given character in Java. Unicode provides a standardized way to represent characters from various writing systems across the world. In Java, characters belong to different Unicode Blocks, which help in categorizing them based on language, symbols, and special characters. Understanding Unicode Blocks A Unicode Block is a range of Unicode characters grouped together based on similar properties.For example: Basic Latin (U+0000 to U+007F) contains English letters and symbols. CJK Unified Ideographs (U+4E00 to U+9FFF) contains Chinese, Japanese, and ... Read More

Store unicode in a char variable in Java

karthikeya Boyini
Updated on 26-Jun-2020 12:10:49

1K+ Views

To store Unicode in a char variable, simply create a char variable.char c;Now assign unicode.char c = '\u00AB';The following is the complete example that shows what will get displayed: when Unicode is stored in a char variable and displayed.Example Live Demopublic class Demo {    public static void main(String []args) {       int a = 79;       System.out.println(a);       char b = (char) a;       System.out.println(b);       char c = '\u00AB';       System.out.println(c);    } }Output79 O «

Copy char array to string in Java

Samual Sam
Updated on 26-Jun-2020 12:11:17

15K+ Views

Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.Let us first create a character array.char[] arr = { 'p', 'q', 'r', 's' };The method valueOf() will convert the entire array into a string.String str = String.valueOf(arr);The following is an example.Example Live Demopublic class Demo {    public static void main(String []args) {       char[] arr = { 'p', 'q', 'r', 's' };       String str = String.valueOf(arr); ... Read More

Java Program to convert ASCII code to String

karthikeya Boyini
Updated on 31-May-2024 15:34:30

18K+ Views

To convert ASCII to string, use the toString() method. Using this method will return the associated character.Let’s say we have the following int value, which works as ASCII for us.int asciiVal = 89;Now, use the toString() method.String str = new Character((char) asciiVal).toString();Example Live Demopublic class Demo {    public static void main(String []args) {       int asciiVal = 87;       String str = new Character((char) asciiVal).toString();       System.out.println(str);    } }OutputW

Advertisements