Java基础:数据类型与运算符

示例代码:

  1. class Demo{
  2.     public static void main(String[] args){
  3.         boolean b=false;                              //boolean类型:true false
  4.         System.out.println("b is "+b);
  5.         if(b){
  6.             System.out.println("This is not executer.");
  7.         }
  8.  
  9.         double x,y;                           //double类型:
  10.         byte b;                                 //byte类型:
  11.         int i;
  12.         char ch;                              //char类型
  13.         
  14.         x=10.0;
  15.         y=3.0;
  16.         
  17.         i=(int)(x/y);                       //强制转换(int)为int类型
  18.         System.out.println("Integer outcome of x/y:"+i);
  19.         
  20.         i=100;
  21.         b=(byte)i;                       //强制转换(byte)类型
  22.         System.out.println("Value of b:"+b);
  23.         
  24.         i=257;
  25.         b=(byte)i;
  26.         System.out.println("Value of b:"+b);
  27.         
  28.         b=88;
  29.         ch=(char)b;                 //强制转换为(char)类型
  30.         System.out.println("ch:"+ch);
  31. -------------------------------------------------------------------------------------------------   
  32. 算数运算符:+(加),-(减),*(乘),/(除),%(取余),++(自增),--(自减)
  33. 关系运算符:==(等于),!=(不等于),>(大于),<(小于),>=(大于等于),<=(小于等于)
  34. 逻辑运算符:&(与AND),|(或OR),^(异或XOR),||(短路或),&&(短路与),!(非NOT)
  35. ----------------------------------------------------------------------------------------------------
  36.        逻辑运算符真值表 p&q(一假为假)    p|q(一真为真)   p^q(相同为假,不同为真)   !p(取反)
  37.         System.out.println("P\tQ\tAnd\tOR\tXOR\tNOT");    //\t:tab,\n 换行
  38.         boolean p,q;
  39.         
  40.         p=true;q=true;
  41.         
  42.         System.out.print(p+"\t"+q+"\t");                          //print()不换行
  43.         System.out.print((p&q)+"\t"+(p|q)+"\t");
  44.         System.out.print((p^q)+"\t"+(!p)+"\t");
  45.         System.out.println();                                           //println()自动换行
  46.         
  47.         p=true;q=false;
  48.         
  49.         System.out.print(p+"\t"+q+"\t");
  50.         System.out.print((p&q)+"\t"+(p|q)+"\t");
  51.         System.out.print((p^q)+"\t"+(!p)+"\t");
  52.         System.out.println();
  53.         
  54.         p=false;q=true;
  55.         
  56.         System.out.print(p+"\t"+q+"\t");
  57.         System.out.print((p&q)+"\t"+(p|q)+"\t");
  58.         System.out.print((p^q)+"\t"+(!p)+"\t");
  59.         System.out.println();
  60.         
  61.         p=false;q=false;
  62.         
  63.         System.out.print(p+"\t"+q+"\t");
  64.         System.out.print((p&q)+"\t"+(p|q)+"\t");
  65.         System.out.print((p^q)+"\t"+(!p)+"\t");
  66.         System.out.println();
  67.     }
  68. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值