Java Tutorial For Beginners

Operator Description Example + Indicates positive value +a – Negates an expression value (Converts positive number to negative) -a ++ Increments a value by 1 ++a — Decrements a value by 1 –a ! Inverts the value of a boolean type !true gives false %= Assigns the remainder to the left operand when dividing the…

Read More Java operators

Java is a programming language that is widely known for its versatility, power, and many features. It is popular among developers due to its object-oriented design, portability, and strong security. Java’s object-oriented design allows developers to organize their code into modular units, making it easier to build large-scale applications. Java’s portability means that code can…

Read More Java’s Powerful Features: A Comprehensive Guide

Java main method is the entry point of any Java program. It’s the method that the Java Virtual Machine (JVM) calls to execute the program. In this tutorial, we’ll explore what the main method is, how it’s executed, and what the valid Java main method signatures are. We’ll also discuss some best practices for writing…

Read More Java Main Method: A Comprehensive Guide

class Vehicle { int maxSpeed = 250; public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Car extends Vehicle { int maxSpeed = 300; @Override public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Test { public static void main(String[] args) { Vehicle vehicle = new Car(); // upcasting…

Read More Upcasting Vs Downcasting in Java

public class MyCustomException extends RuntimeException { public MyCustomException(String message) { super(message); } } class Test { public static void main(String[] args) { Test test = new Test(); try { test.reserveSeats(8); } catch (MyCustomException e) { System.out.println(“Exception occurred with message: ” + e.getMessage()); } } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw…

Read More Custom Exceptions in Java

throw new Exception(); class Test { public static void main(String[] args) { Test test = new Test(); test.reserveSeats(8); } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw new IllegalArgumentException(“No more than 5 seats can be reserved.”); } else { System.out.println(“Reservation successful!”); } } } Output: Exception in thread “main” java.lang.IllegalArgumentException: No more…

Read More Difference Between throw and throws in Java

void methodName() throws Exception class Test { public static void main(String[] args) { } private void validateStatus(String status) { if (status.equals(“invalid”)) { throw new IOException(“Status of the file is invalid, the stream is closed!”); } } } class Test { public static void main(String[] args) { Test test = new Test(); test.validateStatus(“valid”); // OK test.validateStatus(“invalid”);…

Read More Java throws Keyword

throw new Exception(); class Test { public static void main(String[] args) { reserveSeat(new User(“Steve”, “Johnson”, false, 32)); // OK reserveSeat(new User(“Mike”, “Lawson”, true, 16)); // Exception will occur } private static void reserveSeat(User user) { if (user.getAge() < 18) { throw new IllegalArgumentException(“The user is under 18 years old!”); } } } class User {…

Read More Throw an Exception in Java

try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed } class Test { public static void main(String[] args) { try { System.out.println(20 / 0); // this will throw ArithmeticException } catch (ArithmeticException ae) { System.out.println(“ArithmeticException occurred!”); } finally { System.out.println(“Finally block…”); } } }…

Read More Java finally block

Java try-catch blocks are an essential part of Java programming, used for handling exceptions that might occur during program execution. When used correctly, try-catch blocks can help you write more robust and error-free code. In this tutorial, I will explain what Java try-catch blocks are, how they work, and how to use them effectively. I’ll…

Read More Java Try-Catch Blocks: A Comprehensive Guide

class Test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); StringBuffer stringBuffer = new StringBuffer(“Java”); for (int i = 0; i < 8000000; i++) { stringBuffer.append(“Programming”); } System.out.println(“Time taken by StringBuffer: ” + (System.currentTimeMillis() – startTime) + “ms”); startTime = System.currentTimeMillis(); StringBuilder stringBuilder = new StringBuilder(“Java”); for (int i = 0; i…

Read More Difference between StringBuilder and StringBuffer classes

public int compareTo(Object obj) class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Python”); list.add(“Java”); list.add(“JavaScript”); list.add(“Ruby”); list.add(“Kotlin”); System.out.println(“Before sorting:”); System.out.println(list); // sorting the list Collections.sort(list); System.out.println(“After sorting:”); System.out.println(list); } } Output: Before sorting: [Python, Java, JavaScript, Ruby, Kotlin] After sorting: [Java, JavaScript, Kotlin, Python, Ruby] Sorting the array of…

Read More Comparable in Java