Java Assignment Generics Lambda NIO
Java Assignment Generics Lambda NIO
1. Introduction
Java is a widely-used object-oriented programming language that offers powerful features to simplify
code development and make applications more flexible and robust. Among its many features,
Generics, Lambda Expressions, and the New Input/Output (NIO) package play an essential role in
This assignment explores the concepts, syntax, and applications of Generics, Lambda Expressions,
and NIO classes in Java, along with examples to clarify their use in real-world programming.
2. Generics in Java
Generics were introduced in Java 5 as a way to enable type-safe programming. They allow you to
write a single class, interface, or method that can work with different types of data, while ensuring
- Code Reusability: One generic class or method can handle different data types.
class Box<T> {
private T value;
System.out.println(element);
private T number;
Lambda expressions were introduced in Java 8 to provide functional programming capabilities. They
Example:
@FunctionalInterface
interface MathOperation {
Java NIO (New Input/Output), introduced in Java 1.4 and enhanced in Java 7 (NIO.2), provides a
buffer.put("Hello NIO".getBytes());
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
buffer.clear();
bytesRead = channel.read(buffer);
file.close();
4.5 NIO.2 (Path, Files, and DirectoryStream)
Reading a File:
lines.forEach(System.out::println);
Writing to a File:
-----------------|------------------|-------------------------|-----------------
6. Conclusion
Generics, Lambda Expressions, and the NIO classes are integral components of modern Java
programming. Generics ensure type safety and code reusability, Lambda Expressions make the
code more expressive and readable, and NIO provides high-performance I/O capabilities.