Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of naming, identifiers, and variables in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "detailed introduction of naming, markers and variables in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of naming, markers and variables in Java.

I. Marker

Identifiers are used to name variables, constants, methods, classes, and packages, distinguishing between upper and lower case letters.

A sequence of characters consisting of letters, numbers, underscores (_), and dollar signs ($)

Must start with a letter, an underscore (_) or a dollar sign ($)

Cannot be reserved word, true, false, null

Can be of any length.

Second, variables

Used to store data that will be used in the program. Using variables requires declaration and initialization.

For example: int age;age = 1; (age is a variable, int age is a declaration, age = 1 is initialization)

Assignment statement (assignment expression)

After declaring a variable, use an assignment statement (assignment statement) to assign a value to it.

In Java, the equal sign (=) is used as the assignment operator.

The variable name must be to the left of the assignment operator, so 1 = x is wrong.

System.out.print (x = 1); equivalent to x = 1 position system. Out.print (x)

IV. Naming constant

Syntax: final datatype CONSTANTNAME = VALUE; needs to be declared and assigned in the same statement and is conventionally named in uppercase.

For example, final double PI = 3.14159

Advantages: 1. You don't have to enter the same value repeatedly; 2. If you have to modify the constant value, you only need to change it in one place in the source code.

5. Numerical data types and their operations

Each data type has its own range of values, and the compiler allocates memory space according to the data type of the variable's live constant.

Byte-2 ^ 7 ~ 2 ^ 7 8-bit signed number

Short-2 ^ 15 ~ 2 ^ 15-1 16-bit signed number

Int-2 ^ 31 ~ 2 ^ 31-1 32-bit signed number

Long-2 ^ 63 ~ 2 ^ 63-1 64-bit signed number

Float 32-bit signed number

Double 64-bit signed number

Java uses four types of integers: byte, short, int, long

Java uses two types of floating point numbers: float (single precision) and double (double precision)

Be careful to prevent overflow during use (Java does not report warnings or errors about overflow).

VI. Numeric operator

Arithmetic operators: plus sign (+), minus sign (-), multiplication sign (*), division sign (/), remainder sign (%)

A few points to pay attention to:

When the operands of division are all integers, the result of division is an integer (e. G.: 5gamp2 = 2mai 5bin2 =-2)

The remainder used for division can be used for positive and negative integers and floating point values (for example: 7% 3 = 1,-7% 3 =-1, 20 = 7, if the divisor is negative, the remainder is negative)

7. Short-cut operator

There are: + = (addition assignment operator),-=, * =, / =, + + (self-increasing),-- (self-subtracting operator)

For example: I + = 8; equivalent to I = I + 8

IIncrement + (after the self-increment operator), add 1 to the value of the variable I but use the original value of I

+ + I (prefix self-increment operator), add 1 to the value of variable I and use the new value after I increase

The same goes for iMui-and-- I.

Int I = 10

Int num = 10 * I; equivalent to int num = 10 * I; I = I + 1

VIII. Numerical type conversion

The operation of converting one data type to another.

A variable that converts a variable of a small type to a large type is called a widening type, and the conversion can be performed automatically.

A variable that converts a variable of a large type to a small type is called a narrowed type (narrowing type). It must be shown to be complete, otherwise a compilation error will occur.

Syntax: the target type is placed in parentheses, followed by the variable name or value to be converted.

For example, System.out.println ((int) 1.7); result output 1.

IX. Escape sequence of special characters

For: System.out.println ("He said" Java is fun ""); if there is a syntax error, the compiler will assume that the second quote character is the closing flag of the string and do not know what to do with the remaining characters.

To solve this problem, Java defines escape sequences to represent special characters.

\ b (Backspace key),\ t (Tab key),\ n (newline symbol),\ f (). \ r (enter),\ (backslash),\'(single quotation marks),\ "(double quotation marks)

So System.out.println ("He said\" Java is fun\ "); is right.

Conversion between character char data and numerical data

Char data can be converted to any numerical type, and vice versa. When converting an integer to char data, only the sixteen bits of the data are used and the rest are ignored.

When you convert a floating-point value to a char, the float is first converted to an int, and then to a char.

When a char data is converted out of the cabinet, the Unicode of the character is converted to a specific value.

For example, int I = (int) 'Achievement system. Out.println (I); / / output 65

At this point, I believe you have a deeper understanding of the "detailed introduction of naming, identifiers and variables in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report