Basic--java Basic Syntax

Source: Internet
Author: User
Tags java se

First day

1.Linux:

Open source operating system, free, server-side

Directory (/root directory), mount point (directory), high

Command-based----PWD,LS,CD

Absolute path, relative path

2.java Operating Environment:

. Java, compiled, generates. Class bytecode files

The JVM loads the. Class and runs the. class

Cross-platform, one-time programming used everywhere

Jvm:java virtual Machines

Jre:java Run-time environment

Jre=jvm+java System Class Library

Jdk:java Development Kit

Jdk=jre+ compile, run and other tools

3.eclipse

There are two types of Linux:

1.------with graphical interface to facilitate your operation

2.----A real server operating system without a graphical interface

Windows--------Company is

Linux----------Java program is written and then provisioned to the server.

Complementary points of knowledge:

1.Linux directory Structure

The directory structure of the Windows operating system is in drive letter units, C, D, E, and so on, the data is stored under the various drive characters, and the Linux operating system on the top level only a root directory root, all files are stored under this root directory.

If the Windows operating system inserts an external device (USB stick, etc.), the system is more than a drive letter H, the operation of the H-drive is the operation of the external device. The Linux operating system is a subdirectory named MNT under the root directory, in this directory, a directory will appear, assuming that sdcard, known as the Mount point, the operation of it is the operation of external devices.

Windows operating system after a user login to the system, all files have Add and remove permissions to change, that is: You can operate any directory, assuming that the C disk under the Windows directory deletion, it will cause system anomalies, security is poor. Linux operating system to strict permissions requirements, the user login is not to all directories have additions and deletions permissions, the default current directory is the root directory under the home directory soft01, called the home directory, the directory has the highest permissions, the remaining directory files are generally read-only, can not be arbitrarily deleted, This guarantees security.

2. Java programs Follow the principles of compiling first and then executing them. First, the Java source program (. java file) is compiled into a Java bytecode (. class file) through the Javac command, and then the JVM is started by the Java command, which loads the. class file and runs the. class file.

3. The System class library is the Java pre-packaged functional class, just take it directly to use, can greatly improve the development efficiency.

4. The JVM is a software. It is a hardware machine that is "virtual" with software. Like a real hardware machine, this "virtual machine" defines details such as instruction sets, registers, and storage areas, while Java bytecode can be seen as a machine code that runs on this "virtual machine". It is because Sun defines the JVM specification, and different operating systems provide different JVM implementations, so that the same bytecode files can be run on different systems (of course, these systems must have a JVM for that system installed), so that Java wins the "compile, use everywhere" appellation. The JVM is implemented with local C, so the Linux system and the Windows system use different JVMs.

5. Terminology explanation JVM, JRE, JDK

The JVM, called the Java Virtual machine, is a specification for computing devices, a fictional computer that is implemented by simulating various computer functions on a real computer. The JVM masks information related to the operating system platform, enabling Java programs to run without modification on multiple platforms by generating only the target code (bytecode) that runs on the Java virtual machine. When the JVM executes the bytecode, it actually eventually interprets the bytecode as a machine instruction execution on a specific platform.

The JRE (Java SE runtime Environment) is called the Java SE Runtime environment and provides the software environment necessary to run Java applications, including Java virtual machines (JVMs) and Rich class libraries (Libraries). Running a Java application that has already been developed must install the JRE.

The JDK (Java Development Kit), called the Java SDK, is a superset of the JRE, or the JDK contains the JRE. In addition to all the content that contains the JRE, the JDK provides the compiler and debugging tools necessary to write Java programs. For Java developers, there is a general need to download the JDK, the current mainstream version is JDK 6.

6. Can be used under Linux system./eclipse start the execution program eclipse

Next day

1. Variables: The number of stored, pronouns, refers to the number of its existence

1) Statement:

int A; Declares an integer variable named a

int b,c,d; Declares three integer variables, named B,c,d

2) Name:

2.1) can only contain letters, numbers, _ and $ characters,

and cannot start with a number

2.2) strictly case-sensitive

2.3) cannot use keywords

2.4) can be named in Chinese, but not recommended

Suggestions: See the name of the idea, the hump named law

3) Initialization: First time assignment

3.1) Declare simultaneous initialization

int a=250;

3.2) first declaration after initialization

int A;

a=250;

4) Use:

4.1) must match the data type

int a=25.678; Compilation error, type mismatch

4.2) The operation of the variable is the operation of that number

int a=5,b=10;

int c=a+b; Remove 5 from A, plus 10 in B, assign to C

System.out.println (c); The number in output C is 15

4.3) variables must be declared and initialized before they are used

System.out.println (m); Compile error, variable not declared

int m;

System.out.println (m); Compile error, variable not initialized

int m=250;

System.out.println (m); That's right

2. Basic data type:

1) int: integer, 4 byte,-21 million to 21 multi billion

1.1) Integer Direct quantity defaults to int, if the range is out of compile error

1.2) divide two integers, the result is an integer, the decimal place is unconditionally discarded

