Fundamentals of Java Programming (i)

Source: Internet
Author: User

Fundamentals of Java Programming (i)
1. Java Basic Syntax
1.1 Java Basic format

Modifier Class class Name {

Program code

}

    1. Java is strictly case-sensitive, such as the class keyword cannot be written as class, and the Java compiler will not recognize it.
    2. A sequential string in a Java program cannot be written in a line, and if you want to write a branch, you can make two strings, with the + sign connected in the middle.
    3. Each statement ends with a semicolon.

1.2 Java annotations

1.2.1 Features:

Added code readability, easy to read, it is only valid in the source file, compile-time compiler will ignore this information, do not compile it into the class file.

1.2.2 Classification

There are three types of comments:

    1. Single-line Comment

Usually used to explain a line of code, with a symbol//, followed by the comment content, the concrete example is as follows:

int c = 10//define an integer

    1. Multi-line comments,

As the name implies, a multiline comment is that the content in the comment can have more than one line, which begins with the symbol "/*" and ends with "*/", and the multiline comment is not nested.

For example:

/*

int c=10; You don't want to delete code, you don't want to use it, you can use multiple lines of comments

int x=5;

*/

    1. Document comments

A document comment is a comment description of a piece of code that can be extracted from a document comment using Javadoc to generate a Help document, and document comments are not nested.

Format: Javadoc–d document storage path [–version–author] destination file

-version and –author are optional, indicating that you want to add a version number and author to the help document.

To use Javadoc to generate a help document, the class must be decorated with public

Details of the use of 1.2.3 annotations:

1. Give the statement a description, and the comment should be written next to the statement.

2. Single-line comments are generally written in the back of the statement multiline comments and document comments are generally written on top of the statement

Note: Document annotations can only appear above the class, property, method.

1.3 Key Words

Keywords are words that the programming language defines in advance and gives special meaning, which represents a data type, or the structure of a program, also known as a reserved word. Many keywords are reserved in Java, such as Class,public.

Do not have to spend time to remember, use more, nature will remember.

1.4 Identifiers

In the programming process, it is often necessary to define some names in the program, such as package name, class name, interface name, and so on. These symbols are called identifiers.

Precautions:

    1. Identifiers can be made up of any order of uppercase and lowercase letters, numbers, underscores (_), and dollar signs ($)
    2. Identifiers cannot start with a number
    3. The identifier cannot be a keyword in Java

Programming Habits:

    1. Package name All letters are lowercase, such as cn.itcast.test.
    2. The first letter of each word of the class name and interface name is capitalized, such as ArrayList, Iterator.
    3. All letters of the constant name are capitalized and the words are underlined to connect, such as Day_of_month
    4. The first letter of the variable name and method name is lowercase, and the other word is capitalized, such as LineNumber,
    5. In the program, you should try to use meaningful English words to define identifiers, so that the program is easy to read. If you use username to identify the user name, password to represent the password.

1.5 Constants in Java

A constant is the amount of value that cannot be changed during a program's operation.

Categories of constants:

  1. Integer constants: All integers
  2. Floating-point constants: All decimals
  3. Character constants: All single characters
  4. String constants: All strings
  5. Boolean constants: True and False
  6. NULL constant: Only one value is null

    1. Integral type constant

There are three kinds of forms, divided into decimal, binary, octal, hex

Decimal: All are made up of 0-9 of these nine digits and cannot start with 0.

Binary: consists of 0 and 12 digits.

Octal: Consists of 0-7 numbers, in order to differentiate it from the other binary numbers, beginning with 0.

Hex: Consists of 0-9 and a-f. To differentiate between other numbers, start with the ox.

Binary: A notation that represents all the numbers in a finite number of digits

In life, we are most often exposed to the decimal, because humans in the calculation of the natural use of 10 finger counting

However, the use of binary in the computer to store data, because this is the simplest implementation, hardware implementation is convenient, because the binary data writing and memory is very non-trivial, resulting in octal and hexadecimal, 3-bit binary equivalent to a bit 8 binary, 4-bit binary representation of a hexadecimal.

People use decimal, the computer underlying processing data is binary, octal, hexadecimal,

2. Floating-point constant

Floating-point constant is the decimal number used in mathematics, is divided into float single-precision floating-point number and double-precision floating-point number, single-precision floating-point number with F or F end, double precision with D or D end, if no suffix default is double.

Floating-point constants can also be expressed in exponential form, for example:

2e3f 3.6d 0f 3.84d 5.022e+23f

3. Character constants

A character constant is a char that is caused by a single quotation mark in an English half-width format, which can be an English letter, a number, a punctuation mark, and a special character represented by an escape sequence. For example

' A ' ' 1 ' & ' \ R '

