Category: Java programs

python tutorials and learn python

Created with Sketch.

Static block in Java

  Static block in Java Java programming language offers a block known as static that runs before the execution of the main method. Below is an example to understand its functioning. Later, we see its practical use. Java static block program class StaticBlock { public static void main(String[] args) { System.out.println(“Main method is executed.”); }static…
Read more

Static method in Java with example

  Static method in Java with example Java static methods: we call them without creating an object of the class. Why is the main method static? Because program execution begins from it, and no object exists before calling it. Consider the example below: Static method Java program class Languages { public static void main(String[] args)…
Read more

Using multiple classes in a Java program

  Using multiple classes in a Java program A Java program may contain any number of classes. The following program comprises of two classes: Computer and Laptop, both the classes have their constructors and a method. In the main method, we create objects of two classes and call their methods. Using two classes in Java…
Read more

Java constructor

  Java constructor A Java constructor is a method that is used to initialize an object. It has the same name as that of the class and is called or invoked when we create an object of the class, and we can’t call them explicitly. While creating an object, its attributes may or may not…
Read more

Exception handling in Java

  Exception handling in Java Java exception handling: we learn how to handle exceptions in Java with the help of suitable examples. Exceptions are errors that occur when a program executes. At compile time, syntax and semantics checking is done, and code doesn’t get executed on a machine, so exceptions get caught at run time.…
Read more

Swapping of two numbers in Java

  Swapping of two numbers in Java Java program to swap two numbers with and without using an extra variable. Swapping is frequently used in sorting techniques such as bubble sort, quick sort, and other algorithms. Swapping program in Java import java.util.Scanner; class SwapNumbers { public static void main(String args[]) { int x, y, t;…
Read more

Java program to find the largest of three numbers

  Java program to find the largest of three numbers Java program to find the largest of three numbers, if the numbers are unequal, then “numbers are not distinct” is printed. Comparison operator ‘>’ is used to compare two numbers. To find the largest number out of given numbers you can also use an array.…
Read more

Enhanced for loop in Java

  Enhanced for loop in Java Enhanced for loop in Java is better when scanning a complete array instead of using a for loop. Its syntax is: for (data_type variable: array_name) Here, array_name is the name of an array. Enhanced for loop Java program class EnhancedForLoop { public static void main(String[] args) { int primes[]…
Read more

Factorial Program in Java

  Factorial Program in Java Java program to find factorial of a number, if the number is negative, then an error message is printed. You can also find factorial using recursion. The first program uses integer data type so it can calculate the factorial of small numbers only. Factorial of large numbers using BigInteger. Factorial…
Read more

Java program to print prime numbers

  Java program to print prime numbers Java program to print prime numbers, a user input how many of them are required. Remember, the smallest prime number is 2. We use the sqrt method of Math package, which finds the square root of a number. To check if an integer (say n) is prime, you…
Read more