Java basic syntax

Source: Internet
Author: User

 

1. Java Character Set

Java adopts a character set called Unicode. This character set is a new encoding standard, which differs from common ASCII codes in that:
Unicode uses 16-bit binary instead of 8-bit to represent a character.
Many non-Latin characters are added to the Unicode Character Set.

2 Identifier: variable name, function name, constant name, class name, etc.
Java identifiers must consist of letters, numbers, dollar signs "$", underscores "_". The first character of the identifiers is not a digit.
The Java language is case sensitive to letters and is strictly case sensitive in syntax.

3 keywords
Keywords are reserved words for a specific purpose. programmers do not use keywords as their own identifiers.
Java keywords:

Abstract Boolean break byte case catch char class continue do double else extends false find finally float for implements import instanceof int interface long native New nullpackage Private Public return short static super switch synchronized this throw throws true try void while

 

4 Constants
Constants can be classified into Integer constants, floating point constants, character constants, string constants, and Boolean constants.

 

5 Java annotations
Java annotations have three forms:
Single line comment
For example: // comment on one line
Multi-line comment
For example:/* Comment on one line
Or more lines */
Document comment
For example:/** document
Comment */

 

6. Basic data types of Java

Data Type category width (BIT) value range
Boolean logical type 1 true/false
Char text type 16 0 ~ 65535
Byte Integer type 8-27 ~ 27-1
Short Integer type 16-215 ~ 215-1
Int Integer type 32-231 ~ 231-1
Long Integer type 64-263 ~ 263-1
Float float 32
Double Floating Point 64

 

7. Java Variables
Java variables must be declared before use. Such as int radios and color;
Java predefines all basic data types (pre-set values ). For example
Type name Boolean char byte short int long float double
Preset value: false '/u0000' (byte) 0 (short) 0 0 0l 0.0f 0.0

 

8. Data Type Conversion
Java is a strongly typed language. Type matching is required when values and parameters are passed. There are three types of conversions: automatic conversion, forced conversion, and method conversion using classes.
Automatic conversion: Generally, low-precision data types can be automatically converted to high-precision data types.
For example, byte b1 = 10, b2 = 20;
Int B3 = b1 + B2;
Forced conversion: forced conversion is required between high-precision and low-precision types. In this case, some information may be lost.
For example, char key = (char) (12 + 55) // The variable key is assigned a unicode value of 67 characters 'C'
Method conversion: as shown in Figure
String STR = "123 ";
Int A = integer. parseint (STR );
Use the integer method parseint to convert string to the corresponding integer.

 

9 Java programming conventions
To improve program readability, Sun recommends the following conventions for Java programming:
Classes: A class name may consist of one or more nouns. It must start with an upper-case name. The distinction between nouns is also in upper case, and the other is in lower case.
For example, class accountbook
Class complexvariable
Interface (interfaces): Same name as the rule.
For example, interface account
Method: It is composed of one or more vertices. It must start with lowercase and be separated by uppercase and lowercase.
For example, balanceaccount ()
Variable: starts with a lowercase letter, and words are separated by uppercase letters.
Example: currentcustomer
Constant: All letters are capitalized and words are distinguished by underscores.
For example, maximum_size

 

10 Java Operators

Arithmetic Operators: +,-, *,/, % (Note: 5/2 = 2; 5.0/2 = 2.5)

Comparison operators: =, <, >,< =, >=, <>

Logical operators: And, or, not

Comma OPERATOR: For (INT I = 0; I <array. lenth; I ++ ){...}

Three-object OPERATOR: (expression )? Value1: value2

 

 

11 Java statements
(1) If-Else
(2) Switch-case
(2-1) Case 'A':... Clause
(2-2) Default:... Clause
(3) Break
(4) Return
(5) While
(6) Do-while: do {...} while (...); pay attention to the last semicolon while
(7)
(8) continue
(9) package: All lowercase letters
(10) Import

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.