1.3) If the int range is exceeded, the overflow----need to avoid

2) Long: Length, 8 bytes, very large very large

2.1) The direct amount of a long type is a number followed by L or L

2.2) If there is a possibility that the super range is set to the first number plus L

2.3) System.currenttimemillis ();

Gets the number of milliseconds since 1970.1.1 0 o'clock to this moment

3) Double: floating-point, 8-byte

3.1) Decimal Direct quantity default to double type

3.2) Double has rounding error and is not available for precise operation

4) Boolean: Boolean, 1-byte

4.1) Only values are true and false

5) Char: Character type, 2 bytes

5.1) in Unicode encoding format, each character corresponds to one code

ASCII code--' a ' (+) ' a ' (65) ' 0 ' (48)

5.2) characters should be placed in a pair of single quotes

Must Have and only one

5.3) Special symbols need to be escaped with \

3. Conversions between data types:

The type is small to large as follows:

Byte-short-int-long-float-double

char--

1) Two types of methods:

1.1) Automatic type conversion: From small type to large type

1.2) Coercion of type conversions: from large types to small types

Strong turn may overflow or loss of precision

2) Two-point rule:

2.1) Integer direct value can be assigned directly to Byte,short,char, but not over range

2.2) When the Byte,short,char type variable participates in the operation,

Convert to int before operation

Usage of 4.Scanner:

1) under the package:

Import Java.util.Scanner;

2) in main {}:

Scanner scan = new Scanner (system.in);

3) below the 2nd step:

int age = Scan.nextint ();

Double price = Scan.nextdouble ();

Additional knowledge:

1. Direct volume over-range--------compilation errors

Arithmetic operation over range------overflow (not error, bad phenomenon)

2. Calculate the time a program runs by code

Using the System.currenttimemillis () method provided by the JDK, the method returns the number of milliseconds that have elapsed from January 1, 1970 0 o'clock to this point, with a data type of long, which is often used for timing operations.

3. Char ascii=98;

SYSTEM.OUT.PRINTLN (ASCII);

The output of the above code is: b

The reason is that if you assign an int integer directly to a char type variable in the 0~65535 range, the int type integer is automatically treated as a char type.

4.. byte b1=10;

BYTE b2=20;

BYTE b3=b1+b2; The above code has a compile error on the third line because the B1+B2 result is of type int. convert int type result to byte type using coercion type conversion

Third Day

1. Operators:

1) arithmetic operator: +-*/%,++,--

2) relational operator: >,<,>=,<=,==,!=

3) logical operator: &&,| |,!

4) Assignment operator: =,+=,-=,*=,/=,%=

5) string concatenation: +

6) Three mesh operator: Boolean number 1: Number 2

2. Branching structure:

1) If structure: 1 paths

2) If...else structure: 2 roads

3) IF...ELSE If structure: multiple paths

4) Switch...case structure: Multiple paths

Advantages: High efficiency, clear structure

Cons: Integers, equals

Break: Jump out of switch

Add:

1. Use "+" for string connections

"+" In addition to the arithmetic operation, you can also implement the connection of strings, while the string and other data types can be "connected."

When the + sign acts on two numeric type variables, it is an arithmetic operation.

When a variable on either side of the + sign has a string type, which is "", it is a string connection, and the concatenated result is a string type

2. Java syntax specifies that "{}" can be omitted when only one statement is included in the IF statement block, but the IF statement block may only act on one of the statements below it. Given the readability and extensibility of the code, it is recommended that you do not omit "{}" even if there is only one statement in the IF statement block.

3. switch is the integer constant value after the case as the entry, and if the value is equal, start executing the statement following it.

There are two issues to be aware of when using switch, first, the constant value after the case must be different, and second, the value of the integer expression after switch must be integer or character type

4.1. Arithmetic operator: +,-, *,/,%,++,--

1)%: residual, modulus----remainder of 0 is divisible

2) ++/--: self-increment 1/auto minus 1, can be placed before the variable can also be placed after the variable

2.1) When used alone, there is no difference before and after

2.2) When used, there is a difference in the previous

2.2.1) a++ value of a

2.2.2) The value of ++a is a+1

Fourth day

1. Loops: Repeatedly executing the same or similar code

2. Cyclic three elements:

1) Initialization of cyclic variables

2) Conditions of the loop (based on the cyclic variable)

3) changes in cyclic variables (toward the end of the cycle)

Loop variable: The amount of change in the loop

3. Loop structure:

1) While: first judgment after execution, it is possible not to execute at once

2) Do...while: First execution after judgment, at least one execution

The 1th element is preferred with the 3rd element

3) for: Highest application rate

Add

    1. Randomly generates random numbers between 0 and 99 math.random () *100

Math.random ()--------0.0 to 0.99999999 .....

    1. public static void Main (string[] args) {

int count=0;

while (COUNT<5);

{

System.out.print (count+ "");

count++;

}

}

This code will form a dead loop.

