Found 7442 Articles for Java

How to create your own helper class in Java?

Arjun Thakur
Updated on 26-Jun-2020 07:26:54

4K+ Views

A helper class serve the following purposes.Provides common methods which are required by multiple classes in the project.Helper methods are generally public and static so that these can be invoked independently.Each methods of a helper class should work independent of other methods of same class.Following example showcases one such helper class.Examplepublic class Tester {    public static void main(String[] args) {       int a = 37;       int b = 39;       System.out.println(a + " is prime: " + Helper.isPrime(a));       System.out.println(b + " is prime: " + Helper.isPrime(b));    } } ... Read More

How to create the immutable class in Java?

George John
Updated on 26-Jun-2020 07:27:35

358 Views

An immutable class object's properties cannot be modified after initialization. For example String is an immutable class in Java. We can create a immutable class by following the given rules below.Make class final − class should be final so that it cannot be extended.Make each field final − Each field should be final so that they cannot be modified after initialization.Create getter method for each field. − Create a public getter method for each field. fields should be private.No setter method for each field. − Do not create a public setter method for any of the field.Create a parametrized constructor ... Read More

Count the Number of matching characters in a pair of Java string

Chandu yadav
Updated on 26-Jun-2020 07:28:27

2K+ Views

In order to find the count of matching characters in two Java strings the approach is to first create character arrays of both the strings which make comparison simple.After this put each unique character into a Hash map.Compare each character of other string with created hash map whether it is present or not in case if present than put that character into other hash map this is to prevent duplicates.In last get the size of this new created target hash map which is equal to the count of number of matching characters in two given strings.Example Live Demoimport java.util.HashMap; public class ... Read More

CopyOnWriteArrayList Class in Java programming

Ankith Reddy
Updated on 26-Jun-2020 07:29:45

255 Views

Class declarationpublic class CopyOnWriteArrayList extends Object implements List, RandomAccess, Cloneable, SerializableCopyOnWriteArrayList is a thread-safe variant of Arraylist where operations which can change the arraylist (add, update, set methods) creates a clone of the underlying array.CopyOnWriteArrayList is to be used in Thread based environment where read operations are very frequent and update operations are rare.Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.Any type of modification to CopyOnWriteArrayList will not reflect during iteration since the iterator was created.List modification methods like remove, set and add are not supported in iteration. These method will throw UnsupportedOperationException.null can be added to the list.CopyOnWriteArrayList MethodsFollowing is ... Read More

Compare two Strings lexicographically in Java programming

George John
Updated on 26-Jun-2020 07:30:58

399 Views

We can compare two strings lexicographically using following ways in Java.Using String.compareTo(String) method. It compares in case sensitive manner.Using String.compareToIgnoreCase(String) method. It compares in case insensitive manner.Using String.compareTo(Object) method. It compares in case sensitive manner.These methods returns the ascii difference of first odd characters of compared strings.Example Live Demopublic class Tester {    public static void main(String args[]) {       String str = "Hello World";       String anotherString = "hello world";       Object objStr = str;       System.out.println( str.compareTo(anotherString) );       System.out.println( str.compareToIgnoreCase(anotherString) );       System.out.println( str.compareTo(objStr.toString()));    } }Output-32 0 0

overriding method different package in java

Chandu yadav
Updated on 26-Jun-2020 07:32:47

1K+ Views

TestedThe benefit of overriding is ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.In object-oriented terms, overriding means to override the functionality of an existing method.Example Live Democlass Animal {    public void move() {       System.out.println("Animals can move");    } } class Dog extends Animal {    public void move() {       System.out.println("Dogs can walk and run");    } } public class TestDog {    public static void main(String args[]) {       Animal a = new Animal(); // ... Read More

Object Serialization with inheritance in Java

George John
Updated on 25-Jun-2020 14:27:13

3K+ Views

In Serialization when inheritance is introduced then on the basis of superclass and subclass certain cases have been defined which make the understanding of Serialization in each case much simpler. The fundamental rules which should be followed are as below.1. When super class is implements Serializable interface and subclass is not.In this case the object of subclass get serialized by default when superclass get serialize, even if subclass doesn't implements Serializable interface.Examplepublic class TestSerialization {    public static void main(String[] args) throws IOException, ClassNotFoundException {       B obj = new B();       FileOutputStream fos = new ... Read More

Java object creation of Inherited class

Chandu yadav
Updated on 22-Aug-2024 12:12:28

2K+ Views

The constructor is something responsible for the object creation of a particular class in Java. Along with other functions of the constructor, it also instantiates the properties/instances of its class. In Java by default super() keyword is used as the first line of the constructor of every class, here the purpose of this method is to invoke the constructor of its parent class so that the properties of its parent get well instantiated before subclass inherits them and use. The point that should remember here is when you create an object the constructor is called but it is not mandatory that ... Read More

Multithreading in Java

Arjun Thakur
Updated on 25-Jun-2020 14:32:18

686 Views

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing ... Read More

Moving a file from one directory to another using Java

George John
Updated on 25-Jun-2020 14:32:50

6K+ Views

We can use Files.move() API to move file from one directory to another. Following is the syntax of the move method.public static Path move(Path source, Path target, CopyOption... options) throws IOExceptionWheresource − Source path of file to be movedtarget − Target path of file to be movedoptions − options like REPLACE_EXISTING, ATOMIC_MOVEExampleimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Tester {    public static void main(String[] args) {       //move file from D:/temp/test.txt to D:/temp1/test.txt       //make sure that temp1 folder exists       moveFile("D:/temp/test.txt", "D:/temp1/test.txt");    }    private static void moveFile(String ... Read More

Advertisements