Java Articles

Page 2 of 450

Why do we need generics in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

Reference typesAs we know a class is a blue print in which we define the required behaviors and properties and, an interface is similar to class but it is a Specification (containing abstract methods).These are also considered as datatypes in Java, unlike other primitive datatypes a literal of these kind of types points/refers to the location of the object. They are also known as reference types.GenericsGenerics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the ...

Read More

Difference Between Daemon Threads and User Threads In Java

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 2K+ Views

As we know java is a language that supports multi threading and on the basis of nature threads in java are classified into two types Daemon thread and User thread.The following are the important differences between Daemon Threads and User Threads.Sr. No.KeyDaemon ThreadsUser Threads1NatureDaemon thread is low in priority i.e JVM does not care much about these types of threads.User threads are recognized as high priority thread i.e. JVM will wait for any active user thread to get completed.2CPU availabilityIt is not guaranteed that Daemon thread always gets CPU usage whenever it requires due to its low priority.User thread always ...

Read More

Matching Nonprintable Characters using Java regex

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

There are 7 common non printable characters used in general and each character has its own hexadecimal representation.NamecharactersHexa-decimal representationbell\a0x07Escape\e0x1BForm feed\f0x0CLine feed0x0ACarriage return\r0X0DHorizontal tab\t0X09Vertical tab\v0X0BExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "\x09";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Matching the compiled pattern in the String       Matcher matcher = ...

Read More

Difference between sum of the squares of and square of sum first n natural numbers.

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 1K+ Views

Problem StatementWith a given number n, write a program to find the difference between sum of the squares of and square of sum first n natural numbers.Examplen = 3 Squares of first three numbers = 3x3 + 2x2 + 1x1 = 9 + 4 + 1 = 14 Squares of sum of first three numbers = (3 + 2 + 1)x(3 + 2 + 1) = 6x6 = 36 Difference = 36 - 14 = 22Examplepublic class JavaTester {    public static int difference(int n){       //sum of squares of n natural numbers       ...

Read More

Difference between sums of odd and even digits.

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 599 Views

Problem StatementWith a given long integer n, write a program to find the difference between sum of the odd digits and even digits to be 0 or not. Index starts from 0.Examplen = 1212112 Sum of odd position elements = 2 + 2 + 1 = 5 Sum of even position elements = 1 + 1 + 1 + 2 = 5 Difference = 5 - 5 = 0 Output = YesExampleclass JavaTester {    public static int difference(int n){       return (n % 11);    }    public static void main(String args[]){       int n = 1212112;       System.out.println("Number: " + n);       System.out.println(difference(n) == 0 ? "Yes" : "No");       n = 12121121;       System.out.println("Number: " + n);       System.out.println(difference(n) == 0 ? "Yes" : "No");    } }OutputNumber : 1212112 Output: Yes Number : 12121121 Output: No

Read More

Difference between sums of odd level and even level nodes of a Binary Tree in Java

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 475 Views

Problem StatementWith a given binary tree, write a program to find the difference between sum of nodes at odd level and even level. Assume root at level 1, left/right child of root at level 2 and so on.Example        5       /   \       2     6      /  \     \     1     4    8    /    /  \   3    7    9 Sum of nodes at odd level = 5 + 1 + 4 + 8 = 18 Sum of ...

Read More

Difference between the largest and the smallest primes in an array in Java

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 997 Views

Problem StatementWith a given array of integers where all elements are less than 1000000. Find the difference between the largest and the smallest primes in an array.ExampleArray: [ 1, 2, 3, 4, 5 ] Largest Prime Number = 5 Smallest Prime Number = 2 Difference = 5 - 3 = 2.SolutionUse Sieve of Eratosthenes approach, which is an efficient way to find out all prime numbers smaller than a given number. Then we will figure out the largest and smallest prime number to get the required difference.Examplepublic class JavaTester {    static int MAX = 1000000;   ...

Read More

Difference between Traits and Abstract Classes in Scala.

Mahesh Parahar
Mahesh Parahar
Updated on 11-Mar-2026 1K+ Views

TraitsTraits are similar to interfaces in Java and are created using trait keyword.Abstract ClassAbstract Class is similar to abstract classes in Java and are created using abstract keyword.Exampletrait SampleTrait {    // Abstract method    def test    // Non-Abstract method    def tutorials() {       println("Traits tutorials")    } } abstract class SampleAbstractClass {    // Abstract method    def test    // Non-abstract meythod    def tutorials() {       println("Abstract Class tutorial")    } } class Tester extends SampleAbstractClass {    def test() {       println("Welcome to Tutorialspoint") ...

Read More

Incremental Java infinite loop

Abhinanda Shri
Abhinanda Shri
Updated on 11-Mar-2026 246 Views

ExampleFollowing is the required program −public class Tester {    public static void main(String args[]) {       int i = 0;       do {          i++;          System.out.println(i);       }while (true);    } }The output will keep printing numbers in sequential order.

Read More

Incremental Java infinite loop

Abhinanda Shri
Abhinanda Shri
Updated on 11-Mar-2026 246 Views

ExampleFollowing is the required program −public class Tester {    public static void main(String args[]) {       int i = 0;       do {          i++;          System.out.println(i);       }while (true);    } }The output will keep printing numbers in sequential order.

Read More
Showing 11–20 of 4,495 articles
« Prev 1 2 3 4 5 450 Next »
Advertisements