Java Arithmetic operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators current time generator integer division modulus rand

Java's basic arithmetic operators are the same as most other programming languages. These include a plus sign (+), minus (-), Division (/), multiplication (*), and modulus (%, which gets the remainder from integer division). Integer division cuts the decimal number directly, not the rounding.
Java also operates in a shorthand form, with assignment operations at the same time. This is marked by an operator before the equal sign, and is fixed for all operators in the language. For example, to add 4 to the variable x and assign the result to X, use: x+=4.
The following example shows the various uses of arithmetic operators:

 

: Mathops.java//Demonstrates the mathematical operators import java.util.*;
  public class Mathops {//Create a shorthand to save typing:static void prt (String s) {System.out.println (s);
  }//Shorthand to print a string and a int:static void pint (string s, int i) {PRT (s + "=" + i);
  }//Shorthand to print a string and a float:static void Pflt (string s, float f) {prt (s + "=" + f); public static void Main (string[] args) {//Create a random number generator,//seeds with the current time by de
    Fault:random rand = new Random ();
    int I, j, K;
    '% ' limits maximum value to 99:j = rand.nextint ()% 100;
    K = rand.nextint ()% 100;  Pint ("J", J);
    Pint ("K", K); i = j + k;
    Pint ("J + k", i); i = j-k;
    Pint ("J-k", I); i = k/j;
    Pint ("k/j", I); i = k * J;
    Pint ("K * j", I); i = k% J;
    Pint ("K% J", I); J%= K;
    Pint ("J%= K", J);  Floating-point number tests:float u,v,w; Applies To Doubles, too v = rand.nextfloat ();
    W = rand.nextfloat (); Pflt ("V", V);
    Pflt ("W", W); U = v + w;
    Pflt ("V + w", u); u = v-w;
    Pflt ("V-w", u); U = v * w;
    Pflt ("V * w", u); u = v/w;
    Pflt ("v/w", u); The following also works for//chars, Byte, short, int, long,//and double:u + = V;
    Pflt ("U + + V", u); U-= v;
    Pflt ("U-= v", u); U *= v;
    Pflt ("U *= V", u); U/= v;
  Pflt ("U/= V", u); }
} ///:~

The first thing we noticed was a quick way to print (display): The PRT () method prints a string;pint () prints a string and then prints an int, while Pflt () prints a string and then prints a float. Of course, they end up with System.out.println ().
To generate a number, the program first creates a random (random) object. Because the arguments are passed during the creation process, Java uses the current time as a "seed value" by a random number generator. By random objects, programs can generate many different types of random numbers. The practice is simple, just call a different method: Nextint (), Nextlong (), nextfloat () or nextdouble ().
If used with the results of a random number generator, the modulus operator (%) can limit the result to the upper limit of 1 for an operand (this example is 99).

1. Unary Plus and minus operators
unary minus (-) and unary plus (+) are the same operators as the two plus and minus signs. Depending on how the expression is written, the compiler automatically determines which one to use. For example, the following statement:
x = A;
The meaning of it is obvious. The compiler can correctly identify the following statement:
x = A *-B;
But readers will be confused, so it's better to write more explicitly:
x = A * (b);
The negative value of an operation object obtained by a unary minus sign. The meaning of a unary plus is the opposite of a unary minus, although it does not actually do anything.

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.