关键字
| abstract | assert | boolean | byte | break |
|---|---|---|---|---|
| case | catch | char | class | const |
| continue | default | do | double | else |
| enum | extends | final | finally | float |
| for | goto | if | implements | import |
| instanceof | int | interface | long | native |
| new | package | private | protected | public |
| return | strictfp | short | static | super |
| switch | synchronized | this | throw | throws |
| transient | try | void | volatile | while |
数据类型(强类型语言)

int i = 10;int i2 = 010; // 八进制int i3 = 0x10; //十六进制------------------------// float 有限 离散 舍入误差 大约 接近单不等于// 避免使用浮点数进行比较// 使用BigDecimal类float f = 0.1f;double d = 1.0/10;System.out.print(f == d); // falsefloat d1 = 1222222222222f;float d2 = d1 + 1;System.out.print(d2 == d1); // true---------------------------char c1 = 'a';char c2 = '中';System.out.print((int)c1); // 强制转换
类型转换
- 强制类型转换 (类型)变量名称 高 — 低
- 自动转换 低 — 高 ```java / 注意点: 1.不能对布尔值进行转换 2.不能把对象类型转换为不相干的类型 3.在把高容量转换到低容量得时候,强制转换 4.转换得时候可能存在内存溢出,或者精度问题 /
int num = 10_0000_0000; int year = 20; int total = year * num; System.out.print(total); // 内存溢出 System.out.print((long)total); //依然溢出,转化之前就已经有问题了
System.out.print((long)year * num); //将整个运算提升至long
float num = 1.2f; double num1 = 1.3; System.out.print((int) num); // 1 精度问题 System.out.print((int) num1); // 1 精度问题
<a name="eGX2n"></a>## 变量<a name="v3Tel"></a>### 变量声明```javatype varName [=value] [{, varName[=value]}];// 类型 变量名称 [=值]; 可以使用逗号声明多个同类型的变量int a, b, c; // 不建议写入一行String name = "name";
变量作用域
- 类变量
- 实例变量
局部变量
public class Variable {static int allClicks = 0;// 类变量String str = "hello world";// 实例变量public void method() {/*局部变量作用域*/int i = 0; // 局部变量/*实例变量的使用*/Variable variable = new Variable();System.out.print(variable.str); // "hello world"/*类变量的使用*/System.out.print(allClicks); // 0}public void method2() {/*无法访问其他方法中的局部变量*/System.out.print(i);}}
常量
public class ConstDemo {// 修饰符, 不区分先后顺序// 静态 的 常量 double类型static final double PI = 3.14;public static void main(String[] args) {System.out.print(PI);}}
命名规范
- 见名知意
- 驼峰命名法
- 常量:使用大写字母和下划线: MAX_COUNT
-
运算符
算数运算符: +, -, *, /, %, ++, —
- 赋值运算符 =
- 关系运算符 >, <, >=, <=, ==, !=instanceof
- 逻辑运算符 &&, ||, !
- 位运算符 &, |, ^, ~, >>, <<, >>>
- 条件运算符 ?
-
包机制
为了更好的组织类,Java提供了包机制,用于区别类名的命名空间
- 包语句的语法格式为
package pkg1[.pkg2[.pkg3]]; - 一般利用公司域名倒置作为包名
com.baidu.www 为了能够使用某一个包的成员,我们需要在Java程序中明确导入改包。使用
import语句可完成此功能import package1[.package2...].(classname | *)JavaDoc
Javadoc命令是用来生成自己的API文档的
author
package com.java.base;/*** @author: xiaoyi* @version: 1.0* @since 1.8*/public class Doc {String name;/**** @param name* @return* @throws Exception*/public String test(String name) throws Exception {return name;}}
切到当前java类的目录下执行
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
生成如下文档:
