KEMBAR78
Java New Features Unit 3 Program Used in Video (RRSIMT) | PDF | Workweek And Weekend | Anonymous Function
0% found this document useful (0 votes)
17 views8 pages

Java New Features Unit 3 Program Used in Video (RRSIMT)

The document provides an overview of various new features in Java, including nested classes, anonymous classes, lambda expressions, functional interfaces, default and static methods, sealed classes, and Base64 encoding/decoding. It also covers the use of the switch expression, yield keyword, text blocks, and records. Each feature is demonstrated with code examples to illustrate their usage and functionality.

Uploaded by

mahesh2635583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Java New Features Unit 3 Program Used in Video (RRSIMT)

The document provides an overview of various new features in Java, including nested classes, anonymous classes, lambda expressions, functional interfaces, default and static methods, sealed classes, and Base64 encoding/decoding. It also covers the use of the switch expression, yield keyword, text blocks, and records. Each feature is demonstrated with code examples to illustrate their usage and functionality.

Uploaded by

mahesh2635583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

//Java New Features

//Nested class in Java


class A{
static int roll_no;
class B{
public static void display()
{
System.out.println("Roll no : "+roll_no);
}
}
B obj = new B();
}
public class Main {
public static void main(String[] args) {
A obj = new A();
obj.roll_no = 23;
A.B.display();
}
}

class A{
int x ;
void print()
{
System.out.println(x);
}
}
public class Main {
public static void main(String[] args) {
A obj = new A();
// obj.x = 23;
// obj.print();
new A().print();// Anonymous Object, Object Without Reference
}
}

//Anonymous class concept

abstract class Animal {


abstract void bark();
}
class Dog extends Animal{
@Override
public void bark()
{
System.out.println("Dog is Barking....");
}
}
public class Main {
public static void main(String[] args) {
Dog sheru = new Dog();
sheru.bark();
}
}

//Creating a Anounymous class By extending a class


abstract class Animal {
abstract void bark();
}
public class Main {
public static void main(String[] args) {
Animal dog = new Animal() {
@Override // here We Override the bark methods wihout
creating any seperate class this is where anonymous class in used.
void bark() {
System.out.println("Dog is Barking...");
}
};
dog.bark();

}
}

//Creating a Anony. class By implementing a interface


interface Vechicle {
void drive();
}
public class Main {
public static void main(String[] args) {
Vechicle car = new Vechicle() { //Anonymous Class without any name
@Override
public void drive() {
System.out.println("Car is Driving....");
}
};
car.drive();
}
}

//Lambda Expression
@FunctionalInterface
interface Calculator{
void meth1(int a, int b);
}
public class Main {
public static void main(String[] args) {
Calculator obj =(a,b)-> { //Lembda Expression used: ( )-> { }
System.out.println("Sum is : "+(a+b));
};
obj.meth1(2,10);
}
}

//Understanding Functional interface in java 8


@FunctionalInterface
interface myFunctionalInteraface{
void execute();
}
public class Main {
public static void main(String[] args) {
myFunctionalInteraface obj = ()->{
System.out.println("Hello Functional Interface ! ");
};
obj.execute();
}
}
public class Main {
public static void main(String[] args) {

List<Integer>num = Arrays.asList(10,2,1,15,8);

Stream<Integer>data = num.stream(); //Creating a new stream

//Using filter ( )
// Stream<Integer>filterdata = data.filter(n->n%2!=0);//Odd Fetch
// filterdata.forEach(n-> System.out.println(n));

// //Using Map( )
// Stream<Integer>mapdata = data.map(n->n*2);
// mapdata.forEach(n-> System.out.println(n));

//Using sorted( )
// Stream<Integer> sorteddata = data.sorted();
// sorteddata.forEach(n-> System.out.println(n));

//Using count( )
// long count = data.count(); //Throw exception because stream is
already consumed
// System.out.println(count);

}
}

//Default methods in java


interface Vehicle{
void cleanVehicle();
default void startVehicle() //Default methods is here
{
System.out.println("Vehicle is Starting....");
}
}
class Car implements Vehicle{
@Override
public void cleanVehicle ()
{
System.out.println("Car is cleaning now....");
}
public void startVehicle()
{
System.out.println("Car is Starting....");
}
}
class Bike implements Vehicle{
@Override
public void cleanVehicle() {
System.out.println("Vehicle is Starting now....");
}
@Override
public void startVehicle()
{
System.out.println("Bike is Starting....");
}
}
public class Main {
public static void main(String[] args) {
Car obj = new Car();
obj.cleanVehicle();
obj.startVehicle();
}
}

//Static Methods implementation


interface Vehicle{
static void cleanVehicle(){
System.out.println("Vehicles is Cleaning...");
}
}
class Car implements Vehicle{
public void cleanVehicle()
{
System.out.println("Car is Cleaning...");
}
}
public class Main {
public static void main(String[] args){
Vehicle.cleanVehicle();

}
}

//Sealed Classes and Sealed Interface


sealed class Human permits Arman,Ashish{
public void info()
{
System.out.println("I am Human ");
}
}
final class Arman extends Human{
@Override
public void info()
{
System.out.println("I am Arman Ali ");
}
}
final class Ashish extends Human{
@Override
public void info ()
{
System.out.println("I am Ashish Soni ");
}
}
public class Main {
public static void main(String[] args) {
Human h1 = new Arman();
Human h2 = new Ashish();
h1.info();
h2.info();
}

}
import java.util.*;
// Java Base 64 Encoding and Decoding
//Program to Encode the data
public class Main {
static String encoded;
public static void Encode(String Original_data)
{
byte[] array = Original_data.getBytes();
encoded = Base64.getEncoder().encodeToString(array);
System.out.println("Encoded : " + encoded);

}
public static void Decode()
{
//Program to Decode the data
byte [] decodedbyte = Base64.getDecoder().decode(encoded);
String decodedString = new String(decodedbyte);
System.out.println("Decoded : "+decodedString);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter data to Encode :");
String Original_data = sc.nextLine();
Encode(Original_data);
Decode();

}
}

//forEach methods in java


public class Main {
public static void main(String[] args) {
// List<Integer> num = Arrays.asList(10,20,30,50);
// num.forEach(n-> System.out.println(n));
List<String>Fruits = new ArrayList<String>();
Fruits.add("Apple");
Fruits.add("Banana");
Fruits.add("Kiwi");
Fruits.add("Lemon");
Fruits.forEach(fruit-> System.out.println(fruit));
}
}

class KeyPadPhone{
@Deprecated
void sendMessage(){
System.out.println("Text message sent!");
}
}

class AndroidPhone extends KeyPadPhone{


@Override
void sendMessage(){
System.out.println("Message sent via WhatsApp!");
}
}

public class Main{


public static void main(String args[]){
AndroidPhone Samsung = new AndroidPhone();
Samsung.sendMessage();
}
}

//Switch Expression in java


public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the day : ");
String day = sc.nextLine();
int daynumber = switch (day)
{
case "Monday"-> 1;
case "Tuesday"-> 2;
case "Wednesday"-> 3;
case "Thursday"-> 4;
case "Friday"-> 5;
case "Saturday"-> 6;
case "Sunday"-> 7;
default -> 0;
};
System.out.println("Day Number : "+daynumber);
}
}

//Write java code to print name of days of weeks and also print whether the
day is
//weekday or weekend using Switch statement.
public class Main {
public static void main(String[] args) {
String[] days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY"};
for(String day: days)
{
String typeofday = switch (day){
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY"->"WeekDay";
case "SATURDAY", "SUNDAY"->"Weekend";
default -> "Invalid day";
};
System.out.println(day+" is a "+typeofday);
}
}
}

//Yield keyword in java


public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number :");
int a = sc.nextInt();
System.out.println("Enter the second number : ");
int b =sc.nextInt();
int c;
System.out.println("Enter your choice : ");
String choice = sc.next();
int result = switch (choice)
{
case "Add"->{
c= a+b;
yield c;
}
case "Sub"->{
c= a-b;
yield c;
}
default -> {
yield -1;
}
};
System.out.println("Result is : "+result);
}
}

//Text block in java


public class Main {
public static void main(String[] args) {
String s = """

Key Points about the yield Keyword:


Usage in Switch Expressions: yield is used in place of
break to return a value from a case block in a switch expression.
gjhgjfgj""ghjgtjykty'''gjfjk
Returning Values: It allows you to return a value directly
from a case, which becomes the result of the entire switch expression.

Immediate Exit: Once yield is executed, it terminates the


execution of that case block and returns control to the caller.
""";
System.out.println(s);
}
}

public class Main {


public static void main(String[] args) {
System.out.println("Hello %s your salary is %d
".formatted("Arman",25000));
}

//Records in java
class Person {
String name;
int age;
int id;
String address;

public Person(String name,int id, int age, String address){


this.id = id;
this.name = name ;
this.age = age ;
this.address = address;
}
public int getId(){
return this.id;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public String getAddress(){
return this.address;
}
}

public class Main {


public static void main(String[] args) {
Person p = new Person("Arman Ali",23,22,"Lucknow");
System.out.println(p.getAddress());
}
}

You might also like