Development Environment setup and basic syntax

Source: Internet
Author: User

Today is xiaoxiaocoder's first Technical Review blog .. I have summarized the title based on my own code for more than a year. I want to use this title to express the topic of each article, in this way, both the summary of myself and the learning of new development friends are very helpful.

I. Development Environment Construction

To learn about Java, we should first set up the environment. The first task of Java Development is to configure the environment variables and set up the environment. Although JDK and later do not need to be changed, however, many other tools still need to be manually configured when JDK is used. so be familiar with it. in fact, there are only a few, just three, right click on "My Computer"-> "property"-> "advanced"->
"Environment variable (n )"

1. Create the system variable java_home: JDK root directory

2. Create the system variable classpath:.; % java_home % \ Lib; (Note: The dot number indicates the current directory and cannot be omitted)

3. Add the following content before the path value of the system variable: % java_home % \ bin; (Note: The semicolon here cannot be omitted)

After the configuration is complete, Enter cmd and press Enter. If Java-version is correctly configured, the current JDK version will be output.


Naming Conventions of Java
1. All letters in the package name are in lower case. For example, xxxyyyzzz.
2. The class name and interface name should use nouns. the first letter of each word is capitalized. For example, xxxyyyzzz.
3. method name. The first word is in lower case, and the first letter of each word is in upper case. For example, xxxyyyzzz.
4. variable name. The first word is in lowercase, and the first letter of each word is in uppercase.
5. Each letter in the constant name is capitalized. For example, xxxyyyzzz.

Ii. Basic syntax

Java basic syntax (1) -- Data Type
Java is a strictly typed language. This means that each variable must have a declared type. Java provides eight basic types. Six numeric types (four integers, two floating point types), one character type, and one boolean type. Java also provides large numeric objects, but it is not a Java data type.
1. Integer:
Definition: A number without decimal digits. A negative number is allowed.
Type: Java provides four integer types:
Int 4 bytes-2,147,483,648 to 2,147,483,647
Short 2 bytes-32,768 to 32,767
Long 8 bytes-9,223,372,036,854,775,808 L to 9,223,372,036,854,775,807 L
Byte 2 bytes-128 to 127
2. Floating Point Number:
Definition: number that contains the decimal part.
Category: Java provides two floating point numbers:
Float 4 bytes +-3.40282347e + 38f (6 ~ 7 valid decimal digits)
Double 8 bytes +-1.79769313486231570e + 308 (15 valid digits)
Note:
1) The float type value has a suffix F. If there is no suffix F, the default value is double. The suffix D can also be used for values of the double type.
2) When these numbers encounter a value range error, it will occur (overflow), and underflow will occur when the image is divided by zero ).
3. Character Type:
Definition: single quotes are used to represent char constants.
Note:
1) double quotation marks indicate a string, which is an object of Java and is not a data type.
2) the char type indicates the characters in the Unicode encoding scheme.
Unicode can contain 65536 characters at the same time, and ASCII/ANSI can contain only 255 characters. It is actually a subset of Unicode. Unicode characters are usually expressed in hexadecimal encoding. The range is between '\ u0000' and '\ uff. \ U0000 to \ u00ff represents ASCII/ANSI characters. \ U indicates that this is a unicode value.
3) in Java, in addition to representing characters in the \ U format, you can also use the code-changing sequence to represent special characters.
\ B backspace \ u0008
\ T tab tabulation \ u0009
\ N newline \ u000a
\ R hard press enter \ u000d
\ "Double quotation marks \ u0022
\ 'Single quotes \ u0027
\ Backslash \ u005c
4) theoretically, Unicode characters are used in Java applications and small applications, but whether they can be truly displayed depends on the browser and operating system used, the operating system is the most fundamental.
4. boolean type:
The boolean type has only two values: false and true. Friends who have used other programming languages must be familiar with them.
  
Java basic syntax (2) -- variables and constants
1. variables:
Definition: I believe that the definition of it should not be mentioned :) everyone should be familiar with it.
For example, a, A1, and name are all valid variables.
Note:
1) Java requires that the type of the variable be declared before a variable is used.
2) the declaration of a variable in Java is a complete Java statement, so a semicolon should be used at the end.
3) Naming rules for variables:
The variable must start with a letter.
Variable names are any combination of letters or digits.
In Java, letters indicate any character in UNICODE, which is equivalent to a letter.
A digit also contains any UNICODE character other than 0-9 that is equal to a digit.
+ The copyright information symbol Circle C and space cannot be used in the variable name.
Variable names are case sensitive.
The length of the variable name is basically unlimited.
If you want to know which Unicode characters are considered to be letters in Java, you can use the isjavaidentifierstart and isjavaidentifierpart methods in the character class to check.
Java reserved words cannot be used in variable names.
4) multiple variables can be declared in one statement. Different variables are separated by commas.
2. Variable assignment and initialization:
The value of a variable can be obtained in two ways. One is a value assignment. To assign a value to a variable, a value assignment statement is required. Another method is initialization, Which is initialization. It is actually a value assignment statement, but this value assignment statement is completed together when the variable is declared.
For example:
Int A = 10; // This is the initialization process of a variable.
The functions of the following two statements are the same as those of the preceding statement, except that the declaration and value assignment of variables are separated here.
Int;
A = 10; // it should end with a semicolon at the end of the value assignment statement.
Note:
1) there is absolutely no uninitialized variable in Java. You must assign a value to the variable before using it.
2) declarations can appear at any position in the code, but can be declared only once for a variable in any code block of the method.
3. constants:
Definition: a constant is a constant.
For example:
1. "Hello"
Note:
1) Use the final keyword in Java to define a constant.
Int final A = 10; // declares an integer constant A whose value is 10.
2) The constant name is often capitalized.
  
