Operador y el tipo de datos Java

1, tipo de datos de conversión

variable definida, tenemos que darle un valor inicial a una variable

conversiones de tipos automáticos: tipo de gama se puede convertir a un valor pequeño en una amplia gama de tipos, puede contener gran pequeño enmarcado enmarcada

Emitidos: una amplia gama de tipo no se puede convertir en el valor de pequeña escala, en forma de estructura pequeña, porque gran marco de la mente, a menos que el enchufe fuerte

Cast: pequeño nombre tipo nombre de variable = valor (pequeño nombre de tipo) el tipo de ancho;

demo2 public class {

         public static void Main (args String []) {

                  int a = 10;

                  largo b = 20L;

                  20 es C = largo;      // no error, conversiones de tipos automáticos

                  doble d = 1,234;

                  // flotador e = 1,234; dada, float a int posible pérdida de precisión

                  flotar f = 1.234f;

                  Char g = 'g';

                  boolean h = true;

                  // i boolean; error, el valor no inicializado

                  System.out.println (a);

                  System.out.println (b);

                  System.out.println (c);

                  System.out.println (d);

                  System.out.println (f);

                  System.out.println (g);

                  System.out.println (h);

         }

}

conversiones de tipos automáticos: tipo de gama se puede convertir a un valor pequeño en una amplia gama de tipos, puede contener gran pequeño enmarcado enmarcada

Emitidos: una amplia gama de tipo no se puede convertir en el valor de pequeña escala, en forma de estructura pequeña, porque gran marco de la mente, a menos que el enchufe fuerte

Cast: pequeño nombre tipo nombre de variable = valor (pequeño nombre de tipo) el tipo de ancho;

demo5 public class {

public static void Main (args String []) {

         byte a = -128;

         byte b = 127;

       // byte c = a-1; error, la precisión puede ser perdido

         D = byte (byte) (1-A.);     // fundido

         System.out.println (1-A.);  // no desbordamiento, el resultado será automáticamente se convertirá en int byte

         System.out.println (. B + 1);  // no desbordamiento, el resultado será automáticamente se convertirá en int byte

       //System.out.println(c); error: tipos incompatibles: conversión de int a byte puede ser la pérdida de

         System.out.println (d);   // Después de coerce, la salida 127 se produce desbordamiento , -1 = valor máximo del valor mínimo, valor máximo = el valor mínimo + 1

         }

}

Descripción: Byte, no son intercambiables entre el corto, char, si se desea operar, tiene que llevarlos primero convertidos a tipo int, a continuación, realizar la operación.

demo9 public class {

         public static void Main (args String []) {

                  byte a = 1 ;   short b = 10 ;    int c = 100 ;

                  /* char i = a ;  错误: 不兼容的类型: 从byte转换到char可能会有损失

                  char j = b ;  错误: 不兼容的类型: 从short转换到char可能会有损失

                  char k = c ;  错误: 不兼容的类型: 从int转换到char可能会有损失 */

                  char i =(char) a ;

                  char j =(char) b ;

                  char k =(char) c ;

                  System.out.println(i);  //输出乱码

                  System.out.println(j);  //输出乱码

                  System.out.println(k);  //输出d

         }

}

 

2、 扩展数据类型

在计算机中,整数可以有 4 种表示方式,分别是十进制、十六进制数、八进制数、二进制数。

public class demo11{

                       public static void main(String[] args) {

                       int a = 20 ;       //十进制

                       int b = 0x89 ;     //十六进制:表示形式0X或0x开头,0-9 A-F

                       int c = 012 ;      //八进制:表示形式0开头,0-7

                       int d = 0b1010 ;   //二进制:表示形式0b或0B开头,0或1

                       System.out.println("十进制:"+a);    //输出 十进制:20

                       System.out.println("十六进制:"+b);  //输出 十六进制:137

                       System.out.println("八进制:"+c);    //输出 八进制:10

                       System.out.println("二进制:"+d);    //输出 二进制:10

                       }

//输出的结果都显示为十进制的数

关于整数除法的问题

public class demo15{

