Getting Started with Java--(1) Basics of Java programming

Source: Internet
Author: User

Getting Started with Java--(1) Java Programming Basics Chapter II basic Java code format: Modifier class class Name {program code} 2.1 keyword: A word that gives special meaning. 2.2 Identifier: Some names that are defined in the program. For example: class name A-Z 0-9 _ $ number can not start, you can not use keywords, the general first letter uppercase. 2.3 Note: The text of the annotation description program.         Role: 1, the program to explain. 2, debugging procedures. Note: Multiline comments can be nested single-line comments, but cannot nest multiple lines of comments. /* Multiline Comment *//** document comment *///single-line comment/* Requirements: Write a Hello World program that will display the Hello Word screen ideas:
    1. Find the compiler
    2. Write a program with the Java Voice
    3. This program needs to be running and has the function of displaying.
Steps:
    1. Defines a class that is completed by class.
    2. The main method is required for this class to run.
    3. Display functions can be implemented through output statements.
Code: Test. *///1, define classes with class. The main method is defined in class DEMO2{//2, classes. public static void Main (string[] args) {//3, the output statement is defined in the main method. System.out.println ("Hello World");}} 2.4 Constants and variable constants represent values that cannot be changed. ' A ' can contain only one character; the usual escape character for the "Bad" string: 1, \ r for carriage return, positioning the cursor at the beginning of the current line, and not jumping to the next line. 2, \ n represents a newline character, and wraps to the beginning of the next line. 3, \ t represents a tab, moves the cursor to the next tab position, as if you were using the TAB key in the document. 4. \b Represents the backspace symbol, just like the backspace key on the keyboard. \ ': Single quotation mark \ ': double quotation marks \ \: Backslash   variable concept: A storage area in memory the area has its own name (variable name) and type (data type) the data for that area can change over the same type of range   Why define variables: Constants that are used to keep the same type, and can be reused   Use variables Note: The scope of the variable (valid between a pair of {}) The initial value defines the format of the variable: data type variable name = initial value Note: Format is fixed, remember format, Status quo.  java language is strongly-typed speech, for each kind of data has defined specific data types, in memory allocated a different size of memory space data type: 1, the basic data type: ① numeric type: integer type (byte8 bit, short 16 bits, int normal 32 bits, Long 64-bit) floating-point type (float single precision, double doubles) ② character: (char) ③ Boolean (Boolean) 2, reference data type: ① Class (Class) ② interface (interface) ③ array ([])   Operators in Java: ① in the arithmetic of self-increment + + and decrement--if the operator + + or--is placed in front of the operand, the self-increment or decrement operation is performed first, and then other operations are performed. Conversely, if the operator is placed after the operand, it is preceded by an additional operation followed by a self-increment or decrement operation. such as: int a = 1;int b=2;int x =a+b++; System.out.println ("b=" +b); System.out.println ("x=" +x); Operation step: 1, x=a+b=1+2=3; 2, b=b++=2+1=3; if you change int x=a+b++ to int x = ++a+b, the steps are: 1, a=++a=1+1=2; 2, x = A+b=2+2=4.② when the division operation, when the divisor and dividend are integers, the resulting result is also an integer. If a decimal is involved, the resulting result will be a decimal. ③ in the modulo (%) operation, the positive or negative of the result of the operation depends on the symbol of the modulus (the number of the left of the%), regardless of the symbol (the number on the right of the%), such as (-5)%3=-2,5% (-2) = 2. Table-Assignment operators
operator Assignment example result
= assignment a=3;b=2; a=3;b=2;
+ = plus equals a=3;b=2;a+=b; a=5;b=2;
-= minus equals a=3;b=2;a-=b; a=1;b=2;
*= multiply equals a=3;b=2;a*=b; a=6;b=2;
/= except equals a=3;b=2;a/=b; a=1;b=2;
% = modulo equals a=3;b=2;a%=b; a=1;b=2;
Note: Java is also assigned a value of int x,y,z;x=y=z=5;//to three variables at the same time. Error notation: int x=y=z=5; The two types of logic are basically the same as those for (&& and &), with two logical OR (| | and |) are basically the same arithmetic rules. & and | Arithmetic is to calculate all the logical expressions, while && (short-circuit and) and | | (short-circuit or) operation has a short-circuit calculation function. For &, if the left condition is false, the value of the right condition is also computed, and for &&, if the condition on the left is false, the condition on the right is not evaluated, which is known as a short-circuit phenomenon. The so-called short-circuit calculation, refers to the system from left to right logical expression calculation, once the calculation results have been determined, the calculation process is terminated. For the && operation, the end result is false, regardless of whether the value of the operator's right side is true or false, as long as the value of the left side of the operator is false. Therefore, once the system determines that the value of the left side of the && operator is false, the system terminates the subsequent calculation process; Operation, as long as the value of the left side of the operator is true, the end result is true regardless of whether the right side of the operator is true or false. So, once the system is judged | | The value of the left side of the operator is true, then the system terminates the subsequent evaluation process. Note: The difference or the same is false, and the opposite is true. The operator ^ represents an XOR operation, and the result is False when the Boolean value on either side of the operator is the same (both true or false). When both sides of the Boolean value are different, the result is true. What is the function of bitwise arithmetic?

Bit Operation example

Operator

Name

Example

Description

&

Bit and

X&y

The X and Y bitwise

|

Bit or

X|y

make x and Y bitwise OR

~

-Bit non-

~x

The X-Bitwise

^

Bit XOR or

X^y

make x and Y bitwise different OR

>>

Move right

X>>y

Move the x to the right Y-position.

<<

Move left

X<<y

Shift the X to the left Y position.

>>>

Move right

X>>>y

Shift x to the right Y-position, 0 on the left.

The sign bit of positive number is 0, and the sign bit of negative number is 1.   Moving a number to the left << causes the value to be multiplied by the power of 2.        Move a number right >> "causes the value to be divided by a power of 2. The right shift (fill 0) operator, the unsigned right shift, >>> never produces a minus sign because its sign bit is always 0. Whether the moved number is positive or negative, the left-hand portion is 0. A special ternary operation in Java and the IF ... else statement is similar in format: judging the condition? Expression 1: Expression 2 such as:/*int Max; if (x > y) {max = x;} else {max = y;} *///equal to the following sentence. int max=x>y? x:y; For (initialize expression ①; loop condition ②; action expression ④) {EXECUTE statement ③} Java declares a method with the specific syntax format as follows: Modifier returns a value type method name ([argument type parameter name 1, argument type parameter Name 2, ...] {EXECUTE statement ... return value;} If the return value type is void, the return statement can be omitted. The overloads of a method are independent of the return value type, which only needs to satisfy two conditions, one is the same as the method name, the other is the number of arguments, or the parameter type is different.  Recursive method refers to the process of invoking itself within a method, and recursion must have an end condition. Java defines an array format: int [] x = new int [100]; Declare a variable of type int [] and create an array of length 100. The length of the array can be "array name. Length" To get the length of the array, that is, the number of elements. How multidimensional arrays are expressed:
    1. int [] [] arr = new int [3][4];
    2. int [] [] arr = new int [3][];
    3. int [] [] arr = {{1,2},{3,4,5,6},{7,8,9}};
The continue statement, in the Loop statement, is the function of terminating the loop and performing the next loop. The break statement in the Loop statement, the role is to jump out of the loop statement, the execution of the following code, if it appears in the loop nesting, only jump out of the inner loop, want to jump out of the outer loop, you need to loop outside to mark.

Getting Started with Java--(1) Basics of Java programming

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.