Java Program to Throw Exception

01. Example: public class throwErrorExample { static void throwErrorMethod(int number) { if (number < 0){ throw new ArithmeticException("There is something wrong."); } else{ System.out.println("Check positive and negative number."); } } …

Read more

Java Program For Exception Handling

01. Example: import java.util.*; class exceptionHandling { public static void main(String[] args) { int x, y, result; Scanner input = new Scanner(System.in); System.out.print("Input two integers: "); x = input.nextInt(); y …

Read more

Java Program To Create Constructor Overloading

01. Example: class constructorOverloading { String name; constructorOverloading() { System.out.println("Hey, I'm simple constructor."); } constructorOverloading(String parameter01) { name = parameter01; System.out.println("Hey, I'm constructor with "+name+"."); } public static void main(String[] …

Read more

Java Program To Create Constructor

01. Example: class createConstructor { //constructor method createConstructor() { System.out.println("Hey, I'm constructor method."); } public static void main(String[] args) { createConstructor object = new createConstructor(); //creating object } } Output: …

Read more

Java Program To Create Multiple Class

01. Example: class firstClass { firstClass() { System.out.println("Constructor of first class."); } void firstClassMethod() { System.out.println("Method of first class."); } public static void main(String[] args) { firstClass firstClassObject = new …

Read more

Java Program To Execute Static Block

01. Example: class staticBlockExecution { public static void main(String[] args) { System.out.println("Main Method Is Executed."); } static { System.out.println("Static Block Is Executed Before Main Method."); } } Output: Static Block …

Read more

Java Program To Replace With New String

01. Example: class StringMethods { public static void main(String args[]) { int N; String string01 = “Learn Java”, newString = “”, stringContact = “”; System.out.println(string01); // Find length of string …

Read more

Java Program To Concatenation Two Strings

01. Example: class StringMethods { public static void main(String args[]) { String string01 = "Learn Java", stringContact = ""; System.out.println(string01); // Concatenating string with another string stringContact = string01.concat(" With …

Read more

Java Program To Find String Length

01. Example: class StringMethods { public static void main(String args[]) { int N; String string01 = “Learn Java”; System.out.println(string01); // Find length of string N = string01.length(); System.out.println(“Number Of Characters: …

Read more