Javaassign 4
Javaassign 4
Roll number:2200291530070
definition: base64 is an encoding scheme that converts binary data into an ascii string format using
64 different ascii characters. it is commonly used for encoding data that needs to be stored and
transferred over media that are designed to handle textual data.
java
import java.util.Base64;
// Original String
// Encode
// Decode
Expected Output:
2. FOREACH METHOD
definition: the forEach method is a part of the iterable interface. it provides a way to perform an
action for each element of a collection.
java
import java.util.Arrays;
import java.util.List;
items.forEach(item -> {
System.out.println(item);
});
Expected Output:
apple
banana
cherry
3. TRY-WITH RESOURCES
definition: the try-with-resources statement is a try statement that declares one or more resources. a
resource is an object that must be closed after the program is finished with it.
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
String line;
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
Line 1
Line 2
Line 3
4. TYPE ANNOTATIONS
definition: type annotations are annotations that can be applied wherever a type is used. they
provide metadata about the types in your program.
java
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;
@Target(ElementType.TYPE_USE)
@interface NonNull {
list.forEach(System.out::println);
Expected Output:
apple
banana
cherry
5. REPEATING ANNOTATIONS
definition: repeating annotations allow the same annotation to be applied to a declaration or type
use multiple times.
java
import java.lang.annotation.Repeatable;
@Repeatable(Schedules.class)
@interface Schedule {
String day();
@interface Schedules {
Schedule[] value();
@Schedule(day = "Monday")
@Schedule(day = "Wednesday")
@Schedule(day = "Friday")
Schedule[] schedules =
RepeatingAnnotationsExample.class.getAnnotationsByType(Schedule.class);
Expected Output:
definition: the java module system, introduced in java 9, allows you to group related packages and
resources into a module, enhancing encapsulation and improving maintainability.
java
// module-info.java
module mymodule {
exports com.example.mymodule;
// com/example/mymodule/HelloWorld.java
package com.example.mymodule;
// Main.java
import com.example.mymodule.HelloWorld;
helloWorld.sayHello();
}
}
Expected Output:
definition: the diamond syntax <> simplifies the creation of parameterized types. it can be used with
inner anonymous classes to avoid redundancy in type declarations.
java
import java.util.ArrayList;
import java.util.List;
add("apple");
add("banana");
add("cherry");
};
list.forEach(System.out::println);
}
Expected Output:
apple
banana
cherry
definition: local variable type inference, introduced in java 10, allows the compiler to infer the type
of a local variable from its initializer, using the var keyword.
java
System.out.println(message);
numbers.forEach(System.out::println);
Expected Output:
Hello, World!
4
5
9. SWITCH EXPRESSIONS
definition: switch expressions, introduced in java 12, extend the traditional switch statement to
return a value, making it more concise and expressive.
java
int day = 3;
};
Expected Output:
definition: the yield keyword is used in switch expressions to return a value from a case block.
java
int day = 4;
case 4 -> {
yield "Wednesday";
};
Expected Output:
definition: text blocks, introduced in java 13, allow you to create multi-line string literals in a more
readable and concise way.
java
Hello,
in Java.
""";
System.out.println(textBlock);
Expected Output:
Hello,
in Java.
12. RECORDS
definition: records, introduced in java 14, provide a compact syntax for declaring classes that are
transparent holders for shallowly immutable data.
java
Expected Output:
Name: Alice
Age: 30
definition: sealed classes, introduced in java 15, restrict which other classes or interfaces may extend
or implement them, providing more control over the class hierarchy.
java
double radius;
Circle(double radius) {
this.radius = radius;
}
final class Rectangle extends Shape {
this.length = length;
this.width = width;
Expected Output:
```
Circle with