New features- Overview: The latest version of Java is Java 23, here are some of the new improvements done in this version. Structured Concurrency, Scoped Values, Enhanced Primitive Type Patterns, and Stream Gatherers. Explanation of each feature:
Structured Concurrency: Aims to simplify multithreaded programming by treating groups of related tasks as a single unit, improving error handling and task management.
Scoped Values: Provides a mechanism for managing values with limited visibility within specific code sections, enhancing performance and robustness.
Enhanced Primitive Type Patterns: Allows for more flexible pattern matching with primitive types within switch statements, making code more concise and readable.
Stream Gatherers: Offers new ways to collect elements from streams with custom logic, giving developers more control over data aggregation.
Components of a Java program A Java program can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what class, object, methods, and instance variables mean.
Note: The JVM components that are mentioned above is just a simple overview for your understanding. If you want to explore more about the JVM architecture then you can visit this link.
Class Loader: how you open a book only when you need to read a chapter. Similarly, the class loader loads classes only when required, instead of loading the entire program at once.
Bytecode Verifier: Think of a security checkpoint at an airport that verifies passengers before they board a plane. Similarly, the bytecode verifier checks Java bytecode before execution to ensure correctness.
JIT Compiler: Imagine a translator converting sentences into another language. If a phrase is used repeatedly, the translator remembers it instead of translating it again and again—this is how JIT optimizes performance.
Programming paradigms in Java A programming paradigm is a style or approach to programming that uses a set of concepts and rules to solve problems. It's a way of thinking about how to structure and write code.
Java supports multiple programming paradigms: 🔹 Object-Oriented (Encapsulation, Inheritance, Polymorphism) 🔹 Procedural (Functions and structured code) 🔹 Functional (Lambda expressions)
// Scanner
import java.util.scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println("You entered: " + age);
}
}
/*
The Scanner class is designed for parsing input from various sources, including the keyboard.
- `Scanner sc = new Scanner(System.in);` creates a scanner object (instanc of the scanner class) to read from standard input.
*/// BufferedReader
// import java.io.* or
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOExectiption;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name:");
String name = reader.readLine();
System.out.println("Hello, " + name);
}
}-
The BufferedReader class is more efficient for reading large amounts of character data.
-
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); creates a BufferedReader that buffers input for efficiency.
-
reader.readLine(); reads an entire line, including spaces, unlike Scanner, which reads up to whitespace.
-
Because BufferedReader deals with lower-level I/O, it requires handling IOException.