At first glance, there is no problem with this snippet, but a closer look at the program, it is not difficult to find the loop condition expression of the while loop followed by a semicolon. In Java, a separate semicolon represents an empty statement that does nothing, which means that the loop body of the while loop is an empty statement. An empty statement is not a big problem as a loop body, the problem is that when Java executes the loop repeatedly, there is no change in the return value of the loop condition, which forms a dead loop. The code block after the semicolon has no relation to the while loop.

3. Scope/range of variables:

Start with the declaration of the variable, and end with the curly brace containing its nearest

The. Variable has the same name problem:

Variable cannot have the same name when scope overlap

Fifth day

1. Better application of three cycle structures:

1) while: "When ..." loop

2) Do...while: "Until ..." loop

The 1th element is preferred with the 3rd element

3) for: fixed number of cycles

2. Nested loops:

1) loop in the loop, generally multi-row multi-column when used

Outer control row, Inner control column

2) The execution process: The outer loop walks once, the inner layer circulates all times

3) The less cycle level, the better, can use a layer of two layers, can use two layers without three layers

If the requirement must be more than 3 levels to achieve, the design has a problem

4) Break can only jump out of a layer of loops

3. program = algorithm + data structure

Algorithm: Process/step to solve the problem (order, branch, loop ...)

Data structure: Save the information according to a specific structure

Number How to save

Well-designed data structures can lead to good algorithms

4. Arrays:

1) Collection of elements of the same data type

2) An array is a data type (reference type)

3) Definition/Declaration of the array:

int[] arr = new INT[4];

4) Initialization of the array:

int[] arr = new INT[3]; 0,0,0

Int[] arr = {1,3,5}; 1,3,5

int[] arr = new int[]{1,3,5}; 1,3,5

Int[] arr;

arr = {1,3,5}; Compile error

arr = new int[]{1,3,5}; That's right

5) Array of access:

5.1) Get the length of the array by. length

int[] arr = new INT[4];

System.out.println (arr.length); 4

5.2) access to elements in the array by subscript/index

Subscript starting from 0, Max to. length-1

int[] arr = new INT[3];

ARR[0] = 100; Assigns a value of 100 to the first element in arr

ARR[1] = 200; Assigns a value of 200 to the second element in Arr

ARR[2] = 300; Assigns a value of 300 to the third element in Arr

ARR[3] = 400; Abnormal

System.out.println (Arr[arr.length-1]); Output last Element

6) Array Traversal:

int[] arr = new INT[4];

for (int i=0;i<arr.length;i++) {

Arr[i] = 100;

}

for (int i=0;i<arr.length;i++) {

System.out.println (Arr[i]);

}

for (int i=arr.length-1;i>=0;i--) {

System.out.println (Arr[i]);

}

7) Copying of arrays:

System.arraycopy (a,1,a1,0,4);

int[] A1 = arrays.copyof (a,6);

A = arrays.copyof (a,a.length+1);

8) sort the array:

Arrays.sort (arr); Ascending row

Bubble sort

Add:

1. Bubbling principle:

1) 4 counts 3 rounds

2) Each round is starting from a 1th element

Every time, it's more than the next element.

3) The number that comes out is not playing with it.

2. Pascal's father Nicklaus Wirth said a word and won the Turing Award, this is a classic word is, the program is: algorithm + data structure, so-called data structure, simple to say is to the specific structure to save information, design reasonable data structure is the precondition to solve the problem.

3. The array is a collection of elements of the same data type, the array elements are arranged in a linear order, the so-called linear Order refers to the first element, each element has a unique precursor element, in addition to the last element, each element has a unique successor ("one with One"), Each element can be accessed by the ordinal number (subscript) of the element's location (subscript starting from 0, Max to number of elements-1)

4. The key points for defining an array of primitive types include:

Exact data type: used to open space size

Whole array Name: a reference to the data

Cannot be missing "[]"

Note that the New keyword is used when defining an array, and it is precisely because of the new statement that the array is assigned to a specified size space (explained in detail later).

5. When declaring an array, int[] arr and int arr [] are both available. Common way for int[] arr.

6. When declaring an array, you do not specify the length of the array (you can see that only int[] is specified at the time of the declaration, no length specified), and the new keyword allocates space by specifying the amount of space allocated (new INT[10]).

7. Summarize the characteristics of the arrays.copyof () method as shown in the following table:

The resulting new array is a copy of the original array;

Newlength Small source array, then intercept; (oneself through the code to demonstrate the effect);

Newlength is larger than the source array, it is filled with 0 or null;

Sixth day

1. Method:

1) for encapsulating a specific logic function

2) The method should be as independent as possible, one way only to do a thing

3) method can be repeatedly called multiple times

4) Clear structure, easy to maintain and facilitate collaborative development

2. Definition of the method:

Modifier return value type method name (parameter list) {

Method body

}

3. Invocation of the method:

1) No return value:

Method name (with reference to the parameter);

2) There is a return value:

Data type variable = method name (with reference to parameter);

4.return:

1) return; End Execution (void method)

2) return value; End execution and return result (with return value method)

Basic--java Basic Syntax

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.