Posts Tagged: "Java"

Java – Loops

Loops in Java are used for repeated execution of a block of code. Common types include for, while, and do-while loops.Example: for (int i = 0; i < 5; i++) { System.out.println(i);}

Java – Functions

Functions are reusable blocks of code that perform specific tasks. They are defined using the public static void syntax.Example: public static void greet() { System.out.println("Hello, World!");}public static void main(String[] args) { greet();}

Java – OOP Concepts

Java is an object-oriented programming language that includes Inheritance, Polymorphism, Encapsulation, and Abstraction.Example: class Animal { void sound() { System.out.println("Animal makes a sound"); }}class Dog extends Animal { void sound() { System.out.println("Dog barks"); }}

Java – Exceptions

Exception Handling in Java is used to manage runtime errors using try, catch, finally, and throw statements.Example: try { int result = 10 / 0;} catch (ArithmeticException e) { System.out.println("Division by zero error!");}

Java – Generics

Generics enable type-safe programming by specifying data types for classes and methods.Example: ArrayList numbers = new ArrayList();numbers.add(10);System.out.println(numbers);

Java – Streams API

The Streams API introduced in Java 8 enables functional-style operations on collections.Example: List names = Arrays.asList("Alice", "Bob", "Charlie");names.stream().filter(name -> name.startsWith("A")) .forEach(System.out::println);