EASY_JAVA_RU Telegram 1958
🤔 Слышал ли что то про метод intern?

Метод intern() в классе String используется для оптимизации памяти.
Он добавляет строку в String Pool и возвращает её ссылку, если строка уже там есть.

🚩Как работает `intern()`?

Без intern() – строки создаются в Heap (куче)
String s1 = new String("Hello"); // В куче (Heap)
String s2 = new String("Hello");

System.out.println(s1 == s2); // false (разные объекты)


С intern() – строки хранятся в String Pool
String s1 = new String("Hello").intern();
String s2 = new String("Hello").intern();

System.out.println(s1 == s2); // true (одна строка в String Pool)


🚩Что такое `String Pool`?

Это специальная область памяти, где хранятся уникальные строковые литералы.
Все строковые литералы ("Hello") по умолчанию хранятся в String Pool.
String s1 = "Hello"; // В String Pool
String s2 = "Hello"; // Ссылается на тот же объект

System.out.println(s1 == s2); // true


🚩Когда использовать `intern()`?

Когда у вас много одинаковых строк в памяти (например, имена, идентификаторы).
Для экономии памяти, если строки часто дублируются.
В парсинге JSON, XML – одни и те же строки могут повторяться тысячи раз.
List<String> names = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
names.add(("User" + (i % 100)).intern()); // Используем String Pool
}


Ставь 👍 и забирай 📚 Базу знаний
Please open Telegram to view this post
VIEW IN TELEGRAM
👍7



tgoop.com/easy_java_ru/1958
Create:
Last Update:

🤔 Слышал ли что то про метод intern?

Метод intern() в классе String используется для оптимизации памяти.
Он добавляет строку в String Pool и возвращает её ссылку, если строка уже там есть.

🚩Как работает `intern()`?

Без intern() – строки создаются в Heap (куче)

String s1 = new String("Hello"); // В куче (Heap)
String s2 = new String("Hello");

System.out.println(s1 == s2); // false (разные объекты)


С intern() – строки хранятся в String Pool
String s1 = new String("Hello").intern();
String s2 = new String("Hello").intern();

System.out.println(s1 == s2); // true (одна строка в String Pool)


🚩Что такое `String Pool`?

Это специальная область памяти, где хранятся уникальные строковые литералы.
Все строковые литералы ("Hello") по умолчанию хранятся в String Pool.
String s1 = "Hello"; // В String Pool
String s2 = "Hello"; // Ссылается на тот же объект

System.out.println(s1 == s2); // true


🚩Когда использовать `intern()`?

Когда у вас много одинаковых строк в памяти (например, имена, идентификаторы).
Для экономии памяти, если строки часто дублируются.
В парсинге JSON, XML – одни и те же строки могут повторяться тысячи раз.
List<String> names = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
names.add(("User" + (i % 100)).intern()); // Используем String Pool
}


Ставь 👍 и забирай 📚 Базу знаний

BY Java | Вопросы собесов


Share with your friend now:
tgoop.com/easy_java_ru/1958

View MORE
Open in Telegram


Telegram News

Date: |

Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. Select: Settings – Manage Channel – Administrators – Add administrator. From your list of subscribers, select the correct user. A new window will appear on the screen. Check the rights you’re willing to give to your administrator. To edit your name or bio, click the Menu icon and select “Manage Channel.” Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators.
from us


Telegram Java | Вопросы собесов
FROM American