Java assignment, arithmetic, and unary operators (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/15/2819621.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

Simple value assignment operator

One of the most common operators is the simple value assignment operator "=". It grants the value from the right of the operator to the left:

 
Int Cadence = 0; int speed = 0; int Gear = 1;

The operator is also used to assign an object to an object reference. For more information, see "create an object ".

 

Arithmetic Operators

JavaProgramming LanguageOperators for addition, subtraction, multiplication, and division are provided. In basic operations, it is a good method to recognize them. The unfamiliar symbol is '%'. It performs the division operation and returns the remainder as the result.

 
+ Addition operator-subtraction operator * multiplication operator/division operator % modulo operator

The followingProgram,Arithmeticdemo:

Class arithmeticdemo {public static void main (string [] ARGs) {// result is now 3 int result = 1 + 2; system. out. println (result); // result is now 2 result = Result-1; system. out. println (result); // result is now 4 result = Result * 2; system. out. println (result); // result is now 2 result = Result/2; system. out. println (result); // result is now 10 result = Result + 8; // result is now 3 result = Result % 7; system. out. println (result );}}

Arithmetic Operators and value assignment operators can be combined to form a composite value assignment. For exampleX + = 1;AndX = x + 1;Both of them will assign the result of the expression x + 1 to X.

+ Operators can also be used to connect two strings, suchThe concatdemo program is as follows:

Class concatdemo {public static void main (string [] ARGs) {string firststring = "this is"; string secondstring = "A concatenated string. "; string thirdstring = firststring + secondstring; system. out. println (thirdstring );}}

Variable at the end of the programThe value of thirdstring is "This Is A concatenated string." And output it to the console.

 

Unary operator

One-dimensional operators only require one operand. There are many operations, such as auto-increment/auto-increment values, reverse expressions, and reverse boolean values.

 
+ Mona1 + operator; indicates a positive value (but the value is positive by default)-mona1-operator; returns the inverse ++ auto-increment operator; increments the value by 1-the auto-increment operator; reduce the value from 1! Logical operator to reverse the Boolean Value

Next programUnarydemo, test the unary operator:

Class unarydemo {public static void main (string [] ARGs) {// result is now 1 int result = + 1; system. out. println (result); // result is now 0 result --; system. out. println (result); // result is now 1 result ++; system. out. println (result); // result is now-1 result =-result; system. out. println (result); Boolean success = false; // false system. out. println (SUCCESS); // true system. out. println (! Success );}}

The auto-increment/auto-increment operators can be prefixed and followed by suffixes ).CodeResult ++; and ++ result; both allow the result to increase by 1. the only difference is that the prefix version (++ result) calculates the returned auto-increment value, while the suffix version (result ++) calculates the returned original value. If you simply execute auto-increment/auto-increment, you do not need to worry about which version to use. However, if you use the auto-increment/auto-increment operators in a larger expression, each version will produce significantly different results.

The next program,Prepostdemo: prefix/suffix unary operator:

Class prepostdemo {public static void main (string [] ARGs) {int I = 3; I ++; // prints 4 system. out. println (I); ++ I; // prints 5 system. out. println (I); // prints 6 system. out. println (++ I); // prints 6 system. out. println (I ++); // prints 7 system. out. println (I );}}

 

Related Article

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.