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

Javaassign 4

Uploaded by

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

Javaassign 4

Uploaded by

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

Name:Nayni Singhal

Roll number:2200291530070

1. BASE64 ENCODE AND DECODE

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;

public class Base64Example {

public static void main(String[] args) {

// Original String

String originalString = "Hello, World!";

// Encode

String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());

System.out.println("Encoded String: " + encodedString);

// Decode

byte[] decodedBytes = Base64.getDecoder().decode(encodedString);

String decodedString = new String(decodedBytes);

System.out.println("Decoded String: " + decodedString);

Expected Output:

Encoded String: SGVsbG8sIFdvcmxkIQ==


Decoded String: Hello, World!

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;

public class ForEachExample {

public static void main(String[] args) {

List<String> items = Arrays.asList("apple", "banana", "cherry");

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;

public class TryWithResourcesExample {

public static void main(String[] args) {

try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {

String line;

while ((line = br.readLine()) != null) {

System.out.println(line);

} catch (IOException e) {

e.printStackTrace();

Expected Output: (Content of test.txt)

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 {

public class TypeAnnotationsExample {

public static void main(String[] args) {

@NonNull List<String> list = List.of("apple", "banana", "cherry");

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")

public class RepeatingAnnotationsExample {

public static void main(String[] args) {

Schedule[] schedules =
RepeatingAnnotationsExample.class.getAnnotationsByType(Schedule.class);

for (Schedule schedule : schedules) {

System.out.println("Class is scheduled on: " + schedule.day());

Expected Output:

Class is scheduled on: Monday

Class is scheduled on: Wednesday


Class is scheduled on: Friday

6. JAVA MODULE SYSTEM

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;

public class HelloWorld {

public void sayHello() {

System.out.println("Hello, Module World!");

// Main.java

import com.example.mymodule.HelloWorld;

public class Main {

public static void main(String[] args) {

HelloWorld helloWorld = new HelloWorld();

helloWorld.sayHello();

}
}

Expected Output:

Hello, Module World!

7. DIAMOND SYNTAX WITH INNER ANONYMOUS CLASS

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;

public class DiamondSyntaxExample {

public static void main(String[] args) {

List<String> list = new ArrayList<>() {

add("apple");

add("banana");

add("cherry");

};

list.forEach(System.out::println);

}
Expected Output:

apple

banana

cherry

8. LOCAL VARIABLE TYPE INFERENCE

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

public class LocalVariableTypeInferenceExample {

public static void main(String[] args) {

var message = "Hello, World!";

System.out.println(message);

var numbers = List.of(1, 2, 3, 4, 5);

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

public class SwitchExpressionsExample {

public static void main(String[] args) {

int day = 3;

String dayName = switch (day) {

case 1 -> "Sunday";

case 2 -> "Monday";

case 3 -> "Tuesday";

case 4 -> "Wednesday";

case 5 -> "Thursday";

case 6 -> "Friday";

case 7 -> "Saturday";

default -> "Invalid day";

};

System.out.println("Day name: " + dayName);

Expected Output:

Day name: Tuesday


10. YIELD KEYWORD

definition: the yield keyword is used in switch expressions to return a value from a case block.

java

public class YieldKeywordExample {

public static void main(String[] args) {

int day = 4;

String dayName = switch (day) {

case 1 -> "Sunday";

case 2 -> "Monday";

case 3 -> "Tuesday";

case 4 -> {

yield "Wednesday";

case 5 -> "Thursday";

case 6 -> "Friday";

case 7 -> "Saturday";

default -> "Invalid day";

};

System.out.println("Day name: " + dayName);

Expected Output:

Day name: Wednesday


11. TEXT BLOCKS

definition: text blocks, introduced in java 13, allow you to create multi-line string literals in a more
readable and concise way.

java

public class TextBlocksExample {

public static void main(String[] args) {

String textBlock = """

Hello,

This is a text block

in Java.

""";

System.out.println(textBlock);

Expected Output:

Hello,

This is a text block

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

public record Person(String name, int age) {}


public class RecordsExample {

public static void main(String[] args) {

Person person = new Person("Alice", 30);

System.out.println("Name: " + person.name());

System.out.println("Age: " + person.age());

Expected Output:

Name: Alice

Age: 30

13. SEALED CLASSES

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

public abstract sealed class Shape permits Circle, Rectangle {}

final class Circle extends Shape {

double radius;

Circle(double radius) {

this.radius = radius;

}
final class Rectangle extends Shape {

double length, width;

Rectangle(double length, double width) {

this.length = length;

this.width = width;

public class SealedClassesExample {

public static void main(String[] args) {

Shape shape1 = new Circle(5.0);

Shape shape2 = new Rectangle(4.0, 6.0);

if (shape1 instanceof Circle circle) {

System.out.println("Circle with radius: " + circle.radius);

if (shape2 instanceof Rectangle rectangle) {

System.out.println("Rectangle with length: " + rectangle.length + " and width: " +


rectangle.width);

Expected Output:

```

Circle with

You might also like