Java basic syntax (3) -- operator and type conversion
1. operators:
1) in the Java language, common operators include: +,-, *,/. You must be familiar with them.
2) power: Java does not have a power operator and must use the math. Pow (X, A) method to represent the power a of X. Both parameters of the POW method belong to the double type, and the returned value is also of the double type.
For example:
Double A = math. Pow (); // defines a variable A, whose value is the power of 10.
3) Increment and decrease operators:
A ++, a -- use the value of the variable first, and then increment or decrease.
+ A, -- a increases or decreases first and then uses the value of the variable.
4) relational and boolean operators:
Relational operators: = ,! =, <,>, <=,> =
Boolean operator: & (and), | (OR ),! (No)
5) bitwise OPERATOR:
& (Plus), | (OR), ^ (same or ),~ (NO),> shift to the right, <(shift to the left), >>> (fill the position on the top with zero)
Note that Java does not have the <operator.
6) brackets and operator classification:
In Java, the default calculation sequence of operators is:
[], () (Method call) from left to right
! ~ ++ -- + (One dollar)-(one dollar) (SHAPE) New from right to left
+/% Left to right
+-Left to right
<>>>> Left to right
<<=>=> Instanceof from left to right
=! = Left to right
& Left to right
^ Left to right
| Left to right
& Left to right
| Left to right
? : Left to right
= + =-= * =/= & =|=^= <<=>=.> = From right to left
In Java, brackets can be used to specify the desired computing sequence, thus changing the default Operation Sequence of operators.
2. Mutual conversion of value types:
When data of different data types is involved in calculation, the conversion of different data types is involved:
The conversion of data types in Java follows the following rules:
If one of the calculation objects is of the double type, the other is also converted to the double type.
Otherwise, if one of the calculation objects is of the float type, the other will be converted to the float type.
Otherwise, if one of the calculation objects is of the long type, the other will be converted to the long type.
Otherwise, if one of the calculation objects is of the int type, the other will be converted to the int type.
Otherwise, if one of the calculation objects is of the short type, the other will be converted to the short type.
Type conversion can also be achieved through "Styling". The syntax is very simple. Place the target type in brackets, and then keep up with a variable name.
For example:
Double X = 9.99;
Int Nx = (INT) x; // in this case, NX is an int type data, and its value is 9. The subsequent parts are lost in the modeling process. You can use the math. Round method to insert a floating point into the nearest integer. After you use the math. Round method, you may also need to use a shape for type conversion.
Java also allows you to assign one type of variable value to another without displaying the shape. You can also perform some specific value assignment conversions, including:
Buty-> short-> int _> long-> float-> double char-> int
When values are assigned, they are converted from left to right in the above Order.
  
Java basic syntax (4) -- string and array
1. String:
Java does not provide built-in string types, but the standard Java library contains a predefined class, which is naturally called "string". Each string enclosed by double quotation marks is an instance of string.
Example: "ABC", "hello"
Java allows the use of "+" to connect two strings. When a string is connected to a non-string value, the latter is converted to a string. This feature is often used in output statements.
Substring: Use the substring method provided by the string class to extract a substring from a large string.
In Java, the position of the first character in the string is 0,
You can use the length method to obtain the length of a string.
You can use charat (n) to extract characters at position n.
You can use the equals method to determine whether two strings are equal. To check whether two strings are equal and ignore the differences between uppercase and lowercase letters, use the equalsignorecase method.
Note: you cannot use = to determine whether two string variables are equal.
2. array: in Java, arrays are the first type of objects. After an array is created, its size cannot be changed easily. When you try to access any element outside the boundary of the array declaration, the program runs and stops, but you can run the command during compilation.
Java creates an array object and provides the abbreviated form of initialization values:
Int [] smallprimes = {2, 3, 4, 5, 6 };
You can also Initialize an "anonymous array ":
New int [] {2, 3, 4, 5, 6 };
When an array is used as a method parameter and you do not want to create a local array variable to save the array, you can use the anonymous array method.
If you want to know the number of elements contained in an array, you can use the arrayname. length method.
Array replication: You can copy one array variable to another, but both variables reference the same array. You can use
System. arraycopy (from, fromindex, to, toindex, count); method.
Array is used as a parameter: array in Java is used as a parameter, and the reference method is used, that is, the address of the passed array, changing the value of the array element in the method can change the value of the source array element.
Array as the return value: A method can return an array.
Multi-dimensional array:
A two-dimensional array can be defined in Java, but there is no multi-dimensional array. Multi-dimensional arrays are actually implemented through arrays.
Example of an array:
Int [];
A = new int [10];
For (short I = 1; I <= 10; I ++)
{
System. Out. println (A [I-1] + "square:" + A [I-1] * A [I-1]);
}
  
Java basic syntax (5)-Control Process
1. Control Process:
The control flow is provided by any programming language. The control flow provided by Java is basically the same as that provided by many other programming languages.
1) Condition Statement:
If (condition) statement;
If (condition) {code block}
If (condition) Statement; else statement;
If (condition) {code block} else {code block}
2) Uncertainty loop:
While (condition) {code block}
Do {code block} while (condition );
3) determine the cycle:
For (INT I = 1; I <= 10; I ++) {code block}
After a variable is declared at the first position of the for statement, the scope of the variable will be extended to the end of the For Loop body. This variable cannot be used outside the loop.
4) multiple cycles:
Switch (choice)
{
Case 1:
Comment 1;
Break;
Case 2:
Comment 2;
Break;
Default:
Comment 3;
Break;
}
The default clause is optional. The switch statement can only test the char type or Integer type other than long.
5) Tag interruption:
Use the label interrupt before a loop and use a colon after the label, and then use the "break tag" in the loop body if you want to exit the loop; to exit the loop and start executing the statement after the loop body.


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.