Java参数传递是值传递,顾名思义,也就是变量指向基础数据的值或者对象的引用地址。
TestClass testClass = new TestClass(); testClass.setName("张三"); update(testClass);//传递对象应用地址 System.out.println(testClass.getName());//李四 update2(testClass);//测试变量只是对象引用地址传递 System.out.println(testClass.getName());//李四 int s = 0;//基础数据值传递 update3(s); System.out.println(s);//0
public static void update(TestClass testClass){
testClass.setName("李四");
}
public static void update2(TestClass testClass){
testClass = new TestClass();
testClass.setName("王五");
}
public static void update3(int temp){
temp = 3;
}
运算
//float f = 1.1; 1.1是double,向下需要强转 float f = 1.1f;
(byte,short,char)—int—long—float—double
将"大"数据转换为"小"数据时,你可以使用强制类型转换。(可能会导致溢出或精度的下降)
char c = ‘c‘; int d = c; System.out.println(d);//字符型转高级类型时,会转换为对应的ASCII值 byte e = 99; char g = (char) e; short h = (short) g;//这三个是同级转换也需要强制转换
表达式的数据类型自动提升, 关于类型的自动提升,注意下面的规则。
①所有的byte,short,char型的值将被提升为int型;
②如果有一个操作数是long型,计算结果是long型;
③如果有一个操作数是float型,计算结果是float型;
④如果有一个操作数是double型,计算结果是double型;
short m = 3; m = (short) (m + 1);
包装类过度类型转换
float r = 1.1f; Float rr = r; double rrr = rr.doubleValue(); Double rrrr = rrr; int rrrrr = rrrr.intValue(); System.out.println(rrrrr);
另外switch简单使用
switch (str){ /*case 1: System.out.println(1); break; case 2: break;*/ case "3333": System.out.println(str); break; } //switch java 7开始可以使用String java 5 开始可以使用枚举 //本来可以使用 byte char short int //switch 也可以做值范围匹配
原文:https://www.cnblogs.com/amenga/p/12497270.html