JAVAPRO_IR Telegram 4469
🎯 Closure در جاوا (Lambda و Capture متغیرهای بیرونی)

وقتی با لامبداها کار می‌کنیم، خیلی وقتا نیاز داریم از متغیرهای بیرونی لامبدا استفاده کنیم. جاوا به این امکان می‌گه Closure.

📌 تعریف ساده:

در واقع Closure یعنی یه تابع (یا همون لامبدا) می‌تونه متغیرهای بیرونی خودش رو بگیره (capture کنه) و حتی بعد از اینکه اون متغیرها از Scope اصلی خارج شدن، همچنان بهشون دسترسی داشته باشه.


مثال:


public class ClosureExample {
public static void main(String[] args) {
String greeting = "Hello";

Runnable r = () -> {
System.out.println(greeting + ", Lambda!");
};

r.run();
}
}


🔎 توضیح:

* متغیر greeting بیرون از لامبدا تعریف شده.
* لامبدا اون رو capture کرده و داخل خودش استفاده می‌کنه.
* وقتی r.run() صدا زده می‌شه، مقدار greeting رو نمایش می‌ده.


⚠️ یک نکته مهم (Effectively Final):

در جاوا، فقط متغیرهایی که final یا effectively final هستن رو می‌شه داخل لامبدا استفاده کرد.
یعنی متغیری که بعد از مقداردهی تغییر داده نشه.

مثال خطا:


public class ClosureError {
public static void main(String[] args) {
int counter = 0;

Runnable r = () -> {
// خطا: چون counter بعداً تغییر داده می‌شه
System.out.println(counter);
};

counter++; // این باعث می‌شه متغیر دیگه effectively final نباشه
}
}


✔️ راه درست:


public class ClosureCorrect {
public static void main(String[] args) {
final int counter = 0;

Runnable r = () -> {
System.out.println(counter);
};

r.run(); // بدون خطا
}
}



🎯 جمع‌بندی:

* لامبداها می‌تونن متغیرهای بیرونی رو Capture کنن → این می‌شه Closure.
* متغیرهایی که capture می‌شن باید final یا effectively final باشن.
* این ویژگی باعث می‌شه بتونیم لامبداهای خیلی قدرتمند و منعطف بنویسیم.

#کاربرـپیشرفته


🆔 @javapro_ir
🆔 @group_javapro
💯3👍2



tgoop.com/javapro_ir/4469
Create:
Last Update:

🎯 Closure در جاوا (Lambda و Capture متغیرهای بیرونی)

وقتی با لامبداها کار می‌کنیم، خیلی وقتا نیاز داریم از متغیرهای بیرونی لامبدا استفاده کنیم. جاوا به این امکان می‌گه Closure.

📌 تعریف ساده:

در واقع Closure یعنی یه تابع (یا همون لامبدا) می‌تونه متغیرهای بیرونی خودش رو بگیره (capture کنه) و حتی بعد از اینکه اون متغیرها از Scope اصلی خارج شدن، همچنان بهشون دسترسی داشته باشه.


مثال:


public class ClosureExample {
public static void main(String[] args) {
String greeting = "Hello";

Runnable r = () -> {
System.out.println(greeting + ", Lambda!");
};

r.run();
}
}


🔎 توضیح:

* متغیر greeting بیرون از لامبدا تعریف شده.
* لامبدا اون رو capture کرده و داخل خودش استفاده می‌کنه.
* وقتی r.run() صدا زده می‌شه، مقدار greeting رو نمایش می‌ده.


⚠️ یک نکته مهم (Effectively Final):

در جاوا، فقط متغیرهایی که final یا effectively final هستن رو می‌شه داخل لامبدا استفاده کرد.
یعنی متغیری که بعد از مقداردهی تغییر داده نشه.

مثال خطا:


public class ClosureError {
public static void main(String[] args) {
int counter = 0;

Runnable r = () -> {
// خطا: چون counter بعداً تغییر داده می‌شه
System.out.println(counter);
};

counter++; // این باعث می‌شه متغیر دیگه effectively final نباشه
}
}


✔️ راه درست:


public class ClosureCorrect {
public static void main(String[] args) {
final int counter = 0;

Runnable r = () -> {
System.out.println(counter);
};

r.run(); // بدون خطا
}
}



🎯 جمع‌بندی:

* لامبداها می‌تونن متغیرهای بیرونی رو Capture کنن → این می‌شه Closure.
* متغیرهایی که capture می‌شن باید final یا effectively final باشن.
* این ویژگی باعث می‌شه بتونیم لامبداهای خیلی قدرتمند و منعطف بنویسیم.

#کاربرـپیشرفته


🆔 @javapro_ir
🆔 @group_javapro

BY برنامه نویسی جاوا | Java


Share with your friend now:
tgoop.com/javapro_ir/4469

View MORE
Open in Telegram


Telegram News

Date: |

The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: Telegram desktop app: In the upper left corner, click the Menu icon (the one with three lines). Select “New Channel” from the drop-down menu. Public channels are public to the internet, regardless of whether or not they are subscribed. A public channel is displayed in search results and has a short address (link). The public channel had more than 109,000 subscribers, Judge Hui said. Ng had the power to remove or amend the messages in the channel, but he “allowed them to exist.” Matt Hussey, editorial director at NEAR Protocol also responded to this news with “#meIRL”. Just as you search “Bear Market Screaming” in Telegram, you will see a Pepe frog yelling as the group’s featured image.
from us


Telegram برنامه نویسی جاوا | Java
FROM American