Java基础第二讲:Java基本语法(一)
如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!
Java基础第二讲:Java基本语法(一)



- 必须以字母、美元符号或下划线开头。数字不能开头
- 第一个字符之后可以是任意长度的包含数字、字母、美元符号、下划线的任意组合。
- 不能使用Java关键字和保留字做标识符
- 标识符是大小写敏感的,Z和z是两个不同的标识符。
- Unicode字符会被视为普通字母对待。

1 |
public class Lesson02{ |
2 |
public static void main(String[] args){ |
3 |
String 世界 = "阿凡达!" ; |
4 |
System.out.println(世界); |
5 |
} |
6 |
} |
abstract | continue | for | new | switch |
assert | default | goto | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient 瞬间 |
catch | extentds | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const | float | native | super | while |
true 布尔字面值 | false 布尔字面值 | null 空值字面值 |
1 |
public class Lesson02{ |
2 |
public static void main(String[] args){ |
3 |
String myName = "nabula" ; |
4 |
myName = "nebulayao" ; |
5 |
System.out.println(myName); |
6 |
} |
7 |
} |
01 |
// 游戏方向设定 北 南 东 西 |
02 |
final int NORTH = 1 ; |
03 |
final int SOUTH = 2 ; |
04 |
final int EAST = 3 ; |
05 |
final int WEST = 4 ; |
06 |
07 |
// 三种游戏元素 |
08 |
final int RED_STAR = 1 ; |
09 |
final int YELLOW_STAR = 2 ; |
10 |
final int GREEN_STAR = 3 ; |
1 |
public class Lesson02{ |
2 |
public static void main(String[] args){ |
3 |
final int SOUTH = 2 ; |
4 |
SOUTH = 1 ; |
5 |
} |
6 |
} |


1 |
42 //整数字面值 |
2 |
false //布尔字面值 |
3 |
3.1415 //double字面值 |
4 |
'b' //char字面值 |

01 |
public class Lesson02{ |
02 |
public static void main(String[] args){ |
03 |
int i= 10 ; //十进制直接写 |
04 |
int j= 012 ; //八进制前面加0,八进制用0-7表示 |
05 |
int k= 0xa ; //十六进制前面加0x或者0X,16进制用 0-9 a-f 表示,为什么用 a-f,因为我们没有发明过 10-16的数字符号,这里的a-f x大小写都可以 |
06 |
int cafe = 0xcafe ; //你觉得这个咖啡会等于几? |
07 |
System.out.println(i); |
08 |
System.out.println(j); |
09 |
System.out.println(k); |
10 |
System.out.println(cafe); |
11 |
} |
12 |
} |
类型 | 字节 | 范围 | 举例 |
byte | 1 | -128 ~ 127 | 125 |
short | 2 | -32768 ~ 32767 | 20000 |
int | 4 | -2147483648 ~ 2147483647 | 123456789,2123456789 |
long | 8 | -9223372036854775808~9223372036854775807 | 9876543210L |
01 |
public class Lesson02{ |
02 |
public static void main(String[] args){ |
03 |
byte a= 127 ; |
04 |
short b= 128 ; |
05 |
int c= 2123456789 ; |
06 |
long d=9876543210l; |
07 |
long e=9876543210L; |
08 |
System.out.println( "a=" +a+ " b=" +b + " c=" +c+ " d=" +d+ " e=" +e); |
09 |
} |
10 |
} |
类型 | 字节 | 范围 | 举例 |
float | 4 | 1.4E-45 ~ 3.4028235E38 | 3.1415f |
double | 8 | 4.9E-324 ~ 1.7976931348623157E308 | 3.1415,3.1415d |
01 |
public class Lesson02{ |
02 |
public static void main(String[] args){ |
03 |
float a= 3 .1415926f; |
04 |
double b= 3.1415926 ; |
05 |
float c= 2123456789 ; |
06 |
float d=9876543210L; |
07 |
double e=9876543210L; |
08 |
System.out.println( "a=" +a+ " b=" +b + " c=" +c+ " d=" +d+ " e=" +e); |
09 |
System.out.println(Float.MIN_VALUE); |
10 |
System.out.println(Float.MAX_VALUE); |
11 |
System.out.println(Double.MIN_VALUE); |
12 |
System.out.println(Double.MAX_VALUE); |
13 |
} |
14 |
} |
1 |
public class Lesson02{ |
2 |
public static void main(String[] args){ |
3 |
boolean flag = true ; |
4 |
flag= false ; |
5 |
System.out.println( "flag=" +flag); |
6 |
} |
7 |
} |
类型 | 字节 | 范围 | 举例 |
char | 2字节,16位 | ‘\u0000’ ~ ‘\uFFFF’ | ‘犇’,'\u004e’,’n’,23002 |
01 |
public class Lesson02{ |
02 |
public static void main(String[] args){ |
03 |
char a= 'a' ; //ascii字符可以 |
04 |
char b= '1' ; |
05 |
char c= '犇' ; //汉字也可以 |
06 |
char d= '\u004e' ; //其实上所有的Unicode字符都可以 |
07 |
char e= '\n' ; //转义符表示的字符也可以 |
08 |
char f= 65535 ; //也可以,因为char存储的时候占两个字节,因为他不是整数,所以不需要符号位,因此它的最大值就是65535了。 |
09 |
char g= 23002 ; |
10 |
char h= 36745 ; |
11 |
System.out.println( "a=" +a+ " b=" +b + " c=" +c+ " d=" +d+ " e=" +e+ " f=" +f+ " g=" +g+ " h=" +h); |
12 |
} |
13 |
} |


1 |
public class Lesson02{ |
2 |
public static void main(String[] args){ |
3 |
int a= 1 ; |
4 |
int b= 1 ,c= 1 ; |
5 |
int d=a=b=c= 2 ; |
6 |
System.out.println( "a=" +a+ " b=" +b + " c=" +c+ " d=" +d); |
7 |
} |
8 |
} |
- 好的注释可以改善软件的可读性,可以让开发人员更快理解新的代码。
- 好的注释可以最大限度的提高团队开发的合作效率。
- 长期的注释习惯可以锻炼出更加严谨的思维能力。
// 注释一行 /* 注释若干行 */ /** 注释若干行,并写入 javadoc 文档 */好了,本讲就到这里,下一讲我们讲基本类型的相互转换和赋值,Take your time and enjoy it. 分享至上:分享源头
- 分类:
- Java
更新时间:
相关文章
Java基础第四讲:Java基本语法(三)
本讲内容: Java中的运算符 前言:运算符 operator Java运算符从一个或多个操作数产生新的值。而操作数指的是位于运算符左边或者右边的内容。 Java operators pro 阅读更多…
Java基础第三讲:Java基本语法(二)
本讲内容: 基本数据类型之间的转换、引用数据类型、字符串 一、基本数据类型之间的转换 所谓数据类型转换就是将变量从当前的数据类型转换为其它数据类型,类型转换在Java里是个很严肃的事情^_^ 阅读更多…