易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第3章 操作符
第3章 操作符/3.1 更简单的打印语句
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.2 使用Java操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.3 优先级
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.4 赋值
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
- 基本类型赋值:将值进行了拷贝,也就是两份内存,比如:a=b,如果对a进行了修改,对b是没有影响的
- 对象类型赋值:对象赋值是引用,执行的是同一份内存,比如:a =b,如果对a进行了修改,那么b也会受影响的
- 举例:
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } }
- 结果输出:
第3章 操作符/3.4 赋值/3.4.1 方法调用中的别名问题
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } }
- 输出结果:
第3章 操作符/3.5 算术操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
例子源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } }
- 例子输出结果:
第3章 操作符/3.5 算术操作符/3.5.1 一元加、减操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.6 自动递增和递减
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 前缀式:先自己运算,后参与其它操作
- 后缀式:先参与其它操作,后自己运算
-
举例:
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */
- 结果:
-
源代码:
第3章 操作符/3.7 关系操作符/3.7.1 测试对象的等价性
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 概念:操作结果为boolean,如果关系为真,则结果为true,否则为false
-
包括
-
- >
- <
- >=
- <=
- ==:适用所有基本类型 和 所有对象( 第2章 一切都是对象/2.2 必须由你创建所有对象/2.2.2 基本类型)
- !=:适用所有基本类型和 所有对象
-
- 测试对象的引用是否相等
- 源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } }
- 测试结果:
- 源代码:
- 测试对象的值是否相等
- 源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } }
- 测试结果:
-
- 测试结果说明:因为equals默认比较的引用,由于两个对象都是单独new的,这样结果肯定是不一样的,除非覆盖equals方法,重新定义比较逻辑
- 源代码:
第3章 操作符/3.8 逻辑操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.8 逻辑操作符/3.8.1 短路
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 概念:指的是如果一部分逻辑运算结果就能确认整个表达式的值则不需要对剩下的逻辑进行运算
-
举例
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } }
- 测试结果:
-
源代码:
第3章 操作符/3.9 直接变量
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.9 直接变量/3.9.1 指数记数法
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
举例
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class Expinents{ public static void main(String []args) { float expFloat = 1.21e-43f; System.out.println("expFloat = " + expFloat ); double expDouble = 4.7e47d; System.out.println("expDouble = " + expDouble ); } }
- 结果:
-
源代码:
- 注意:指数通常默认为double类型输入,如果这样编写一行代码:float f = le-43是会报错:
,尾巴要加上加上“f”
第3章 操作符/3.10 按位操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.11 移位操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 左移
:低位补0,移动多少位就是补0多少位
- 右移
:高位补0,移动多少位就是砍掉末尾多少位 ;如果为正数则高位插入0,如果为负数则高位插入1
- “无符号”右移
:无论正负高位都插入0
-
举例:
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class Expinents{ public static void main(String []args) { float expFloat = 1.21e-43f; System.out.println("expFloat = " + expFloat ); double expDouble = 4.7e47d; System.out.println("expDouble = " + expDouble ); } } class URShift{ public static void main(String []args) { int initValue = 123; int i = initValue; System.out.println("i = " + Integer.toBinaryString(i)); i = (i<< 3); System.out.println("i <<= 3 " + Integer.toBinaryString(i)); i = initValue; i = (i>> 3); System.out.println("i >>= 3 " + Integer.toBinaryString(i)); i = initValue; i >>>= 2; System.out.println("i >>>= 2 " + Integer.toBinaryString(i)); } }
- 输出结果:
-
源代码:
- 左移
第3章 操作符/3.12 三元操作符if-else
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.13 字符串操作符 + 和 +=
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class Expinents{ public static void main(String []args) { float expFloat = 1.21e-43f; System.out.println("expFloat = " + expFloat ); double expDouble = 4.7e47d; System.out.println("expDouble = " + expDouble ); } } class URShift{ public static void main(String []args) { int initValue = 123; int i = initValue; System.out.println("i = " + Integer.toBinaryString(i)); i = (i<< 3); System.out.println("i <<= 3 " + Integer.toBinaryString(i)); i = initValue; i = (i>> 3); System.out.println("i >>= 3 " + Integer.toBinaryString(i)); i = initValue; i >>>= 2; System.out.println("i >>>= 2 " + Integer.toBinaryString(i)); } } class StringOperators{ public static void main(String []args) { int i = 0, y =1 ,z = 2; String s = "x,y,z "; System.out.println(s + i + y + z); System.out.println(s + (i + y + z)); } }
- 输出结果:
第3章 操作符/3.14 使用操作符常犯的错误
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.15 类型转换操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.15 类型转换操作符/3.15.1 截尾和舍入
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 使用场合:主要是针对“窄化转换”,将一个大类型转换为小类型,比如将一个浮点型转换为整型
-
举例:无四舍五入
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class Expinents{ public static void main(String []args) { float expFloat = 1.21e-43f; System.out.println("expFloat = " + expFloat ); double expDouble = 4.7e47d; System.out.println("expDouble = " + expDouble ); } } class URShift{ public static void main(String []args) { int initValue = 123; int i = initValue; System.out.println("i = " + Integer.toBinaryString(i)); i = (i<< 3); System.out.println("i <<= 3 " + Integer.toBinaryString(i)); i = initValue; i = (i>> 3); System.out.println("i >>= 3 " + Integer.toBinaryString(i)); i = initValue; i >>>= 2; System.out.println("i >>>= 2 " + Integer.toBinaryString(i)); } } class StringOperators{ public static void main(String []args) { int i = 0, y =1 ,z = 2; String s = "x,y,z "; System.out.println(s + i + y + z); System.out.println(s + (i + y + z)); } } class CastringNumbers{ public static void main(String []args) { double above = 0.7,below = 0.4; float fabove = 0.7f,fbelow = 0.4f; System.out.println("above = " + (int)above); System.out.println("below = " + (int)below); System.out.println("fabove = " + (int)fabove); System.out.println("fbelow = " + (int)fbelow); } }
- 输出结果:
,没有四舍五入,都是截断为0了
-
源代码:
- 举例:四舍五入
- 源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class Expinents{ public static void main(String []args) { float expFloat = 1.21e-43f; System.out.println("expFloat = " + expFloat ); double expDouble = 4.7e47d; System.out.println("expDouble = " + expDouble ); } } class URShift{ public static void main(String []args) { int initValue = 123; int i = initValue; System.out.println("i = " + Integer.toBinaryString(i)); i = (i<< 3); System.out.println("i <<= 3 " + Integer.toBinaryString(i)); i = initValue; i = (i>> 3); System.out.println("i >>= 3 " + Integer.toBinaryString(i)); i = initValue; i >>>= 2; System.out.println("i >>>= 2 " + Integer.toBinaryString(i)); } } class StringOperators{ public static void main(String []args) { int i = 0, y =1 ,z = 2; String s = "x,y,z "; System.out.println(s + i + y + z); System.out.println(s + (i + y + z)); } } class CastringNumbers{ public static void main(String []args) { double above = 0.7,below = 0.4; float fabove = 0.7f,fbelow = 0.4f; System.out.println("above = " + (int)above); System.out.println("below = " + (int)below); System.out.println("fabove = " + (int)fabove); System.out.println("fbelow = " + (int)fbelow); } } class RoundNumbers{ public static void main(String []args) { double above = 0.7,below = 0.4; float fabove = 0.7f,fbelow = 0.4f; System.out.println("above = " + Math.round(above)); System.out.println("below = " + Math.round(below)); System.out.println("fabove = " + Math.round(fabove)); System.out.println("fbelow = " + Math.round(fbelow)); } }
- 输出结果:
- 源代码:
第3章 操作符/3.15 类型转换操作符/3.15.2 提升
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第3章 操作符/3.16 Java没有sizeof
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |