0% found this document useful (0 votes)
0 views

JavaNewFeaturesGroup3

The presentation covers new features in Java, including Try-with-resources, Type Annotations, and the Java Module System, among others. It highlights improvements such as diamond syntax for type inference, switch expressions, text blocks, records, and sealed classes. Each feature is explained with examples to demonstrate their usage and benefits.

Uploaded by

nikku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

JavaNewFeaturesGroup3

The presentation covers new features in Java, including Try-with-resources, Type Annotations, and the Java Module System, among others. It highlights improvements such as diamond syntax for type inference, switch expressions, text blocks, records, and sealed classes. Each feature is explained with examples to demonstrate their usage and benefits.

Uploaded by

nikku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

A presentation on:

JAVA NEW
FEATURES
Made by: GROUP 3
Akshat Pandey(2300430100006) Rishabh Pandey(2300430100052)
Prasoon Tiwari(2300430100045) Sarthak Kesarwani(2300430100058)
Pushpendra(2300430100049)
TOPICS DISCUSSED:
01 02 03
Try-with-resources Type Annotations and Java Module System
Repeating Annotations
page: 3 page: 4-5 page: 6-7

04 05 06
Diamond Syntax, Local Switch Expressions Text Blocks, Records,
Variable Type and Yield Keyword Sealed Classes
Inference
page: 8-9 page: 10 page: 11-12
TRY-WITH-RESOURCES
Try-with-resources is a feature Example:
introduced in Java 7 that allows try (ResourceType resource
you to automatically close = new ResourceType()) {
resources (like files, sockets, or // Use the resource
database connections) after } catch (Exception e) {
e.printStackTrace();
usage. It works with any class
}
that implements the // The resource is
AutoCloseable or Closeable automatically closed after
interface. It explicits the need try-block execution
for finally blocks to close
resources.
TYPE ANNOTATIONS
Type Annotations allow annotations to be used anywhere a type is used. They
provide better code analysis, error detection, and runtime checks.
You can use these in the following ways:

1. Local variables & Fields: 3.Method parameters

@NonNull String name = "Java"; public void setData(@NonNull


String data) { }
2.Method return type
4.On throws clause
public @NonNull String getData() {
return "Hello"; public void readFile() throws
} @NonNull IOException { }
REPEATING ANNOTATIONS
Example:
Java 8 allows annotations to be
repeated on the same element. @Schedule(day = "Monday")
Useful when applying multiple @Schedule(day = "Friday")
constraints, scheduling tasks, or public void meeting() {}
defining metadata.
To create a repeating annotation, // Requires @Repeatable
you need a container annotation annotation on Schedule
(which holds multiple annotations) definition
and an annotation marked with
@Repeatable.
JAVA MODULE SYSTEM
A module is a self-contained unit of code module-info.java (Module
that consists of module descriptor Descriptor): It defines the module
(module-info.java), packages (exported or name, dependencies and exported
hidden) and dependencies on other packages.
modules. A module has this basic Example:
structure:
module mymodule {
mymodule/
│── src/ exports com.example.
│ ├── mymodule/ utils;
│ │ ├── module-info.java requires java.sql;
<-- Module descriptor // Declares dependency on
│ │ ├── MyClass.java another module
}
CREATING AND USING MODULES
Step 1: Define the Module Step 3: Use the Module in Another
// src/mymodule/module- Module
info.java // src/consumer/module-info.java
module mymodule { module consumer {
exports requires mymodule;
com.example.utils; }

Step 2: Create a Class in the Module


// src/mymodule/com/example/utils/ // src/consumer/com/example/app/
Hello.java Main.java
package com.example.utils; package com.example.app;
import com.example.utils.Hello;
public class Hello { public class Main {
public static void sayHello() { public static void main(String[]
System.out.println("Hello args) {
from mymodule!"); Hello.sayHello();
} }
} }
DIAMOND SYNTAX
Example: The diamond syntax (<>), introduced
in Java 7, allows type inference in
GenericClass<String> obj = new
GenericClass<String>() { generics, reducing redundant code.
void display(String value) { However, it cannot be used with
System.out.println(value); anonymous inner classes due to
} type inference limitations. The
}; compiler cannot infer the type
obj.display("Hello"); parameter when an anonymous
inner class extends a generic class.
//Explicit type To fix this, explicitly specify the
type argument when instantiating
the class.
LOCAL VARIABLE TYPE INFERENCE

Introduced in Java 10, using var for type inference.


Helps in reducing redundancy in variable declarations.
var is not a keyword but a reserved type name.
Works only for local variables (inside methods, loops, or lambdas)
and not for fields, method parameters or return types.
SWITCH EXPRESSIONS
It was introduced in Java 12+ YIELD KEYWORD
to make switch statements Yield is used inside switch
more concise. It returns values expressions to return a value when
directly and supports multiple a case has multiple statements.
cases.
Example:
Example: int value = switch (num) {
case 1 -> {
int result = switch (day) {
System.out.println("One");
case MONDAY, FRIDAY -> 6;
yield 1;
case SUNDAY -> 7;
}
default -> 5;
default -> 0;
};
};
TEXT BLOCKS SEALED CLASSES
It was introduced in Java 13 to A sealed class restricts which other
make multiline strings easier classes can extend it, improving
to write. It reduces the need security and maintainability.
for concatenation and
escape sequences. sealed → Limits which classes can extend it.
permits → Specifies allowed subclasses.
Example:
final → No further subclassing.
String text = """ non-sealed → Allows further subclassing.
This is a text block.
It spans multiple lines.
Syntax:
"""; sealed class Vehicle permits Car, Bike {}
final class Car extends Vehicle {}
non-sealed class Bike extends Vehicle {}
RECORDS
A record is an immutable data Example:
class that auto-generates a
record Person(String name,
constructor, getters, toString(),
int age) {}
equals(), and hashCode().

Person p = new
Key Features:
Person("Alice", 25);
Less boilerplate for data classes
System.out.println(p.name()
Immutable fields (final) ); // Alice
Useful for Data Transfer Objects System.out.println(p);
// Person[name=Alice,
age=25]

You might also like