Java basics 2-basic syntax, java2-syntax

Source: Internet
Author: User

Java basics 2-basic syntax, java2-syntax

1. identifier

Definition: the sequence of Characters Used for class name, method name, package name, and parameter name naming in Java

Rules:

It consists of letters, numbers, underscores, and $ characters.

Cannot start with a number

Unlimited length

Case Sensitive

It cannot be a reserved keyword in java.

Habit: Do this for Identifiers

2. reserved keywords: goto and const

3. escape characters

\ N: line feed

\ R: Enter

\ T: horizontal tabulation

\ ': Generate a single quotation mark

\ ": Generates a double quotation mark.

\: Generate a slash

4. Data Type

Integer type: byte (1 byte,-128-127), short (2 bytes), int (4 bytes), long (8 bytes)

In java, the default Integer type is int. To declare a long variable, add L or l to the variable value.

Float: float (4 bytes), double (8 bytes)

The default double type of floating point in java. If you want to declare a float type variable, you need to add F or f.

Character Type:

In Java, a character is stored by char. The character value must be enclosed by single quotation marks, and one character in java occupies two bytes, Because java adopts Unicode international unified encoding, in addition, the char type can be stored at noon, because a Chinese character also occupies two bytes.

Boolean Type: in java, the boolean type only has two values: true and false.

5. Variables

Definition: variable in the variable amount of a program

Syntax:

Step 1: edge declaration and edge assignment

Data type variable name = variable value;

Example: int num = 30;

Step 2: declare and assign values

Data type variable name;

Variable name = variable value;

For example: int num2; num2 = 100;

6. Steps for defining variables:

Step 1: define the data type to open up memory space

Step 2: Define the variable name to be associated with the opened memory space.

Steo3: assigning values to variables is actually writing data to the memory space.

Step 4: The output variable actually reads data from the memory space of the variable.

7. constants:

The so-called constant refers to the variable amount in the program. Once assigned, it cannot be changed.

8. Syntax:

Final data type constant name = constant value;

9. Conversion between basic data types in Java

The boolean type cannot be converted to any other data type.

Automatic type conversion: the data type with a small capacity is automatically converted to a data type with a large capacity.

A) byte, short, int à long à float à double

B) byte, short, and int are not converted to each other. They are converted to the int type during calculation.

Forced type conversion: when a type with a large capacity is converted to a data type with a small capacity, a forced conversion character must be added.

C) long l = 100L;

D) int I = (int) l;

E) it may cause reduced precision or data overflow. Be careful when using it.

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.