JAVA_QUIZES Telegram 227
Java: Короткая задача

Задача:
Что выведет программа и почему?


public class Tricky {
public static void main(String[] args) {
System.out.println(getValue());
}

static int getValue() {
try {
return 1;
} finally {
return 2;
}
}
}


Разбор:

Блок finally всегда выполняется, даже если есть return в try.

Когда JVM встречает return 1;, она сохраняет значение для возврата.

Затем выполняет finally.

В finally есть новый return 2; — он перезаписывает сохранённый результат.

👉 Поэтому вернётся 2, а не 1.


Полезно знать:
Такое поведение часто ломает логику при работе с ресурсами - finally может изменить или "затереть" возвращаемое значение.
Лучше не использовать return внутри finally вообще.
👍11🤔5



tgoop.com/java_quizes/227
Create:
Last Update:

Java: Короткая задача

Задача:
Что выведет программа и почему?


public class Tricky {
public static void main(String[] args) {
System.out.println(getValue());
}

static int getValue() {
try {
return 1;
} finally {
return 2;
}
}
}


Разбор:

Блок finally всегда выполняется, даже если есть return в try.

Когда JVM встречает return 1;, она сохраняет значение для возврата.

Затем выполняет finally.

В finally есть новый return 2; — он перезаписывает сохранённый результат.

👉 Поэтому вернётся 2, а не 1.


Полезно знать:
Такое поведение часто ломает логику при работе с ресурсами - finally может изменить или "затереть" возвращаемое значение.
Лучше не использовать return внутри finally вообще.

BY Java tests


Share with your friend now:
tgoop.com/java_quizes/227

View MORE
Open in Telegram


Telegram News

Date: |

During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians. To delete a channel with over 1,000 subscribers, you need to contact user support Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation. In handing down the sentence yesterday, deputy judge Peter Hui Shiu-keung of the district court said that even if Ng did not post the messages, he cannot shirk responsibility as the owner and administrator of such a big group for allowing these messages that incite illegal behaviors to exist.
from us


Telegram Java tests
FROM American