JAVA基础语法(下)
四、变量、常量
1.变量
变量就是一块值可以改变的内存空间
变量的作用域
①局部变量
public class Test {
public static void main(String[] args) {
int i = 0;
int j;
System.out.pringln(j);
}
public void test(){
System.out.println(i);
}
}
由于变量 i 的作用域在 main 函数中,所以test函数中找不到变量 i 出现报错。同时在 main 函数中,变量 j 只进行了声明,没有被***初始化***,所以依然报错。
②实例变量
public class Test {
int i;
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.i);
}
}
输出结果
0
实例变量从属于对象,只能用对象实例进行调用,如果不自行初始化,其自动赋值为该类型的默认值,数值类型的默认值是 0或者0.0,char的默认值是 ‘’ ,boolean类型默认值为 false ,其余都是 null。
③类变量
public class Test {
static int i = 100;
public static void main(String[] args) {
System.out.println(i);
}
}
输出结果
100
类变量从属于类,会随着类一同出现,一同消失。
2.常量
常量就是值不能改变的变量
通过修饰符 final 进行定义
static final int FINAL_I = 100;
final static int FINAL_J = 100;
static 与 final 都是修饰符,不区分先后顺序。
3.变量的命名规范
①所有变量、方法 、类名:见名知意。
②类成员变量:首字母小写和驼峰原则:helloWorld。
③局部变量:首字母小写和驼峰原则。
④常量:大写字母和下划线:MAX_NUM。
⑤类名:首字母大写加驼峰原则:GoodMan。
⑥方法名:首字母小写加驼峰原则:runRun()。
*4.注意:
· 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
· 变量名必须是合法的标识符。
· 变量声明是一条完整的语句,因此每一个声明都必须以分号结束。
· 可以在一行内定义多个同类型变量,如 int a,b,c; 但不提倡。
五、运算符(operator)
1.++i(–i) 和 i++(i–)的区别
public class Test {
public static void main(String[] args) {
int i = 1;
int a = i++;
int b = ++i;
System.out.println(i);
System.out.println(a);
System.out.println(b);
}
}
输出结果
3
1
3
解释
int a = i++; 语句执行过程 :先把 i 的值赋值给 a ,再执行 i=i+1。
int a = i;
i = i + 1;
int b = ++i; 语句执行过程 :先执行 i=i+1,再把 i 的值赋值给 a。
i = i + 1;
int a = i;
2.逻辑运算符——与(and) 或(or) 非(取反)
逻辑运算遵循:真真相与为真,其余相与为假。假假相或为假,其余为真。真取非为假,假取非为真。
public class Test {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println("a && b = "(a&&b));
System.out.println("a || b = "(a||b));
System.out.println("!(a && b) = " !(a&&b));
}
}
输出结果
a && b = false; //真假相与为假
a || b = true; //真假相或为真
!(a && b) = true; //非假为真
拓展:短路运算
public class Test {
public static void main(String[] args) {
int x = 4;
int y = 4;
boolean b = (x<3)&&(x++<3);
boolean d = (y<5)&&(y++==5);
System.out.println(b);
System.out.println(x);
System.out.println("=============");
System.out.println(d);
System.out.println(y);
}
}
输出结果为
false
4
=============
true
5
因为与运算,运算双方其一为假,则运算结果为假,所以在 boolean b = (x<3)&&(x++<3); 中,x<3 为假,所以直接得出结果为假,不执行x++。同样在 boolean d = (d<5)&&(y++==5); 中,d<5 为真,之后执行 y++ ,y++===5 为真,故结果为真。
3.字符串连接符(+)
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 5;
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+""+c);
}
}
输出结果
30
1020
305 //先计算 a+b=30,30+"" 转换为字符串,再加 c,得305.
4.三元运算符
public class Test {
public static void main(String[] args) {
// x ? y : z
// if x==true,result=y,else result=z
int score = 95;
String type = score<60?"不及格":"及格";
String type2 = score>90?"优秀":"非优秀";
System.out.println(type);
System.out.println(type2);
}
}
输出结果
及格
优秀
六、包机制、Java Doc
1.包机制
为了更好的组织类,Java提供了包机制,用于区别类名的命名空间
语法格式为:
package pkg1[.pkg2[.pkg3....]]; //声明包空间
import pkg1[.pkg2[.pkg3....]].(class); //导入包中的类
import pkg1[.pkg2[.pkg3....]].*; //导入包中的所有类
2.JavaDoc
javadoc 命令是用来生成自己的 API 文档的
参数信息
@author 作者名
@version 版本号
@since 指明需要最早使用的jdk版本
@param 参数名
@return 返回值情况
@throws 异常抛出情况
示例
//类文档注释
/**
* @author xu
* @version 1.0
* @since 1.8
*/
public class Test {
String name;
public static void main(String[] args) {
}
//方法文档注释
/**
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
使用 javadoc 命令生成帮助文档
javadoc -encoding UTF-8 -charset UTF-8 Test.java //-encoding UTF-8 -charset UTF-8 作用为将文件中的汉字以及英文字符转换为UTF-8编码格式
执行结果