Java基礎_基本運算子
阿新 • • 發佈:2021-11-22
Demo01
package operator; /** * @projectName: JAVA程式設計 * @package: operator * @className: Demo01 * @author: IT_Jay * @description: TODO * @date: 2021/11/17 18:54 */ public class Demo01 { public static void main(String[] args) { //二元運算子 //ctrl + D :複製當前行到下一行 int a = 10; int b = 20; int c = 30; int d = 40; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/b); //int 為整型 System.out.println(a/(double)b); } }
- ctrl + D :複製當前行到下一行
- "/"取整: a/b 10 / 20 = 0.5 , 整數部分為0
Demo02
package operator; /** * @projectName: JAVA程式設計 * @package: operator * @className: Demo02 * @author: IT_Jay * @description: TODO * @date: 2021/11/17 18:59 */ public class Demo02 { public static void main(String[] args) { long a = 12341414141151454L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d); //Long System.out.println(b+c+d); //Int System.out.println(c+d); //Int System.out.println((double) c+d);//Double //System.out.println((String) (c+d)); //不可將Int型轉換(cast)為String型 } }
-
short型、byte型運算結果預設為int型
-
System.out.println((String) (c+d)); //不可將Int型轉換(cast)為String型
Demo03
package operator; /** * @author IT_Jay * @date 2021/11/21 21:13 */ public class Demo03 { public static void main(String[] args) { //關係運算符返回布林值 True False int a = 10; int b = 20; int c = 21; //取餘 模運算 System.out.println(c%a); //c /a 21 / 10 =2......1 System.out.println(a > b); System.out.println(a < b); System.out.println(a == b); System.out.println(a != b); } }
- 關係運算符結果返回布林值 True || False
- “%” 取餘 模運算 c /a 21 / 10 =2......1