← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 1 of 12

Java 8 Features का अवलोकन

Java 8 ने lambdas, streams, और एक नया date system जैसे बड़े upgrades का एक bundle जोड़ा, जैसे आपके phone का एक major update। उन्होंने Java code को छोटा और साफ़ बना दिया।

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

java
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
	}
}

Static Methods in Interfaces

आप interfaces के अंदर उनके abstract और default methods के साथ सीधे static helper methods भी define कर सकते हैं। ये static methods खुद interface से संबंधित हैं, interface के नाम से call किए जाते हैं, और किसी भी implementing class द्वारा override नहीं किए जा सकते।

उदाहरण: Static Methods in Interfaces

java
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
	}
}

Stream and Lambda Synergy

Java 8 ने Streams और Lambdas को साथ introduce किया, और उन्हें आपको collections पर manually लिखे loops के बजाय साफ़, declarative, functional-style pipelines लिखने देने के लिए हाथ-में-हाथ डिज़ाइन किया गया था।

उदाहरण: Stream and Lambda Synergy

java
// 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);
	}
}

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

java
// 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);
	}
}

Base64 Encoding

Java 8 ने java.util में एक native Base64 utility class भी ship किया, जिससे सिर्फ उस एक task के लिए किसी third-party library को लाए बिना Base64 format में data को encode और decode करना सीधा हो गया।

उदाहरण: Base64 Encoding

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. date.plusDays(1) जैसे methods की return value को नज़रअंदाज़ करना, क्योंकि LocalDate immutable है और original वैसा ही रहता है।
  2. किसी static interface method को किसी instance या implementing class के ज़रिए call करना, जैसे car.helper(), जबकि इसे Vehicle.helper() के रूप में call करना चाहिए।
  3. एक जैसे default method के साथ दो interfaces implement करना और उसे override न करना, जो एक compile error है क्योंकि Java एक को नहीं चुन सकता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.