Java chapter II Variables and operators

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

Java Basic application Programming-- variables and operators

1 , Java Program Structure

data type: determines the type of data to be stored in memory .

Variable: is the basic unit for storing data

2 , the concept of variables

A variable is a quantity that can change at any time during the course of a program's operation.

variables are temporary storage places for data in programs

3 , data type and classification

Basic data type:4 classes of 8 basic data types

Reference Data Type

integer Type -byte , Short , int default, Long

Java Three representations of the middle integral type

decimal integer:0 ~ 9, note: The first digit cannot be 0

hex Number:0 ~ 9 A ~ F, note: Must start at 0x or 0X

Example: 0X8A 0x12

octal integer:0 ~ 7, note: Must start with 0

Example: 0123 012

octal integer:0 ~ 7, note: Must start with 0

floating-point type -float , double

Similar to integer types, Java floating-point types have a fixed number of table ranges and field lengths that are unaffected by the platform

Character Type -char

A single character is represented by a char type and is typically evaluated as:

English letters, numbers, escape sequences, special characters, etc.

The characters in Java account for two bytes (16 bits), so hexadecimal encoding is used to represent

The values of character types in Java are usually enclosed in single quotation marks (?). ').

? ' A '-0x0041

? 0x4e2d, ' Zhong '

? In the code: ' 0 ' ~ ' 9 ', ' a ' ~ ' Z ', ' a ' ~ ' Z ' are all consecutively coded!

? ' 0 ' is not the same character as ' \u0000 '

? Minimum value: 0, max: 65535 = 2^16-1

– Character literals use single quotation marks as bounding symbols:

? ' Medium ' is an integer constant!

Escape character

4 , conversion rules for data types

Two ways to convert data types:

Automatic type conversion (implicit type conversion): When there are several types of data blending operations, the system first automatically converts all data to the data type with the largest capacity and then calculates

Automatic conversion rules

The Boolean type cannot be converted to another data type

Integer, character, and floating-point data are converted to each other in a mixed operation, and the following principles are observed when converting

Byte, short, char do not convert to each other, the three of them in the calculation of the first conversion to int type

Forcing type conversions (display type conversions)

When a large data type is converted to a small-capacity data type, a forced type conversion is required, with the following format:

For example : Byte A;

int b;

A = (byte) b;

During coercion type conversion, the value of the source type may be greater than the target type and may result in reduced precision or overflow

5 , operator (operator)

is a special symbol that represents the operation, assignment, and comparison of data

Types of operators:

Arithmetic operators: +, ﹣, *,/,%, + +,--

Assignment operators: =, + =, ﹣=, *=,/=,%=

Comparison operators:>, <, >=, <=, = =,! =, instanceof

Logical operators:!, &&, | |

Bitwise operators: &, |, ^, ~, >>, <<, >>>

Conditional operator:? :

The conditional operator is unique because it is a ternary operator that consists of three operands of an expression. It can replace some kind of if-else statement

Arithmetic operators

Self-increment operation: + +,--

? i++, after + +, first the value of I as the entire expression value, and then add I 1.

? ++i, first + +, first increase I by 1, and then the value of I as the entire expression value.

? Such as:

–int a = 1; int b=1;

–b = a++;//

? 1 The value of the a++ expression is 1

? 2 Executive A=a+1

? 3 performs an assignment operation and assigns the value 1 of the expression to B, which is b=1

Assignment operators

Relational operators (comparison operators)

Logical operators (Boolean operators)

Operands at both ends of a logical operator must be a Boolean type value

bitwise OPERATIONS (valid only for byte,short, char,int, long )

The left and right shift operations are equivalent to the n-times of integers multiplied by or divided by 2

Logic and or the same bitwise AND OR somewhat different:

– The same point is: All can participate in the logical operation, bitwise AND OR completely can replace the logic and OR;

– The difference is that bitwise AND or can participate in bit operations, logic and or only participate in logical operations, logic and or can be "shorted", bitwise AND OR cannot be "shorted".

? Short circuit operation

– Function Operators: logic and &&, Logical OR | |

e.g. (1): Logical expression: A>b && c>d

Assuming that a>b is false,c>d true, the entire expression evaluates to false;

Assuming that A>b is false,c>d to false, the entire expression is also false;

It can be seen that the result of the A>B has determined the result of the whole expression, and the latter c>d does not affect the result of the expression, it may be said that the latter part is "short-circuit".

e.g. (2): logical expression: a>b | | C>d

Assuming that a>b is true, then the second half of the expression will be "shorted";

Ternary operators

– The so-called ternary operator, which is a centralized comparison of three expressions, the knot of expression 1

Is true, it is the second expression and, if False, a third table

Up to the type. The syntax is:

– Expression 1? Expression 2: Expression 3

– The ternary operator is illustrated by the following code.

Precedence of Operators

-An expression is usually made up of multiple operators. Precedence rules determine the order in which each operator is evaluated in any given expression

6 , an expression

Value and type of an expression

The result of operations on operands in an expression is called the value of the expression

The data type of an expression value is the type of an expression

The order of operations of an expression

should be in order of precedence of operators from highest to lowest

Operators with the same precedence are performed in a pre-agreed binding direction

The combination direction and priority of an expression

Java chapter II Variables and operators

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.