S1/using Java to understand program logic/02-variables, data types, and operators

Source: Internet
Author: User

Java Common data types

Data type

Description

Example

Int

Integral type

Used to store integers, such as the number of learners, the employee number of a company, the number of days in a year, the number of hours in a day

Double

Double-precision floating-point type

Used to store numbers with decimals, for example, the price of a commodity, the amount of the bank's loan to another country, the employee's wage

Char

Character type

Used to store a single character, for example, gender "male" or "female", score "excellent" or "good" (using single quotation marks)

String

String line

Used to store a string of characters, for example, employee name, product model, product introduction information (using double quotation marks)

The numeric value of type int is 4 bytes, and the double type is 8 bytes.

Variable naming rules

Serial number

Conditions

Legal variable Name

Illegal variable name

1

The variable must begin with a letter, an underscore "_", or a "$" symbol

_mycar

*myvariable1//Cannot start with *

2

Variables can include numbers, but cannot start with a number

Score1

9variable//Cannot start with a number

3

Variable names cannot contain any special characters except for the "_" or "$" symbols

$myCar

Variable%//cannot contain%

My Variable//cannot include spaces

4

You cannot use the Java language keywords, such as int, class, public, and so on

Graph1_1

A+B//cannot include +

T1-2//cannot include hyphens

The variable name is short and clearly identifies the function of the variable, which can be composed of one or more words, usually the first letter of the initial word is lowercase and the first letter of the following word is capitalized.

For example:

int ageofstudent; Student Age

int ageofteacher; Teacher Age

Attention:

int b;

int a= (b+3) * (b-1);

The last statement takes the value of variable B out of the calculation, and then stores the result in variable a. If written as "(b+3) * (b-1) =a", an error occurs. Remember that the function of "=" is to assign the result of the right-hand expression to the variable to the left of the equal sign.

Common errors:

1, the variable is assigned first use

2. Use illegal variable names

3. Variable cannot have duplicate name

(1) Automatic data type conversion

Rule 1: If one operand is of type double, the entire expression can be promoted to a double type.

Rule 2: Conditions for automatic type conversions are met.

1//Two types are compatible: numeric types (shaping and floating-point) are compatible with each other.

2//the target type is greater than the source type: The double type can hold the int type data because the double type variable allocates a space width sufficient to store the int type variable. Therefore, we also convert the INT type variable to a double type variable called a "magnified transformation".

(2) Forcing data type conversions

Syntax: (data type) expressions

Precede the variable with parentheses, and the type in parentheses is the type to be cast. For example:

Double d=34.5634;

int b= (int) D;

The following values are run:

34

As can be seen from the example, because the forced type conversion is often converted from a wide-width type to a small width type, so that the value of the loss of precision (such as 2.3 into the 2,34.5634 into 34), it can be called the image of the conversion to "narrowing conversion."

Note: The difference between "=" and "= ="

(1) "=" is an assignment operator, which assigns the value on the right to the variable to the left of "=", such as int num=20.

(2) "= =" is a comparison operator, that is, the value to the left of "= =" is compared to the value to the right of "= =" to see if they are equal, if equal is true, otherwise false, such as the 3==4 result is false.

Precedence of Operator: "()" > "! The > Arithmetic operator ("+,-,*,/,%") > Relational operator (">,>=,<,<=,==,!=") > Logical operator ("&&,| |,! ") > Assignment operator (" = ")

S1/using Java to understand program logic/02-variables, data types, and operators

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.