Java 语法 索引 ----- 运算符

算术运算符

float x = 3+2; // 5 // addition 加
x = 3-2; // 1 // subtraction 减
x = 3*2; // 6 // multiplication 乘
x = 3/2; // 1 // division 除
x = 3%2; // 1 // modulus (division remainder) 余数

Combined assignment operators

int x = 0;
x += 5;      //  x = x+5;
x -= 5;     //  x = x-5;
x *= 5;    //   x = x*5;
x /= 5;     //   x = x/5;
x %= 5;     //  x = x%5;

递增和递减运算符

++x; // x += 1
??x; // x -= 1

++x; // pre-increment
??x; // pre-decrement
x++; // post-increment
x??; // post-decrement

x = 5; y = x++; // y=5, x=6
x = 5; y = ++x; // y=6, x=6

比较运算符

boolean x = (2==3); // false // equal to 等
         x = (2!=3); // true // not equal to 不等
         x = (2>3); // false // greater than 大于
         x = (2<3); // true // less than 小于
         x = (2>=3); // false // greater than or equal to 大于等于
         x = (2<=3); // true // less than or equal to 小于等于

逻辑运算符

boolean x = (true && false); // false // logical and 并且
         x = (true || false); // true // logical or 或者
         x = !(true); // false // logical not 不是

Java 语法 索引 ----- 运算符,布布扣,bubuko.com

时间: 2024-08-25 08:26:04

Java 语法 索引 ----- 运算符的相关文章

Java 语法 索引 ----- 条件语句(If Else,Switch)

if (x < 1) System.out.print(x + " < 1"); else if (x > 1) System.out.print(x + " > 1"); else System.out.print(x + " == 1"); Switch switch (y) { case 0: System.out.print(y + " is 0"); break; case 1: System

Java 语法 索引 ----- 主函数 (Main)

public class MyApp { public static void main(String[] args) { System.out.print("Hello World"); } } Java 语法 索引 ----- 主函数 (Main),布布扣,bubuko.com

Java 语法 索引 ----- 变量-----数据类型

数据类型 类型 bits/byte 范围 默认值 byte 8/1 -128 +127 0 short 16/2 -32,768+32,767 0 int 32/4 -2,147,483,648 = -231+2,147,483,647 = 231-1 0 long 64/8 -9,223,372,036,854,775,808 = -263+9,223,372,036,854,775,807 = 263-1 0L float 32/4 1.40129846432481707e-45  = 2-

Java 语法 索引 ----- 数组(Arrays)

数组声明,分配, 赋值 int y[] = new int[3]; y[0] = 1; y[1] = 2; y[2] = 3; int[] x = new int[] {1,2,3}; int[] x = {1,2,3}; 二维数组 String[][] x = {{"00","01"},{"10","11"}}; String[][] y = new String[2][2]; y[0][0] = "00"

Java 语法 索引 ----- 循环(loop)

While 和  Do-While //while int i = 0; while (i < 10) { System.out.print(i++); } //do - while int i = 0; do { System.out.print(i++); } while ( i < 10 ); For 和 Foreach for (int i = 0; i < 10; i++){ System.out.print(i); } for (int k = 0, l = 10; k &l

Java 语法 索引 ----- 字符串(String)

组合吃烤串的各种方法 String a = "Hello"; String b = new String(" World"); //在循环里面的话要小心用 String c = a+b; // Hello World a += b; // Hello World String x = "Hello " + "World"; 比较字符串 boolean x = a.equals(b); // compares string bo

Java 语法 索引 ----- 枚举(Enum)

enum Speed { STOP, SLOW, NORMAL, FAST } Speed s = Speed.SLOW; switch(s) { case SLOW: break; } 参考文献: Java Quick Syntax Reference by Mikael Olsson Java 语法 索引 ----- 枚举(Enum)

Java 语法 索引 ----- 接口

interface MyInterface { void exposed(); } class MyClass implements MyInterface { public void exposed() {} public void hidden() {} } public static void main(String[] args) { MyInterface i = new MyClass(); } 参考文献: Java Quick Syntax Reference by Mikael

Java 语法 索引 ----- 构造函数(constructor)

重载 class MyRectangle{ int x, y; public MyRectangle() { x = 10; y = 20; } public MyRectangle(int a) { x = a; y = a; } public MyRectangle(int a, int b) { x = a; y = b; } } Constructor chaining public MyRectangle() { this(10,20); } public MyRectangle(in