{"id":357367,"date":"2024-05-20T23:49:20","date_gmt":"2024-05-20T23:49:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=357367"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=357367","title":{"rendered":"<span>\u042f \u0445\u043e\u0442\u0435\u043b \u0441\u043b\u043e\u043c\u0430\u0442\u044c Java \u0438 \u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u043c\u0435\u043d\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b \u0440\u0430\u0437\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043b\u0435\u0436\u0430\u0449\u0435\u0433\u043e \u0432 HashSet. \u042f \u0440\u0430\u0437\u0432\u0438\u043b \u0438\u0434\u0435\u044e \u0438 \u043f\u0440\u0438\u0432\u043d\u0451\u0441 <a href=\"https:\/\/www.youtube.com\/watch?v=Zh3Yz3PiXZw\" rel=\"noopener noreferrer nofollow\">\u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443<\/a> \u0432 Java.<\/p>\n<h2>\u041b\u043e\u043c\u0430\u0435\u043c<\/h2>\n<p>\u0412 Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438. \u0414\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 JVM \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0438 \u043a\u0435\u0448\u0438\u0440\u0443\u0435\u0442 Boolean, Byte, Short \u0438 \u0447\u0430\u0441\u0442\u044c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 Integer, \u0447\u0442\u043e\u0431\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432 \u043a\u0435\u0448\u0435.<\/p>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 Integer.java<\/p>\n<pre><code class=\"java\">public final class Integer extends Number         implements Comparable&lt;Integer>, Constable, ConstantDesc {     private final int value;      @IntrinsicCandidate     public static Integer valueOf(int i) {         if (i >= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)             return IntegerCache.cache[i + (-IntegerCache.low)];         return new Integer(i);     } }<\/code><\/pre>\n<p>\u0412 \u043d\u0451\u043c \u043f\u043e\u043b\u0435 value \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a final private. \u0418 \u0435\u0441\u043b\u0438 \u0432\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0435\u0439, \u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432 final \u043e\u043d\u0430 \u0431\u0435\u0441\u0441\u0438\u043b\u044c\u043d\u0430&#8230; \u043d\u043e \u043d\u0435 \u0434\u043b\u044f <s>UB<\/s> Unsafe. \u0417\u0430\u043c\u0435\u043d\u0430 4 \u043d\u0430 22 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430.<\/p>\n<pre><code class=\"java\">Field theUnsafe = Unsafe.class.getDeclaredField(\"theUnsafe\"); theUnsafe.setAccessible(true); Unsafe unsafe = (Unsafe) theUnsafe.get(null);  Field integerValueField = Integer.class.getDeclaredField(\"value\"); long integerValueOffset = unsafe.objectFieldOffset(integerValueField); unsafe.putInt(4, integerValueOffset, 22);  Integer a = 2; Integer b = 2; System.out.println(a + \" + \" + b + \" = \" + (Integer) (a + b));<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0432\u044b\u0432\u0435\u0434\u0435\u0442<\/p>\n<pre><code>2 + 2 = 22<\/code><\/pre>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 11 \u0441\u0442\u0440\u043e\u043a\u0435 \u043a Integer \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0443\u043c\u043c\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432 int \u0438 \u0440\u0430\u0432\u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0451\u043c, \u043f\u0440\u0438 \u0431\u043e\u043a\u0441\u0438\u043d\u0433\u0435 4 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d Integer, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c value \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u043e \u043d\u0430 22. \u0414\u043b\u044f \u0441\u0443\u043c\u043c \u0432\u044b\u0448\u0435 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \u043a\u044d\u0448\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0440\u044e\u043a \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<figure class=\"bordered full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ffd\/df8\/7cc\/ffddf87cc9a20d13b585d6b254e3c1f8.png\" alt=\"\u0422\u0435\u043f\u0435\u0440\u044c \u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443\" title=\"\u0422\u0435\u043f\u0435\u0440\u044c \u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443\" width=\"568\" height=\"576\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ffd\/df8\/7cc\/ffddf87cc9a20d13b585d6b254e3c1f8.png\"\/><\/p>\n<div><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c \u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443<\/figcaption><\/div>\n<\/figure>\n<h2>\u041d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u043b\u0430<\/h2>\n<p>Java \u043a\u0435\u0448\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0438\u0445 \u043f\u043e\u0434\u043c\u0435\u043d\u0430. \u041a\u043b\u0430\u0441\u0441 String \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442. \u041c\u0430\u0441\u0441\u0438\u0432 &#8212; \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u043c\u0435\u043d\u0430 \u0447\u0435\u0440\u0435\u0437 unsafe: <\/p>\n<pre><code class=\"java\">Field stringValueField = String.class.getDeclaredField(\"value\"); long stringValueOffset = unsafe.objectFieldOffset(stringValueField); unsafe.putObject(\"Manchester\", stringValueOffset, \"Liverpool\".getBytes());  System.out.println(\"\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool  \"                    + (\"Manchester\".equals(\"Liverpool\")));<\/code><\/pre>\n<p>\u041d\u0430 \u0431\u0430\u0439\u0442\u044b \u0441\u0442\u0440\u043e\u043a\u0438 &#171;Liverpool&#187; \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0438 &#171;Liverpool&#187; \u0438 &#171;Manchester&#187;. \u041e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool? true<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u043b\u0430\u0434\u0435\u043d\u044c\u043a\u043e\u0435:<\/p>\n<pre><code class=\"java\">Field booleanValueField = Boolean.class.getDeclaredField(\"value\"); long booleanValueOffset = unsafe.objectFieldOffset(booleanValueField); unsafe.putBoolean(Boolean.FALSE, booleanValueOffset, true);  boolean eq = new Boolean(true) == Boolean.TRUE;  System.out.println(\"new Boolean(true) == Boolean.TRUE  \" + eq); System.out.println(\"\u041a\u0430\u043a Boolean  \" + (Boolean) eq); System.out.println(\"TRUE equals FALSE  \" + Boolean.TRUE.equals(false));<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c<\/p>\n<pre><code>new Boolean(true) == Boolean.TRUE  false \u041a\u0430\u043a Boolean  true TRUE equals FALSE  true<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 false<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 == \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0438, \u0430 \u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f equals. \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Boolean \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d Deprecated \u0441 9 \u0432\u0435\u0440\u0441\u0438\u0438, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Boolean.valueOf \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 true.<\/p>\n<\/div>\n<\/details>\n<h2>\u0418\u0442\u043e\u0433<\/h2>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">import java.lang.reflect.Field;  import sun.misc.Unsafe;   public class Main {      public static void main(String[] args) throws Exception {         Field theUnsafe = Unsafe.class.getDeclaredField(\"theUnsafe\");         theUnsafe.setAccessible(true);         Unsafe unsafe = (Unsafe) theUnsafe.get(null);          Field integerValueField = Integer.class.getDeclaredField(\"value\");         long integerValueOffset = unsafe.objectFieldOffset(integerValueField);         unsafe.putInt(4, integerValueOffset, 22);          Integer a = 2;         Integer b = 2;         System.out.println(a + \" + \" + b + \" = \" + (Integer) (a + b));          Field stringValueField = String.class.getDeclaredField(\"value\");         long stringValueOffset = unsafe.objectFieldOffset(stringValueField);         unsafe.putObject(\"Manchester\", stringValueOffset, \"Liverpool\".getBytes());         System.out.println(\"\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool  \"                 + (\"Manchester\".equals(\"Liverpool\")));           Field booleanValueField = Boolean.class.getDeclaredField(\"value\");         long booleanValueOffset = unsafe.objectFieldOffset(booleanValueField);         unsafe.putBoolean(Boolean.FALSE, booleanValueOffset, true);          boolean eq = new Boolean(true) == Boolean.TRUE;          System.out.println(\"new Boolean(true) == Boolean.TRUE  \" + eq);         System.out.println(\"\u041a\u0430\u043a Boolean  \" + (Boolean) eq);         System.out.println(\"TRUE equals FALSE  \" + Boolean.TRUE.equals(false));     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u043e\u0441\u044c \u043d\u0435\u043f\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043d\u043d\u044b\u043c &#8212; \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e, \u043d\u0435 \u043d\u0430\u0440\u0443\u0448\u0430\u0439\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u044b, \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0439\u0442\u0435 Unsafe \u0432 undefined behaviour.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/767114\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">\u0423\u0437\u043d\u0430\u043b\u0438 \u043b\u0438 \u0432\u044b \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043d\u043e\u0432\u043e\u0435?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">49.34% <\/span><span class=\"tm-article-poll__answer-label\">\u0423\u0437\u043d\u0430\u043b \u043f\u0440\u043e Unsafe<\/span><span class=\"tm-article-poll__answer-votes\">75<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 49.34%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">27.63% <\/span><span class=\"tm-article-poll__answer-label\">\u0417\u043d\u0430\u043b \u043f\u0440\u043e Unsafe, \u043d\u0435 \u0437\u043d\u0430\u043b \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c final<\/span><span class=\"tm-article-poll__answer-votes\">42<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 27.63%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">23.03% <\/span><span class=\"tm-article-poll__answer-label\">\u041d\u0438\u0447\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e Unsafe \u0438 \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/span><span class=\"tm-article-poll__answer-votes\">35<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 23.03%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 152 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.    \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0438\u0441\u044c 24 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/767114\/\"> https:\/\/habr.com\/ru\/articles\/767114\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u043c\u0435\u043d\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b \u0440\u0430\u0437\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043b\u0435\u0436\u0430\u0449\u0435\u0433\u043e \u0432 HashSet. \u042f \u0440\u0430\u0437\u0432\u0438\u043b \u0438\u0434\u0435\u044e \u0438 \u043f\u0440\u0438\u0432\u043d\u0451\u0441 <a href=\"https:\/\/www.youtube.com\/watch?v=Zh3Yz3PiXZw\" rel=\"noopener noreferrer nofollow\">\u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443<\/a> \u0432 Java.<\/p>\n<h2>\u041b\u043e\u043c\u0430\u0435\u043c<\/h2>\n<p>\u0412 Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438. \u0414\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 JVM \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0438 \u043a\u0435\u0448\u0438\u0440\u0443\u0435\u0442 Boolean, Byte, Short \u0438 \u0447\u0430\u0441\u0442\u044c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 Integer, \u0447\u0442\u043e\u0431\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432 \u043a\u0435\u0448\u0435.<\/p>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 Integer.java<\/p>\n<pre><code class=\"java\">public final class Integer extends Number         implements Comparable&lt;Integer>, Constable, ConstantDesc {     private final int value;      @IntrinsicCandidate     public static Integer valueOf(int i) {         if (i >= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)             return IntegerCache.cache[i + (-IntegerCache.low)];         return new Integer(i);     } }<\/code><\/pre>\n<p>\u0412 \u043d\u0451\u043c \u043f\u043e\u043b\u0435 value \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a final private. \u0418 \u0435\u0441\u043b\u0438 \u0432\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0435\u0439, \u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432 final \u043e\u043d\u0430 \u0431\u0435\u0441\u0441\u0438\u043b\u044c\u043d\u0430&#8230; \u043d\u043e \u043d\u0435 \u0434\u043b\u044f <s>UB<\/s> Unsafe. \u0417\u0430\u043c\u0435\u043d\u0430 4 \u043d\u0430 22 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430.<\/p>\n<pre><code class=\"java\">Field theUnsafe = Unsafe.class.getDeclaredField(\"theUnsafe\"); theUnsafe.setAccessible(true); Unsafe unsafe = (Unsafe) theUnsafe.get(null);  Field integerValueField = Integer.class.getDeclaredField(\"value\"); long integerValueOffset = unsafe.objectFieldOffset(integerValueField); unsafe.putInt(4, integerValueOffset, 22);  Integer a = 2; Integer b = 2; System.out.println(a + \" + \" + b + \" = \" + (Integer) (a + b));<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0432\u044b\u0432\u0435\u0434\u0435\u0442<\/p>\n<pre><code>2 + 2 = 22<\/code><\/pre>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 11 \u0441\u0442\u0440\u043e\u043a\u0435 \u043a Integer \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0443\u043c\u043c\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432 int \u0438 \u0440\u0430\u0432\u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0451\u043c, \u043f\u0440\u0438 \u0431\u043e\u043a\u0441\u0438\u043d\u0433\u0435 4 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d Integer, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c value \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u043e \u043d\u0430 22. \u0414\u043b\u044f \u0441\u0443\u043c\u043c \u0432\u044b\u0448\u0435 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \u043a\u044d\u0448\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0440\u044e\u043a \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<figure class=\"bordered full-width\">\n<div><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c \u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443<\/figcaption><\/div>\n<\/figure>\n<h2>\u041d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u043b\u0430<\/h2>\n<p>Java \u043a\u0435\u0448\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0438\u0445 \u043f\u043e\u0434\u043c\u0435\u043d\u0430. \u041a\u043b\u0430\u0441\u0441 String \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442. \u041c\u0430\u0441\u0441\u0438\u0432 &#8212; \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u043c\u0435\u043d\u0430 \u0447\u0435\u0440\u0435\u0437 unsafe: <\/p>\n<pre><code class=\"java\">Field stringValueField = String.class.getDeclaredField(\"value\"); long stringValueOffset = unsafe.objectFieldOffset(stringValueField); unsafe.putObject(\"Manchester\", stringValueOffset, \"Liverpool\".getBytes());  System.out.println(\"\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool  \"                    + (\"Manchester\".equals(\"Liverpool\")));<\/code><\/pre>\n<p>\u041d\u0430 \u0431\u0430\u0439\u0442\u044b \u0441\u0442\u0440\u043e\u043a\u0438 &#171;Liverpool&#187; \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0438 &#171;Liverpool&#187; \u0438 &#171;Manchester&#187;. \u041e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool? true<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u043b\u0430\u0434\u0435\u043d\u044c\u043a\u043e\u0435:<\/p>\n<pre><code class=\"java\">Field booleanValueField = Boolean.class.getDeclaredField(\"value\"); long booleanValueOffset = unsafe.objectFieldOffset(booleanValueField); unsafe.putBoolean(Boolean.FALSE, booleanValueOffset, true);  boolean eq = new Boolean(true) == Boolean.TRUE;  System.out.println(\"new Boolean(true) == Boolean.TRUE  \" + eq); System.out.println(\"\u041a\u0430\u043a Boolean  \" + (Boolean) eq); System.out.println(\"TRUE equals FALSE  \" + Boolean.TRUE.equals(false));<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c<\/p>\n<pre><code>new Boolean(true) == Boolean.TRUE  false \u041a\u0430\u043a Boolean  true TRUE equals FALSE  true<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 false<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 == \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0438, \u0430 \u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f equals. \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Boolean \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d Deprecated \u0441 9 \u0432\u0435\u0440\u0441\u0438\u0438, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Boolean.valueOf \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 true.<\/p>\n<\/div>\n<\/details>\n<h2>\u0418\u0442\u043e\u0433<\/h2>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">import java.lang.reflect.Field;  import sun.misc.Unsafe;   public class Main {      public static void main(String[] args) throws Exception {         Field theUnsafe = Unsafe.class.getDeclaredField(\"theUnsafe\");         theUnsafe.setAccessible(true);         Unsafe unsafe = (Unsafe) theUnsafe.get(null);          Field integerValueField = Integer.class.getDeclaredField(\"value\");         long integerValueOffset = unsafe.objectFieldOffset(integerValueField);         unsafe.putInt(4, integerValueOffset, 22);          Integer a = 2;         Integer b = 2;         System.out.println(a + \" + \" + b + \" = \" + (Integer) (a + b));          Field stringValueField = String.class.getDeclaredField(\"value\");         long stringValueOffset = unsafe.objectFieldOffset(stringValueField);         unsafe.putObject(\"Manchester\", stringValueOffset, \"Liverpool\".getBytes());         System.out.println(\"\u041f\u0438\u0448\u0435\u0442\u0441\u044f Manchester, \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f Liverpool  \"                 + (\"Manchester\".equals(\"Liverpool\")));           Field booleanValueField = Boolean.class.getDeclaredField(\"value\");         long booleanValueOffset = unsafe.objectFieldOffset(booleanValueField);         unsafe.putBoolean(Boolean.FALSE, booleanValueOffset, true);          boolean eq = new Boolean(true) == Boolean.TRUE;          System.out.println(\"new Boolean(true) == Boolean.TRUE  \" + eq);         System.out.println(\"\u041a\u0430\u043a Boolean  \" + (Boolean) eq);         System.out.println(\"TRUE equals FALSE  \" + Boolean.TRUE.equals(false));     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u043e\u0441\u044c \u043d\u0435\u043f\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043d\u043d\u044b\u043c &#8212; \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e, \u043d\u0435 \u043d\u0430\u0440\u0443\u0448\u0430\u0439\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u044b, \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0439\u0442\u0435 Unsafe \u0432 undefined behaviour.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/767114\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">\u0423\u0437\u043d\u0430\u043b\u0438 \u043b\u0438 \u0432\u044b \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043d\u043e\u0432\u043e\u0435?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">49.34% <\/span><span class=\"tm-article-poll__answer-label\">\u0423\u0437\u043d\u0430\u043b \u043f\u0440\u043e Unsafe<\/span><span class=\"tm-article-poll__answer-votes\">75<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 49.34%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">27.63% <\/span><span class=\"tm-article-poll__answer-label\">\u0417\u043d\u0430\u043b \u043f\u0440\u043e Unsafe, \u043d\u0435 \u0437\u043d\u0430\u043b \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c final<\/span><span class=\"tm-article-poll__answer-votes\">42<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 27.63%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">23.03% <\/span><span class=\"tm-article-poll__answer-label\">\u041d\u0438\u0447\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e Unsafe \u0438 \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/span><span class=\"tm-article-poll__answer-votes\">35<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 23.03%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 152 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.    \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0438\u0441\u044c 24 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/767114\/\"> https:\/\/habr.com\/ru\/articles\/767114\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-357367","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357367"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357367\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}