According to the Java lesson that I took,I’ll divide my notes into 3 parts:basic,medium and advanced.So this is the first section of part 1.
Keyword
These words have special purposes in Java,each one has special meaning to the Java compiler,also we need to notice that they are case sensitive.
For example1
2void // this is a keyword
Void // this isn't a keyword
And here are part of keywords that we usually use:
abstract | boolean | break | byte | case | catch |
---|---|---|---|---|---|
char | class | continue | default | do | double |
else | extends | false | final | finally | float |
for | if | implements | import | instanceof | int |
interface | long | native | new | null | package |
private | protected | synchronized | this | throw | throws |
transient | true | try | void | volatile | while |
Pratice1
2
3
4
5public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World");
}
}
Identifier
It’s a symbol that we use to name variable,class,method etc.
When we use identifier,there are some rules need to be followed:
- A identifier can consists of letter,number,_,$,and can’t starts with number.
- Identifier can’t be Java keyword or reserved word.
- Identifier is case sensitive.
- Naming a identifier better can inform its meaning or purpose.
Variable
We can see it as a box,we can put anything we want into the box,and replace it with new item at any time.
In Java,we use 3 elements to describe variable:type,name and value.
Also we need to follow rules like:
- A variable name can consists of letter,number,_,$,and can’t starts with number.
- Variable name can’t be Java keyword or reserved word.
- Variable name is case sensitive.
- Naming a variable name better can inform its meaning or purpose.
In order to form a better program habit,there are several naming methods:
Practice1
2
3
4
5
6public class HelloWorld{
public static void main(String[] args) {
String hobby = "football"; // we declare a variable
System.out.println("My hobby is" + hobby); // we bind those two together with '+',and print it out
}
}
Data type
Java is a strong typing programming language,it means every data in Java has its own type,and it must be define when Java compiles,want more details click here
Practice1
2
3
4
5
6
7
8
9
10
11
12
13
14public class HelloWorld{
public static void main(String[] args) {
String name="Tom";
char sex='male';
int age=18;
double price=120.5;
boolean isOK=true;
System.out.println(name);
System.out.println(sex);
System.out.println(num);
System.out.println(price);
System.out.println(isOK);
}
}
Rules of using variables
Variables must declare before using it.
1
2
3
4
5
6public class HelloWorld{
public static void main(String[] args) {
String name="Tom";
System.out.println(name);
}
}You can declare and initialize variables in the same time.
1
String name = "Tom";
Also you can first declareit,and then assign it afterward.1
2String name;
name = "Tom";
You can only assign one value to a variable,but you can change multiple time.
1
2String name = "Tom";
name = "Jack";The variables that you have declared in method main must be assigned first,and then you can output it.
1
2
3
4
5
6public class HelloWorld{
public static void main(String[] args) {
String name="Tom";
System.out.println(name);
}
}Although you might not have error shows up,but in real life development,I suggest only using english when you naming variables.It’s easier to have safety issues if you use other languages,such as having gibberish when you are working on cross platform.
Auto type conversion
In our program,we usually need to convert between differnet primitive data types of data.For example:1
2
3int num1 = 82;
double num2 = num1;
System.out.println(num2);
This kind of conversion is called auto type conversion.Of course,it needs to match particular antecedents:
- Target type is compatible with source type,like double is compatible with int,but char isn’t compatible with int.
- Target type must larger than source type.
Forced type conversion
Although auto type conversion is pretty convenient,but it can’t match all of our programming needs.So we need something called forced type conversion.
- syntax: (target type)value
1 | double num1 = 75.8; |
and then you’ll have1
75
You can see ,it just simply remove everything to the right of the decimal point(including decimal point itself).
Therefore,it’s pretty clear now,using forced type conversion might cause data lost,you need to be careful when you using it.
Constant
We see it as a kind of special variable,when its value has been assigned,it doesn’t allow to change during the operation of your program.
- syntax: final type name = value;
1 | final String NAME = "Tom"; |
Using constant in your program can improve maintainability,and notice we normally use capital letter while naming a constant.
Annotation
When we are coding,usually need to add some notes attach to the source code,in order to describe its purpose.It’s a important thing when we talk about coding.
There are three kinds of annotation:
1
2
3
4
5/**
*
*@authur Seraphcex
*@version v1.0
*/
1
2
3
4/*
*this can include multiple lines
*
*
1
//single line
Annotation won’t be executed during the operation,more details click here.