Expression statement Arithmetic of Java self-study discourse

Source: Internet
Author: User
Tags logical operators

Chapter III expression-statement arithmetic

From here we're going to get into the program inside the statement, whether it's c C + + or is Java , they are all composed of expression statement arithmetic and so on.

expression: consists of operands and operators, operands can be constants, variables can be methods, and operators are arithmetic symbols in mathematics, such as "+","-","*","/","%"and so on.

An expression leads to so many things, let's take a look at one by one and see what this is.

Operand: constant, variable, or method (how is the method used?) )

Common Operators

operators: Mathematical arithmetic symbols, in addition to mathematics, there are several different, we must remember ~ take the complement % Modulo (take the remainder) ! Inverse (only for Boolean values)= assignment, which is explained by an instance.

Public CLASSJAVAFH

{

public static void Main (String args[])

{

byte a = java.lang.byte.max_value;// The maximum value is 127 Minimum Value -128

Boolean B = false;

int c = 5, d = 2, E;

e = c%d; 5/2=2 ... 1

System.out.println ("a =" +a+ ", ~a =" +~a "); ~a Take the minimum value

System.out.println ("b =" +b+ ",!b =" +!b "); b Inverse is true

System.out.println ("5%2 =" +e);

}

}

increment and decrement operations

a++ The entire statement is executed before the a the value plus 1 perform the re-assignment first

++b you first put b the value plus 1 and then executes the entire statement assign value before executing

a-- The entire statement is executed before the a the value minus 1

--b you first put b the value minus 1 and then executes the entire statement

Instance

Public Classjavajj

{

public static void Main (String args[])

{

int a,b,c,d;

A = 6;

b = 6;

c = 6;

D = 6;

System.out.print ("a =" +a);

System.out.println (", a++ =" + (a++) + ", a =" +a ");

System.out.println ("b =" +b+ ", ++b =" + (++b) + ", B =" +b ");

System.out.println ("c =" +c+ ", c--=" + (c--) + ", c =" +c ");

System.out.println ("D =" +d+ ",--d =" + (--d) + ", d =" +d ");

}

}

logical operations and parentheses operations

There are two main logical operators

&& Logic and It's true.

|| logic or false Leave is false

the logical operation is mainly applicable to IF statement, IF The following tutorial describes in detail

Parentheses operations in the mathematical parentheses

The following is an example of a logical operation and a parenthesis operation.

Example: Xiao Ming's Chinese achievements the score, Math score the score, English score - points, the text of the comprehensive is - calculate the average score and determine whether the average the points above to print out excellent, in 70-80 between the points printed out well, in 60-70 print out qualified, in - The following print out of the unqualified.

Public CLASSJAVACJ

{

public static void Main (string[] args)

{

int yw=72,sx=80,en=53,wz=55;

float PJ;

PJ = (YW+SX+EN+WZ)/4; The parentheses symbol is used

if (PJ >= 80)

System.out.println ("Pingjun fen Shi:" +pj+ "Cheng Ji You Xiu");

if (PJ >=) && (pj<)// the use of logic and It's true.

System.out.println ("Pingjun fen Shi:" +pj+ "Cheng Ji Liang Hao");

if ((PJ >=) && (pj< 70))

System.out.println ("Pingjun fen Shi:" +pj+ "Cheng Ji he ge");

if (PJ < 60)

System.out.println ("Pingjun fen Shi:" +pj+ "Cheng Ji bu he ge");

}

}

In the above example, the function implementation is no problem, feel too troublesome, mainly introduce the function of parentheses and logical operators, I hope you can understand.

Priority Level

Priority is the first multiplication after the addition of parentheses, the general principle is first left after the right, that is, mathematical principles.

An expression

An expression is a constant, a variable or other operand that is combined with an operator.

The statement in the above example is composed of expressions, but we want to learn concise expressions

+=  -= *=  /=

a =+ b that means to a+b value is assigned to the a

-         * / similar

A *= b++ a=a*b;b++

A *=++b b++;a=a*b

The following example illustrates the use of these concise expressions

public class JAVASL

{

public static void Main (String []args)

{

int a=2,b=2;

System.out.println ("a =" +a+ ", B =" +b+ ", (a+=b), a =" + (a+=b) ");

System.out.println ("a =" +a+ ", B =" +b+ ", (a+=b++), a =" + (a+=b++) ");

System.out.println ("a =" +a+ ", B =" +b+ ", (a+=++b), a =" + (a+=++b) ");

System.out.print ("a =" +a+ ", B =" +b ");

}

}

data type conversion of an expression

Principle: A type conversion can be done with no loss of data, but the Boolean value cannot be converted.

Conversion principle:

1 a type that consumes less bytes converts to a type that consumes more bytes.

2 character types are converted into int type.

3 int type is converted into float type.

4 in an expression, if the type of the operand is Double , the other operation number is also converted to Double type. In an expression, if the type of an operand is double, the other operand is also converted to a double type.

5 Boolean types cannot be converted to other types.

Look at this example and everyone will understand.

Public Classjavazh

{

public static void Main (string[] args)

{

int a = 2;

Char b = ' B ';

float C = 2.5f;

Double d = 4.25;

System.out.print ("(B/C) + (d/a) =");

System.out.println ((B/C) + (d/a));

}

}


This article is from the "Ops era" blog, please be sure to keep this source http://daomeng.blog.51cto.com/1655407/1812357

Expression statement Arithmetic of Java self-study discourse

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.