Escape character (\): Used to escape the latter character to represent a special meaning. The common special characters that are represented by escape sequences are:

\ r: Represents a carriage return that positions the cursor at the beginning of the current line and does not jump to the next line.

\ n: Represents a newline character and jumps to the beginning of the next line.

\ t: Represents a tab: Moves the cursor to the next tab stop, just like the TAB key function.

\b: Represents backspace, just like the function of the BACKSPACE key on the keyboard.

\ ': Represents a single quotation mark in a string, removing the ability to denote the beginning end of a character, and if the Java compiler does not escape, it will assume that the previous two is a pair and an error.

\ ": to represent a double quotation mark in a string, to remove the double quotation marks to indicate the beginning of the string function, if not escaped the Java compiler will consider the previous two is a pair, and error.

\ \: The function of removing the escape character, representing the literal \.

4. String

Used to denote a string of consecutive characters, with "" to indicate the beginning and end of the strings.

5. Boolean Constants

Boolean constants have only two values of true and false, which are used to differentiate between true and false of a thing.

6.Null

A null constant has only one value, NULL, which indicates that the reference to the object is empty.

1.6 Variables in Java

Some temporary data is generated during the run of the program, and the application stores the data in some memory units, each with identifiers indicating that the value of the identifier is the variable name, and the value of the memory unit is the value of the variable. It can be said that a variable is a container for storing data, the value of which can be updated frequently.

Defining the data type of a variable is in fact the size of the memory unit, and the memory units required for different data stores are different.

8 Basic data types: embedded in the Java language, with the same size and attributes.

Integer type variable (byte,short,int,long)

Floating-point type variable (float, double)

Character type variable (char)

Boolean type variable (Boolean)

1. Integer type variable

An integer type variable is used to store the value of an integer type, because defining a variable calls a different size of memory space, in order to reasonably allocate storage space, the integer type is divided into 4 different types, and its storage space and presentation range are as follows:

Byte 1 bytes-27 ~ 27-1

Short 2 bytes-215 ~ 215-1

int 4 bytes-231 ~ 231-1

A long 8 bytes-263 ~ 263-1

A variable of type int should occupy 4 bytes of memory space, representing a value from 27 to 27-1.

When assigning a value that exceeds the range represented by the int type, it must be appended with L or L because Changshime considers the int type to exceed the range that can be represented, and the use will error.

2. Floating-point type variables

In Java, floating-point types have single-precision (float) and double-precision (double), and the double type represents a more accurate floating-point number than float.

When assigning a value to the float type variable, the decimal must be followed by F or F, because the floating-point constant defaults to the double type variable, and the large range is assigned to a small range, which will result in an error.

3. Character type variables

Used to store a single character, represented in Java in Char, and each char type variable occupies two bytes.

You can also assign a variable of type char to an integer in the 0~65535 range, and the computer automatically converts the character of the integer, such as:

char c = ' a ';

Char CH 97;

4. Boolean type

Used to store a Boolean value of True and false to occupy a byte in memory. Specific examples are as follows:

Boolean flag = false;

Flag = true;

1.7 Type conversions for variables

When assigning the value of a variable of one data type to a variable of another data type, a data type conversion is required because the two data types must be converted in a different format for memory space storage.

A Automatic type conversion (implicit conversion)

Automatic type conversions must meet two conditions

    1. The two data types are compatible with each other,
    2. The value range of the destination type is greater than the storage range of the source type.

For example:

Byte B = 3;

Int x = b;

Because the value range of the int type is greater than the byte type, the compiler does not cause data loss during the assignment, and all compilers automatically complete the conversion without reporting any errors at compile time.

Two Forcing type conversions

Coercion type conversions are also called explicit conversions, meaning that when two data types are incompatible with each other, or if the target type's value is less than the source type, an explicit declaration is required to enforce the type conversion.

such as: int num = 4;

Byte b = num;

There are reports of possible loss of precision, in which case a forced type conversion is required. The specific format is as follows

byte B = (byte) num; After that, there will be no error.

However, after casting, it is very possible to lose precision, because byte can only be one byte, int has 4 bytes, after conversion, the data stored in the first three bytes of int is lost, leaving only the last byte of data.

Note: Variables of type Byte are automatically promoted to int during the operation, and if you also want to assign data of type int, you want to cast, for example:

Byte B1 = 3;

Byte b2 = 4;

Byte B3 = b1 + b2;

will report the error of loss accuracy, so the last sentence should be changed to byte B3 = (byte) (B1+B2);

1.8 Scope of variables

A variable needs to be defined before it is used, and it is defined within the curly braces, and it can only be used within the curly braces, not out of range.

Fundamentals of Java Programming (i)

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.