         public static void main(String[] args) {

         int a = 20 ;        int b = 8 ;

         System.out.println(a/b);    //输出2   a和b都是int类型,所以a/b也是int类型

         System.out.println((float)a/b);  //输出2.5  a是float,b是int类型,所以a/b自动转换为float类型

         System.out.println(a/(float)b);   //输出2.5  b是float,a是int类型,所以a/b自动转换为float类型

         }

}

转义字符

\r

归位

\b

倒退一格

\f

换页

\’

\n

换行

\t

一个制表符

\\

\

\”

转义字符的类型是字符类型

public class demo18{

         public static void main(String[] args) {

         char a = '\n' ;  //换行      char b = '\t' ;  //tab制表符

         char c = '\r' ;  //归位,表示接受键盘输入,相当于摁下了回车键

         char d = '\b' ;  //退一格

         System.out.println("HELLO"+a+"world");  //HELLO换行world

         System.out.println("HELLO"+b+"world");  //HELLO   world

         System.out.println("HELLO WORLD"+c+"ABC");  //ABCLO WORLD  (把HEL换为ABC)

         System.out.println("HELLO WORLD"+d+"ABC");  //HELLO WORLABC (把D删除)

         System.out.println("武汉加油,中国加油");  //武汉加油,中国加油

         System.out.println("武汉加油,\"中国\"加油");  //武汉加油,"中国"加油

         }

}

3、 运算符

1)、赋值运算符(=)

2)、一元运算符

+

 

-

Int a=1;   打印-a,输出-1

!

否定取反

Boolean b=true;  打印!b,输出false

3)、算术运算符

+

加法

int a = -10 ;   int b = 3 ;

  int c = 10 ;   int d = -3 ;

System.out.println("结果为"+(a/b));  //-3

System.out.println("结果为"+(a*b));  //-30

System.out.println("结果为"+(a%b));  //-1

System.out.println("结果为"+(c%d));  //1

System.out.println("结果为"+(a%d));  //-1

-

减法

*

乘法

/

除法

%

取余

4)、关系运算符

>/<

 

返回结果为boolean类型

>=/<=

 

==

判断是否等于

!=

不等于

5)、自增自减运算符

x++

先运算后自加

                  int a = 10 ;  

       int b = 20 ;

                  int x = (a++) + (--b) ;

                  System.out.println("x为"+x);  //29

                  System.out.println("a为"+a);  //11

                  System.out.println("b为"+b);  //19

x--

先运算后自减

++x

先自加后运算

--x

先自减后运算

6)、逻辑运算符

与:要求所有条件都进行判断; 短路与:如果第一个条件为false,则后面的条件不再判断

或:要求所有条件都进行判断; 短路或:如果第一个条件为true,则后面的条件不再判断

&

                  int a = 10 ;   int b = 20 ;

                  boolean x = (a > b) ;  boolean y = (a < b) ;

                  System.out.println(x & y);  //false

                  System.out.println(x | y);  //true

                  System.out.println(x && y);  //false

                  System.out.println(x || y);  //true

&&

短路与

|

||

短路或

7)、括号运算符

         ()代表优先级最优先的运算

8)、三目运算符

         表达式?  成立时的结果: 不成立时的结果

int a = 10 ;   int b = 20 ;

                  int x = a>b?a:b;

                  System.out.println(x);  //20

9)、位运算符

&

按位与:都为1,则取1,否则为0

|

按位或:都为0,则取0,否则为1

^

异或:相同为0,否则为1

~

取反

x<<2

左位移:将x的二进制数整体左移指定的位数2,空位用0填充

x>>2

右位移:将x的二进制数整体左移指定的位数2,正数用0填充,负数用1填充

>>> 

无符号右位移:一律用 0 填充

位运算符都是需要转换为二进制后才能进行运算

public class demo25{

