Java 8 Features का अवलोकन
In this page:
Default Methods in Interfaces
Java 8 आपको default keyword का उपयोग करके किसी interface के अंदर सीधे एक concrete method implementation जोड़ने देता है। यह library authors को मौजूदा interfaces में बिना हर class को तोड़े नए methods से extend करने देता है जो उन्हें पहले से implement करती हैं, क्योंकि implementers अपने आप default behavior inherit कर लेते हैं।
उदाहरण: Default Methods in Interfaces
public class Main {
interface Vehicle {
default void honk() {
System.out.println("Beep beep!");
}
}
static class Car implements Vehicle {}
public static void main(String[] args) {
new Car().honk(); // inherited default behavior
}
}
Login to try C/C++/Java/PHP code in the editor
Static Methods in Interfaces
आप interfaces के अंदर उनके abstract और default methods के साथ सीधे static helper methods भी define कर सकते हैं। ये static methods खुद interface से संबंधित हैं, interface के नाम से call किए जाते हैं, और किसी भी implementing class द्वारा override नहीं किए जा सकते।
उदाहरण: Static Methods in Interfaces
public class Main {
interface MathUtil {
static int square(int n) {
return n * n;
}
}
public static void main(String[] args) {
System.out.println(MathUtil.square(6)); // called through the interface name
}
}
Login to try C/C++/Java/PHP code in the editor
Stream and Lambda Synergy
Java 8 ने Streams और Lambdas को साथ introduce किया, और उन्हें आपको collections पर manually लिखे loops के बजाय साफ़, declarative, functional-style pipelines लिखने देने के लिए हाथ-में-हाथ डिज़ाइन किया गया था।
उदाहरण: Stream and Lambda Synergy
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3, 4, 5);
// Create a stream from the collection to process it step by step
nums.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
}
}
Login to try C/C++/Java/PHP code in the editor
Local Date and Time API
java.time package, जो Java 8 में introduced हुआ, एक modern, immutable, thread-safe date और time API प्रदान करता है। इसे specifically पुरानी java.util.Date और Calendar classes को replace करने के लिए बनाया गया था, जिनकी mutability और confusing month-indexing ने सालों तक सूक्ष्म bugs पैदा किए थे।
उदाहरण: Local Date and Time API
// Import java.time.LocalDate so it can be used by its short name
import java.time.LocalDate;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 1, 15);
LocalDate nextWeek = date.plusDays(7);
// Print a line to the console
System.out.println(date + " -> " + nextWeek);
}
}
Login to try C/C++/Java/PHP code in the editor
Base64 Encoding
Java 8 ने java.util में एक native Base64 utility class भी ship किया, जिससे सिर्फ उस एक task के लिए किसी third-party library को लाए बिना Base64 format में data को encode और decode करना सीधा हो गया।
उदाहरण: Base64 Encoding
// Import java.util.Base64 so it can be used by its short name
import java.util.Base64;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String encoded = Base64.getEncoder().encodeToString("hello".getBytes());
// Create a new String object and store it in decoded
String decoded = new String(Base64.getDecoder().decode(encoded));
// Print a line to the console
System.out.println(encoded + " -> " + decoded);
}
}
Login to try C/C++/Java/PHP code in the editor
date.plusDays(1)जैसे methods की return value को नज़रअंदाज़ करना, क्योंकिLocalDateimmutable है और original वैसा ही रहता है।- किसी static interface method को किसी instance या implementing class के ज़रिए call करना, जैसे
car.helper(), जबकि इसेVehicle.helper()के रूप में call करना चाहिए। - एक जैसे default method के साथ दो interfaces implement करना और उसे override न करना, जो एक compile error है क्योंकि Java एक को नहीं चुन सकता।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: