Java's understanding and basic syntax

Source: Internet
Author: User

Overview of Java

The origin of Java, the birth of the 95, with portability, security, multi-threading, object-oriented and other characteristics of the language. Java program development process: helloword.java→ computer compiles →helloword.class→java virtual machine.

Java Software Installation

Download installation of JDK and configuration of environment variables:

1.java_home:c:\program files\java\jdk1.7.0_13 (Installation path);

2.PATH:%java_home%\bin;%java_home%\jre\bin;

3.CLASSPATH:.; %java_home%\lib;%java_home%\lib\tools.jar;

Basic operations for writing Java programs

Compilation of Java source programs:

public class HelloWorld {

public static void Main (string[] args) {

System.out.println ("HelloWorld");

}

}

Edit the Java program with Notepad, and then compile and run the program.

Basic data type of Java: Byte type byte 1 byte 8 bit

Short 2-byte 16-bit

Integer int 4 bytes 32 bits

Long 8-byte 64-bit

Single-precision float 4 byte 32-bit

Double Doubles 8 bytes 64 bits

Character Char 2 byte 16 bit

Boolean-Type Boolean

Basic types of conversions and forced conversions;

Declaration and assignment of variables: int a=5;

The use of strings;

Naming rules for Java identifiers;

Method of representation of constants: final double pi=3.14159;

Usages and expressions of operators

Relational operators > < >= <= = = = =

Logical Operators && | | !

Trinocular operator? :

System.out.print ("Please enter your age");

Age = S.nextint ();

String res = (age>18)? "  Welcome to the Bathing Center ": minors are not allowed to enter"; Age>18

Arithmetic operator +-*/% + +--

Bitwise operators & Located in | Bit or ^ bit XOR-bit complement (bitwise non) << left shift operator >> right shift >>> right shift 0 operator

expression: int a = 18; The whole sentence. There is an instruction. is also a statement

A = = 19//Variable expression

Use of the IF statement and If-else

/* Single Branch */

/*if () {

}else{

}*/

/*IF Statement Multi-BRANCH Conditional statement */

if (time<4) {//(expression, boolean-expression. ) {code block}

if (money>1000) {

System.out.print ("Go to IFS to see a movie, see Imax");

}else{

System.out.print ("Go to IFS to see a movie, see 2D");

}

}

Use of Switch

Although nested if statements can fully implement the functions of multi-branch selection, the number of nested layers is too large, the program becomes tedious and difficult to read, and the readability is poor. After introducing the switch statement, the structure of the program is clear and some nesting errors are reduced. The switch statement provides an efficient way to evaluate and manipulate multiple choices in a program. Statement format:

switch (arithmetic expression) {

case constant Expression 1: statement block 1

Break

Case constant Expression 2: statement block 2

Break

...

Default: Statement

Break

}

Loop structure

The following statements are used in Java to implement loops:

While statement

Do-while statements

For statement

Depending on the position of the condition where the end of the cycle is judged, the difference is divided into:

Prior cycle, posterior cycle

while (i<5) {

System.out.println (i++);

}

Description: While is a keyword in java. Expressions in a pair of parentheses after the while are used to control whether the loop body executes, only a Boolean expression. The following loop body is a repeating part of the loop, either a basic statement, a control statement, or a compound statement enclosed in curly braces. The Do-while statement is a posteriori loop that can be executed 1 or more times. The loop code executes at least once, regardless of whether the condition of the judgment part behind the loop is true, and then determines whether the condition of the continuation loop is met. General format for Do-while loops:

do{

Loop body

}while (boolean expression);

do{

System.out.println (i++);

}while (I < 5);

Description: Do is a Java keyword and must be used in conjunction with while. The Do-while loop starts with do and ends with a while, it is important to note that the ";" after the while (expression) cannot be lost, which represents the end of the Do-while statement. The expression in parentheses after the while is used to determine whether the loop body executes.  The loop body after do can be an executable statement or a compound statement composed of multiple statements. A For loop is the most widely used loop control statement in a loop control structure. It is a priori cycle. The general format for the For loop is:

for (expression 1; expression 2; expression 3) {

Loop body

}

for (i=0;i<5;i++) {

System.out.println (i++);

}

Description: For is a java keyword expression 1 for the loop control variable initialization expression 2 as the loop continues to execute the judgment statement expression 3 as a change in the loop control variable. The loop body can be a basic statement, or a compound statement consisting of a control statement or multiple statements. The For statement is suitable for construction cycles and is known as a cyclic program;

The while statement is suitable for looping programs where the number of construction cycles is unknown

Do-while is not used much, still suitable for the construction cycle is unknown, but at least 1 times to execute the loop program

Array in Java, there are two steps to creating an array:

1, declare an array variable;

Grammar:

data type [] array variable name;

Or

Data type array variable name [];

For example:

Int[] sums; or int sums[];

2. Initialize the array with the New keyword and specify the size of the array.

Grammar:

Array variable = new array type [array length];

For example:

sums = new INT[20];

Characteristics

The array is a contiguous memory space, with the subscript starting at 0 and the maximum subscript as length-1. Each element in the array is the same type as the element in the variable array and the type of the declaration array. And each element in the array is of the same type. Once the array has been created, the length cannot be changed. Length is obtained by the array name. length. The array initializer does not need to use the New keyword when creating an array.

int [] odds = {1, 3, 5, 7, 9};

String [] weekend = {"Saturday", "Sunday"};

It is worth noting that the array initializer can only be used when declaring a new array reference.

String [] Weekend;

Weekend = {"Saturday", "Sunday"}; Compilation does not pass

An array is a contiguous memory space that accesses each element in the space by subscript, with the subscript starting at 0, and the maximum subscript as the array length minus 1 The data types stored in the arrays must be identical. Once the array has been created, the length cannot be changed. Length is obtained by the array object. Length. Arrays are also objects. Create an array format of

data type [] array variable = new data type [10];

The reference type array element holds the object reference. Arrays are divided into a set of arrays, two-dimensional arrays, and multidimensional arrays Javadoc is one of the three annotation methods in Java. Rational use of Javadoc is a good programming habit.

Java's understanding and basic syntax

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.