Home Java javaTutorial Analysis of Java Programming Example Questions

Analysis of Java Programming Example Questions

Apr 24, 2023 pm 07:07 PM
java

Question 1:

<code>public static void demo01() {<br>    Integer f1 = 100, f2 = 100, f3 = 200, f4 = 200;<br>    System.out.println(f1 == f2);<br>    System.out.println(f3 == f4);<br>}</code>
Copy after login

Question 2:

<code>private static Integer i;<br>public static void demo02() {<br>    if (i == 0) {<br>        System.out.println("A");<br>    } else {<br>        System.out.println("B");<br>    }<br>}</code>
Copy after login

Question 1 answer:

true

false

Answer to question 2:

NullPointerException

Analysis:

Question 1:

The following is the source code of "autoboxing" in the Integer class:

<code>public static Integer valueOf(int i) {<br>    if (i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)<br>        return IntegerCache.cache[i + (-IntegerCache.low)];<br>    return new Integer(i);<br>}<br></code>
Copy after login

The value of IntegerCache.low is -128, and the value of IntegerCache.high is 127. That is to say, when Integer is automatically boxed, if the range of the integer value is judged to be between [-128,127], the value in the integer constant pool will be used directly; if it is not in this range, a new Integer() will be created . Therefore, f1 and f2 in this question are both in the range of [-128,127] and use the same value in the constant pool. However, f3 and f4 are not in the range of [-128,127], and their values ​​​​are both new, so f3 and f4 are not the same object.

Question 2:

The default value of Integer i is null. When i==0 is executed, the right side of the equal sign is a number, so in order to perform comparison operations, Integer will be automatically unboxed (that is, Integer will be converted to int type). Obviously, if you unbox null (convert null to a number), a NullPointerException will be reported.

The above is the detailed content of Analysis of Java Programming Example Questions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles