Numerical operations for getting started with Java

Source: Internet
Author: User
Tags abs array length

  1. Integers and decimals can have an error because the decimal is converted to a binary number, which can be an infinite loop decimal, such that an integer is converted to a floating-point, and then the result of the operation is forced to transform:
    int n = (int) 10-24.0/5;
  2. Rounding tips, you can first convert the floating point number + 0.5, and then convert to an integer:
    INI n = (int//
  3. In a Boolean operation, once a condition is computed with the operation and or operation, the result is no longer running:
     Public classBooleantest { Public Static voidmain{intn = 0; BooleanA = 5/n;//will be an error         Booleanand = (5 < N) && (5/n);//returns false as long as there is a condition that does not meet the criteria to return and stop the subsequent calculation         Booleanor = (5 > 0) | | (5/n);//returns true as long as one satisfies the condition and returns and stops the subsequent calculation
    System.out.println (and);
    System.out.println (or);
    } }

  4. The ternary operator (B?X:Y), which calculates X or y based on the condition, calculates only one, and the type of x and Y must be the same.
    int n = +; Boolean positive = n >= 0; int abs = positive? N:-N;

  5. Char and string are primitive type characters and reference type strings, the reference type means that the variable itself points to a string, and changing the value of the string variable simply modifies the pointer to the variable and does not have the value of the table string.
  6. Integers and decimals can have an error because the decimal is converted to a binary number, which can be an infinite loop decimal, such that an integer is converted to a floating-point, and then the result of the operation is forced to transform:
    int n = (int) 10-24.0/5;
    1. Rounding tips, you can first convert the floating point number + 0.5, and then convert to an integer:
      INI n = (int) (12.5 + 0.5); 13
    2. In a Boolean operation, once a condition is computed with the operation and or operation, the result is no longer running:
      public class Booleantest {public     static void main{         int n = 0;         Boolean  a = 5/n;  Will error         Boolean and = (5 < N) && (5/n);//return False if one does not meet the condition to return and stop after the calculation of         Boolean or = (5 > 0) | | (5/n);  Returns true as long as one satisfies the condition and returns and stops the subsequent calculation
      System.out.println (and);
         System.out.println (or);
      } }
    3. The ternary operator (B?X:Y), which calculates X or y based on the condition, calculates only one, and the type of x and Y must be the same.
      int n = +, Boolean positive = n >= 0; int abs = positive? N:  -N;
    4. Char and string are primitive type characters and reference type strings, the reference type means that the variable itself points to a string, and changing the value of the string variable simply modifies the pointer to the variable and does not have the value of the table string. Characters are represented by single quotation marks, and strings are denoted by double quotation marks.

    5. Array, the same type of data can be created as an array. An array is an object of a reference type.
      int New int [5];//Type [] Initializes an array, int[] Each value defaults to 0 after initialization, and the array size is immutable when initialized
      Arr[0] = 56;
      Arr[1] = 44;
      ARR[2] = 65;
      ARR[3] = 45;
      ARR[4] = 90;
      Array length can be obtained by arr.length

      You can not specify an array size, which is automatically calculated by the compiler:
      int[] ns = new int[] {68,34,43,24,54,45};
      Can be further shortened to:
      Int[] ns = {68,34,43,24,54,45};


Numerical operations for getting started with Java

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.