Chapter 2 variables, data types and operators, and data type operators

Source: Internet
Author: User
Tags float double

Chapter 2 variables, data types and operators, and data type operators

1. variables: a basic unit for storing data. Different variables are independent of each other.

2. Variable declaration and value assignment syntax

1) declare and assign values: data type variable name = value; example: int price = 10;

2) separate declaration and Value assignment: data type variable name; for example, int price;

Variable name = value; for example, price = 10;

3. perform the following steps to use a variable:

1) Declare the variable, that is, apply for a space in the memory based on the data type, and take an alias for the space; for example: money.

2) assign a value to a variable, also called an initialization variable; for example, money = 100;

3) Call the variable. For example, System. out. println (money );

Note: variables must be declared and the initial values must be assigned to them before they can be used.

4. variable naming rules

1) The variable must start with a letter, underscore, or $.

2) variables can contain numbers, but cannot start with numbers.

3) except the "_" or "$" symbol, the variable name cannot contain any special characters.

4) You cannot use keywords in the Java language, such as int class.

5. Data Type

Eight basic data types: Numeric: byte short int long float double

Non-numeric type: char boolean

Reference data type: Class interface array String

6. Data Type Conversion

Conditions for automatic type conversion

1) compatible with types (int and double are both numeric)

2) The target type is greater than the source type (double is greater than int)

Forced type conversion

(Data Type) variable name;

(Data Type) value;

For example, double num = 5.5;

Int result = (int) num;

7. Operators

Arithmetic Operator: +-*/% + + --

Assignment operator: = + =-= * =/= % =

Relational operators:> <>=<==! =

Logical OPERATOR :! & |

Bitwise OPERATOR :~ ^ >><>>>

String concatenation operator: +

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.