Found 338 Articles for Java Programming

What is static blank final variable in Java?

Syed Javed
Updated on 30-Jul-2019 22:30:22

345 Views

No. It is not allowed in Java. Compiler will fail the compilation throwing error that the blank final field may not have been initialized.

What is blank or uninitialized final variable in Java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

197 Views

No. It is not allowed in Java. The compiler will fail the compilation throwing error that the blank final field may not have been initialized.

What are the 6 ways to use this keyword in Java?

Priya Pallavi
Updated on 18-Jun-2020 07:53:10

2K+ Views

this can be used to get the current object.this can be used to invoke current object's method.this() can be used to invoke current class constructorthis can be passed as a parameter to a method call.this can be passed as a parameter to a constructor.this can be used to return the current object from the method.

Can we create a program without a main method in Java?

Syed Javed
Updated on 30-Jul-2019 22:30:22

2K+ Views

No. For Java based application, JVM will complain about missing main method.

Java constructor return a value but, what?

Priya Pallavi
Updated on 04-Feb-2020 10:54:52

749 Views

No. Java constructor cannot return a value. If required, just create a method which calls the required constructor and returns the required value. See the example below.public class Tester {    public Tester(){}    public static Tester getInstance(){       Tester tester = new Tester();        return tester;    } }

Java program to subtract two matrices.

Syed Javed
Updated on 05-Mar-2020 12:37:40

652 Views

Following is the required program.ExampleLive Demopublic class Tester {    public static void main(String args[]) {       //matrix 1       int a[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 3, 4, 5 } };       //matrix 2       int b[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 1, 2, 4 } };       //result matrix       int c[][] = new int[3][3]; // 3 rows and 3 columns       // subtract and print matrix       for (int i = 0; i < 3; i++) {          for (int j = 0; j < 3; j++) {             c[i][j] = a[i][j] - b[i][j];             System.out.print(c[i][j] + " ");          }          System.out.println();       }    } }Output0 0 0 0 0 0 2 2 1

Find the 3rd smallest number in a Java array.

Kumar Varma
Updated on 12-Mar-2020 10:10:40

4K+ Views

ExampleFollowing is the required program.Live Demopublic class Tester {    public static int getThirdSmallest(int[] a) {       int temp;       //sort the array       for (int i = 0; i < a.length; i++) {          for (int j = i + 1; j < a.length; j++) {             if (a[i] > a[j]) {                temp = a[i];                a[i] = a[j];                a[j] = temp;             }          }       }       //return third smallest element       return a[2];    }    public static void main(String args[]) {       int a[] = { 11,10,4, 15, 16, 13, 2 };       System.out.println("Third Smallest: " +getThirdSmallest(a));    } }OutputThird smallest: 10

Factorial program in Java without using recursion.

Vikyath Ram
Updated on 18-Jun-2020 08:09:48

1K+ Views

Following is the required program.ExampleLive Demopublic 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

Prime number program in Java.

V Jyothi
Updated on 18-Jun-2020 07:00:54

2K+ Views

Following is the required program.ExampleLive Demopublic 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

Fibonacci series program in Java using recursion.

V Jyothi
Updated on 05-Mar-2020 12:16:09

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int n1 = 0, n2 = 1, n3 = 0;    static void fibbonacci(int count) {       if (count > 0) {          n3 = n1 + n2;          n1 = n2;          n2 = n3;          System.out.print(" " + n3);          fibbonacci(count - 1);       }    }    public static void main(String args[]) {       int count = 5;       System.out.print(n1 + " " + n2);       fibbonacci(count - 2);    } }Output0 1 1 2 3

Advertisements