         public static void main(String[] args) {

                  int a = 3 ;

                  //00000000 00000000 00000011

                  int b = 6 ;

                  //00000000 00000000 00000110

                  System.out.println(a & b);  //2

 

            //   00000000 00000000 00000011

           //    00000000 00000000 00000110

           //    -----------------------------------

           //    00000000 00000000 00000010

 

                  System.out.println(a | b);  //7

 

           //    00000000 00000000 00000011

           //    00000000 00000000 00000110

           //     ----------------------------------

           //    00000000 00000000 00000111

 

                  System.out.println(a ^ b);  //5

 

           //    00000000 00000000 00000011

           //    00000000 00000000 00000110

           //     ----------------------------------

           //    00000000 00000000 00000101

         }

}

位移

public class demo26{

         public static void main(String[] args) {

                  int a = 3 ;

                  //00000000 00000000 00000011

                  System.out.println(a<<2);  //12

                  /*

                  00000000 00000000 00000011

              00000000 00000000 0000001100

                  */

                  System.out.println(a>>2);  //0

                  /*

                  00000000 00000000 00000011

                  0000000000 00000000 00000011

                  */

                  System.out.println(a>>>2);  //0

                  /*

                  00000000 00000000 00000011

                  0000000000 00000000 00000011

                  */

         }

}

~取反和负数的二进制表示

public class demo27{

         public static void main(String[] args) {

                  int a = 3 ;

                  //00000000 00000000 00000011

                  int b = -3 ;

                  //10000000 00000000 00000011  3的二进制把符号位变为1

                  //10000000 00000000 00000011  原码

                  //11111111 11111111 11111100  反码

                  //11111111 11111111 11111101  补码 -3的二进制表示

                  //00000000 00000000 00000010   ~b二进制表示

                  System.out.println(~a);           //-4

                  System.out.println(~b);           //2

         }

}

10)、运算符的优先级

表达式通常由多个运算符组成。优先级的规则决定每个运算符在任何给定表达式中的计算顺序

按操作数多少划分:一元操作符 → 二元操作符 → 三元操作符

按运算类型划分:算术运算符→关系运算符→逻辑运算符→赋值运算符

尽量多的使用括号:括号优先级别最高

 

4、 简洁表达式

运算符

范例

相当于

备注

+=

a+=b

a=a+b

short a = 3 ;

a+=1;

//a = a + 1;   错误: 不兼容的类型: 从int转换到short可能会有损失

System.out.println(a);   //输出4

//说明:此处的a+=1,等价于a = (short)(a+1)

-=

a-=b

a=a-b

*=

a*=b

a=a*b

/=

a/=b

a=a/b

%=

a%=b

a=a%b

 

5、 JShell交互式编程工具

在 JDK1.9 以后,提供了一个交互式的开发工具——JShell。利用此工具可以方便的执行程序。JSheel一般用于测试,开发阶段不使用此工具

进入JSheel的两种方法:直接在dos窗口中输入jsheel 或 在运行中输入JSheel

 

可以在DOS窗口中,可以直接执行文件的代码,使用指令:/open i:/01.txt

 

 

6、 流程控制语句

顺序语句:程序至上而下执行,一条语句执行完毕后再执行下一条,一直执行到程序末尾

选择语句:判断语句,根据条件来选择到底执行哪个语句

循环语句:根据条件来判断执行的次数

1)、选择语句

If语句语法格式1

If(判断条件){

    语句;

}

判断语句可以是一个boolean值,也可以是一个表达式(结果也必须是boolean值)

public class demo30{

         public static void main(String[] args) {

                  int a = 3 ;

                  if (a>10){

                          System.out.println(a);

                  }

                  System.out.println("a<10");  

         }

}

If语句语法格式2

If(判断条件){

成立时的语句;

}

Else{

不成立时的语句;

}

public class demo31{

         public static void main(String[] args) {

                  int a = 3 ;

                  if (a%2==1){

                          System.out.println("奇数");

                  }

                  else{

                          System.out.println("偶数");  

                  }

         }

}

System.out.println((a%2==1)?"奇数":"偶数");    以上if语句等同于此三目运算符

If语句中的花括号可以省略,但是不建议省略

 

作者:kerwin-chyl

文章链接:https:////www.cnblogs.com/kerwin-chyl

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

Supongo que te gusta

Origin www.cnblogs.com/kerwin-chyl/p/12514304.html
Recomendado
Clasificación