Java Array Operations

Source: Internet
Author: User

When using Java for programming, when assigning an integer constant to a long variable, if the value exceeds the value range of the int data, the program will have a compile error, but there is a case where the program will not be faulted. is to assign multiple int data to a long variable, even if the result of the operation exceeds the range of int data, the program will not compile errors.

Test 1:

Long num=2147483648, when assigning a value to num, an error occurred, exceeding the range of values.

Test 2:

    1. Package Test;

    2. public class Test {

    3. public static void Main (string[] args) {

    4. Long num=2147483647*10;

    5. SYSTEM.OUT.PRINTLN (num);

    6. }

    7. }

Operation Result:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160810/ae5ec17991a64c578d5a9ac39b019358.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Cause: The two integers participating in the calculation do not exceed the range of the int data, so there is no compilation error, and the result error is that when the two numbers are evaluated with the int data, the result is the int type, because the result of the calculation is beyond the range of the value of the int data. So the program overflows, but the program retains the result of the overflow, so the output is-10.

Test 3:

    1. Package Test;

    2. public class Test {

    3. public static void Main (string[] args) {

    4. Long num=2147483647*10;

    5. SYSTEM.OUT.PRINTLN (num);

    6. Long num1=2147483647l*10;

    7. System.out.println (NUM1);

    8. }

    9. }

Operation Result:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160810/74f29eea37004be2aac310d574cc2f6e.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Cause: Because the uppercase letter L is added after the first integer constant in the calculated two integers, the operation to be performed is done by a long integer, so the program does not overflow and can calculate the correct result.

Technology sharing: www.kaige123.com

This article from the "11247808" blog, reproduced please contact the author!

Java Array Operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.