注释
Java中的注释有三种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Demo { public static void main(String[] args) { System.out.println("Hello,World!");
} }
|
标识符和关键字
Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符
- 所有的标识符都应该以字母(A-Z或者a-z),美元符($)、或者下划线(_)开始
- 首字符之后可以是字母(A-Z或者a-z),美元符($)、下划线(_)或数字的任何字符组合
- 不能使用关键字作为变量名或方法名
- 标识符是大小写敏感的
数据类型讲解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Demo { public static void main(String[] args) { int num1 = 10; byte num2 = 20; short num3 = 30; long num4 = 30L; float num5 = 50.1F; double num6 = 3.141592653589793238462643; char name = '国'; boolean flag = true; } }
|
数据类型扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class Demo { public static void main(String[] args) { int i = 10; int i2 = 010; int i3 = 0x10; System.out.println(i); System.out.println(i2); System.out.println(i3); char c1 = 'A'; char c2 = '中'; char c3 = '\u0061'; System.out.println(c1); System.out.println((int)c1); System.out.println(c2); System.out.println((int)c2); System.out.println(c3); System.out.println("Hello\tWorld"); System.out.println("Hello\nWorld"); } }
|
运行结果
1 2 3 4 5 6 7 8 9 10 11
| 10 8 16 A 65 中 20013 a Hello World Hello World
|
类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Demo { public static void main(String[] args) {
int i = 128; byte b1 = (byte)i; double b2 = i; System.out.println(i); System.out.println(b1); System.out.println(b2); System.out.println((int)23.7); System.out.println((int)-45.89f); char c = 'a'; int d = c+1; System.out.println(d); System.out.println((char)d); int money = 10_0000_0000; int years = 20; int total = money*years; long total2 = money*years; long total3 = money*((long)years); System.out.println(total); System.out.println(total2); System.out.println(total3); } }
|
运行结果
1 2 3 4 5 6 7 8 9 10
| 128 -128 128.0 23 -45 98 b -1474836480 -1474836480 20000000000
|
变量、常量、作用域
变量
注意事项
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
- 变量名必须是合法的表示符
- 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class Demo { static double salary = 2500; String name; int age; public static void main(String[] args) { int i = 10; System.out.println(i); Demo demo = new Demo(); System.out.println(demo.age); System.out.println(demo.name); System.out.println(salary); } public void add(){
} }
|
运行结果
常量
1 2 3 4 5 6 7
| public class Demo { static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI); } }
|
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则:
monthSalary
除了第一个单词以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线:
MAX_VALUE
- 类名:首字母大写和驼峰原则:
Man
,GoodMan
- 方法名:首字母小写和驼峰原则:
run()
,runRun()
基本运算符
Demo1
1 2 3 4 5 6 7 8 9 10 11 12 13
| package operator; public class Demo { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/(double)b); } }
|
运行结果
Demo2
1 2 3 4 5 6 7 8 9 10 11 12
| package operator; public class Demo { public static void main(String[] args) { long a = 123123123123123L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d); System.out.println(b+c+d); System.out.println(c+d); } }
|
运行结果
Demo3
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package operator; public class Demo { public static void main(String[] args) { int a = 10; int b = 20; int c = 21; System.out.println(c%a); System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); } }
|
运行结果
自增自减运算符、初始Math类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package operator; public class Demo { public static void main(String[] args) { int a = 3; int b = a++; System.out.println(a); System.out.println(b); int c = ++a; System.out.println(a); System.out.println(c); double pow = Math.pow(2,3); System.out.println(pow); } }
|
运行结果
逻辑运算符、位运算符
Demo1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package operator; public class Demo { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b:"+(b&&a)); System.out.println("a || b:"+(a||b)); System.out.println("!(a && b):"+!(a&&b)); int c = 5; boolean d = (c<4)&&(c++<4); System.out.println(d); System.out.println(c); System.out.println(2<<3); } }
|
运行结果
1 2 3 4 5 6
| a && b:false a || b:true !(a && b):true false 5 16
|
三元运算符及小结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package operator; public class Demo { public static void main(String[] args) { int a = 10; int b = 20; a+=b; System.out.println(a); a-=b; System.out.println(a); System.out.println(""+a+b); System.out.println(a+b+""); int score = 50; String type = score <60 ?"不及格":"及格"; System.out.println(type); } }
|
运行结果
包机制
JavaDoc
生成文档
JavaDoc
命令是用来生成自己API文档的
- 参数信息
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws异常抛出情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.zhang.operator;
public class Demo { String name;
public String test(String name) throws Exception{ return name; } }
|
在当前java文件夹下打开cmd,输入:
1
| javadoc -encoding UTF-8 -charset UTF-8 demo.java
|