Java Articles

Page 118 of 450

What are unchecked exceptions in Java?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 3K+ Views

An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.Examplepublic class Unchecked_Demo {    public static void main(String args[]) {       int num[] = {1, 2, 3, 4};       System.out.println(num[5]);    } }OutputException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5    at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

Read More

Can we define an interface inside a Java class?

Sravani S
Sravani S
Updated on 11-Mar-2026 7K+ Views

Yes, you can define an interface inside a class and it is known as a nested interface. You can’t access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.Examplepublic class Sample {    interface myInterface {       void demo();    }    class Inner implements myInterface {       public void demo() {          System.out.println("Welcome to Tutorialspoint");       }    }    public static void main(String args[]) {       Inner obj = ...

Read More

Does Java support default parameter values for a method?

Sravani S
Sravani S
Updated on 11-Mar-2026 3K+ Views

Java does not support the concept of default parameter however, you can achieve this usingMethod overloadingUsing method overloading if you define method with no arguments along with parametrized methods. Then you can call a method with zero arguments.Variable argumentsIn Java methods parameters accept arguments with three dots. These are known as variable arguments. Once you use variable arguments as a parameter method, while calling you can pass as many number of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.Examplepublic class Sample {    void demoMethod(String... args) {     ...

Read More

How to compare two ArrayList for equality in Java?

Srinivas Gorla
Srinivas Gorla
Updated on 11-Mar-2026 14K+ Views

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.Exampleimport java.util.ArrayList; public class ComparingList {    public static void main(String[] args) {       ArrayList list1 = new ArrayList();       list1.add("JavaFx");       list1.add("Java");       list1.add("WebGL");       list1.add("OpenCV");       ArrayList list2 = new ArrayList();       list2.add("JavaFx");       list2.add("Java");       list2.add("WebGL");       list2.add("OpenCV");       System.out.println(list2);       System.out.println(list1.equals(list2));    } }

Read More

How to convert a list collection into a dictionary in Java?

Nikitha N
Nikitha N
Updated on 11-Mar-2026 2K+ Views

Following is an example to convert a list collection into a dictionary in Java.Exampleimport java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; public class CollectionDictionary {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       Dictionary dictionary = new Hashtable();       Hashtable hashTable = new Hashtable();       hashTable.put(1, list.get(0));       hashTable.put(2, list.get(1));       hashTable.put(3, list.get(2));       hashTable.put(4, list.get(3));       System.out.println(hashTable);    } }Output[JavaFx, Java, WebGL, OpenCV] {4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}

Read More

Fibonacci series program in Java without using recursion.

Syed Javed
Syed Javed
Updated on 11-Mar-2026 4K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {        int n1 = 0, n2 = 1, n3, i, max = 5;        System.out.print(n1 + " " + n2);        for (i = 2; i < max; ++i) {           n3 = n1 + n2;           System.out.print(" " + n3);           n1 = n2;           n2 = n3;        }     }  }Output0 1 1 2 3

Read More

Fibonacci series program in Java without using recursion.

Syed Javed
Syed Javed
Updated on 11-Mar-2026 4K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {        int n1 = 0, n2 = 1, n3, i, max = 5;        System.out.print(n1 + " " + n2);        for (i = 2; i < max; ++i) {           n3 = n1 + n2;           System.out.print(" " + n3);           n1 = n2;           n2 = n3;        }     }  }Output0 1 1 2 3

Read More

Prime number program in Java.

V Jyothi
V Jyothi
Updated on 11-Mar-2026 2K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Read More

Prime number program in Java.

V Jyothi
V Jyothi
Updated on 11-Mar-2026 2K+ Views

Following is the required program.Examplepublic class Tester {    public static void main(String args[]) {       int i, m = 0, flag = 0;       int n = 41;// it is the number to be checked       m = n / 2;       if (n == 0 || n == 1) {          System.out.println(n + " not a prime number");       } else {          for (i = 2; i

Read More

Factorial program in Java without using recursion.

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 2K+ Views

Following is the required program.Examplepublic class Tester {    static int factorial(int n) {       if (n == 0)          return 1;       else          return (n * factorial(n - 1));    }    public static void main(String args[]) {       int i, fact = 1;       int number = 5;       fact = factorial(number);       System.out.println(number + "! = " + fact);    } }Output5! = 120

Read More
Showing 1171–1180 of 4,498 articles
« Prev 1 116 117 118 119 120 450 Next »
Advertisements