java-based (a) - Laboratory Building

An identifier: Case
of numbers, uppercase and lowercase letters, $, _ composition, not begin with a number.
Sequence of characters used when naming variables, methods, classes, and other factors known.
Wherever they can from the name of the call identifier.
Keywords and reserved words can not be used, but can contain keywords and reserved words
strictly case-sensitive, unlimited length.
Identifiers can not contain spaces. And do not appear in Chinese pinyin name (Some of these methods have named another English alphabet, would seem neither fish nor fowl)
try to make sense, "see the name to know Italian."

Second, the naming convention:
Project name:
1. If there is only one word to all lowercase
more than 2. Two words to use "-" separated, lowercase

Package name:
multi-word in all lowercase letters: xxxyyyzzz
.... Rule can press [reverse domain name] [team name] [item name] [a block] [two modules] .. [N-level modules] . [classes] format to be divided, if more features, more detailed package can be divided
if it is organized, I just use org.xxx.xxx
if the company's project, to use com. company name project name


Class name, interface name:
multi-word, the first letter of all words capitalized: XxxYyyZzz
functional class specific keywords can be used as a marker:
to achieve by the end of the class impl: Interface + impl
abstract class uses the Abstract as the beginning of the class named
Exception class name to use "Exception" as the class name at the end of
unit test classes using the "test" as of the end of the class name

Variable names, method names:
multi-word, lowercase first letter of the first word, the second word began to capitalize the first letter of each word: xxxYyyZzz

Constant name:
All the letters are capitalized. When many words each word with an underscore: XXX_YYY_ZZZ

 

Third, the variable
variable storage means is named values present in a computer memory where
variables typically may be modified, i.e., it can be used to represent a state variable.
Program to change the state of the entire program by changing the value of the variable. In order to facilitate the use of variables, variables needs to be named, called the variable name.
In Java, variables need to declare (declare) to use. In a statement, indicating the type of a variable, the variable given special names, so call it in the back of the program.
Declare variables can be used anywhere in the program, the syntax is as follows:
Data type variable name;
example:
int = A. 1;
in this syntax, the data type can be any type of Java language, such as int. Variable name is an identifier of the variable, the need to follow the naming rule identifier, spaced by spaces, using data between variable names and types; as the end.


Fourth, the constant

The program is running on behalf of the constants can not change the value. We can also understand them as special variables, but they are in the process of running the program is not allowed to change. Value of the constant can not be modified.

The final keyword in Java can be used to declare properties (constants), methods and classes. When the final modification attribute, the attribute representative of once allocated memory space must be initialized, which means "this is not altered" or "end state." Add a keyword to declare a constant final in front of the variable. In Java coding specifications required constant name must be capitalized.

Syntax:

name = data type constant final value;
for example:

final double PI = 3.14;
constants can be declared, then the assignment, but can only be assigned once, such as:
Final int FINAL_VARIABLE; FINAL_VARIABLE = 100;

Fifth, data types

8 large basic data types:
numeric:
integer byte, short, int, long
float float, double
Boolean: boolean
character: char


Reference Type:
Class class is a Class String String []
The interface to
the array

Sixth, the code Exercise
1,

```
public class VarTest{
public void main(String args[]){
System.out.println("Define a variable 'a' is ");
int a ;//声明变量a
a = 5;
System.out.println(a);
}
}
```

shiyanlou:project/ $ javac VarTest.java [18:27:36]
shiyanlou:project/ $ java VarTest [18:33:47]
Error: Main method is not static in class VarTest, please define the main method as:
public static void main(String[] args)

 


2、

```
public class VarTest{
public static void main(String args[]){
System.out.println("Define a variable a is ");
int a ;//声明变量a
a = 5;
System.out.println(a);
}
}
```

shiyanlou:project/ $ javac VarTest.java [18:33:54]
shiyanlou:project/ $ java VarTest [18:34:35]
Define a variable a is
5


3、

```
public class VarTest{
public static void main(String args[]){
System.out.println("Define a variable "a" is ");
int a ;//声明变量a
a = 5;
System.out.println(a);
}
}
```

shiyanlou:project/ $ javac VarTest.java [18:34:40]
VarTest.java:3: error: ')' expected
System.out.println("Define a variable "a" is ");
^
VarTest.java:3: error: not a statement
System.out.println("Define a variable "a" is ");
^
VarTest.java:3: error: ';' expected
System.out.println("Define a variable "a" is ");
^
3 errors


4、

```
public class VarTest{
public static void main(String args[]){
System.out.println("Define a variable 'a' is ");
int a ;//声明变量a
a = 5;
System.out.println(a);
}
}
```


shiyanlou:project/ $ javac VarTest.java [18:35:19]
shiyanlou:project/ $ java VarTest [18:35:49]
Define a variable 'a' is
5

5、
```
public class FinalVar(){
public static void main(String args[]){
final int b;
b=100;
System.out.println(b);
}


}
```

shiyanlou: project / $ Javac FinalVar.java [18:42:33]
FinalVar.java:1 error: '{' expected
public class FinalVar () {
^
1 error
shiyanlou: project / $ Javac FinalVar.java [18:58 : 25]
shiyanlou: project / $ java FinalVar [18:58:54]
100

 

Guess you like

Origin www.cnblogs.com/cainiaoputeng/p/11322385.html