JAVA学习第三天笔记

一、运算符

运算符包括算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符、三元运算符。

 1.1算术运算符例子:

class VarDemo
{

public ststic void main(string[] args)

{

//6000 java是强类型转换

//算术运算符 +-*/%(膜)

int x=6370;

x=x/1000*1000;

system.out.printin(x);


//左边小于右边为左边 3

system.out.printin(3%4);

//负数参考被膜数 1

system.out.printin(5%-2);

//5+5=55

system.out.printin("5+5="+5+5);

//5+5=10

system.out.printin("5+5="+(5+5));


//4.5

int a=4,b=5;

system.out.printin(a+","+b);


//a=4,b=3

int a=3,b;

b=a++;

system.out.printin("a="+a",b="+b);


//a=4,b=4

int a=3,b;

b=++a;

system.out.printin("a="+a+",b="+b);


//3

int i=3;

i=i++;

system.out.printin(i);


//s+=4自动转换过程

short s=3;

s+=4;//精度丢失

s=s+4; 正确方式:s=(short)(s+4);

system.out.printin("s="+s);

}

}

1.2赋值运算符

class VarDemo
{

public ststic void main(string[] args)

{

//丢失精度

byte b=4;

//b=3+7;

byte b1=3;

byte b2=7;

b=b1+b2;


system.out.printin(b);


int x;

int x1=integer.max_value;

int x2=2;

x=x1+x2;

system.out.printin(x);

}

}

1.3比较运算符

class VarDemo
{

public ststic void main(string[] args)

{

/*

比较运算符,运算符的结果必须是true或者false。

*/

//system.out.printin(3>2);//true

system.out.printin(3==2);//false

}

}

1.4逻辑运算符

class VarDemo
{

public ststic void main(string[] args)

{

/*
逻辑运算符用于连接两个boolean类型的表达式
&:与  一假必假
|:或  一真必真
^:异或:和或有点不一样
运算特点
true^true=false;
true^false=true;
false^true=true;
false^false=false;
运算规律:符号的两边结果如果相同,结果是false,反之成立


!非运算符
&&双与
||双或 当左边为true时,右边不参与运算
*/
int x=3;
system.out.printin(x>1&x<4);//true 


int x=3;
system.out.printin(x>1&&x<4);//true 


int x=3;
system.out.printin(x<1&&x>4);//false 短路效应

}

}

1.5位运算符

class VarDemo
{

public ststic void main(string[] args)

{

system.out.printin(6&3);//2

//6&3=2

110

      &011

      ----

      010=2

system.out.printin(6|3);//7

//6|3=2

110

      |011

      ----

        111=7

//6 ^ 3 ^3=6 注:一个数异或同一个数两次,结果还是这个数。(加密应用)

   ---  ---

 加密 解密


 //<< >>
 //3<<2=12  3<<3=24 左移几位其实就是该数据乘以2的几次方。<<:可以完成2的次幂运算
 //6>>1=3 6>>2=1
 //6/2(1)=3 6/2(2)=6/4=1
 //>>>;无符号右移,数据进行右移时,高位出现的空位,无论原高位是什么,空位都用0补。
  system.out.printin(2<<3);//2*8=2*2^3=16

 /*实例*/
  int a=3,b=5; 
  system.out.printin("a"+a+",b="+b);//3,5


  int a=3,b=5; 
  int c;
  c=a;
  a=b;
  b=c;
  system.out.printin("a"+a+",b="+b);//5,3




 //这种方式不要用,如何两个整数的数值过大,会超出int范围,会强制转换,数据会变大
  int a=3,b=5; 
  //a=3+5;a=8
  a=a+b;
  //3+5-5=3;b=3;
  b=a-b;
  //3+5-3=5;a=5
  a=a-b;
  system.out.printin("a"+a+",b="+b);//5,3

  //面试模式
  int a=3,b=5; 
  a=a^b;a=3^5;
  b=a^b;b=(3^5)^5;b=3
  a=a^b;a=(3^5)^3;a=5;
  system.out.printin("a"+a+",b="+b);//5,3

}
}

1.6三元运算符

class VarDemo
{
public ststic void main(string[] args)
{
/*
三元运算符
*/
int x=3,y;
y=(x>1)?10:20
system.out.printin("y="+y);//10


//获取两个整数中的较大的整数
int a,b;
int max=a>b?a:b;

//获取三个整数中的较大的整数
int o,p,q;
int temp=o>p?o:p;
int max2=temp>q?temp:q;
}
}

二、程序流程控制(判断、选择、循环结构)

class VarDemo
{
public ststic void main(string[] args)
{


{//局部代码块可以定义局部变量的生命周期  作用:1、释放内存 2、性能优化
int a=3;
//a的运算
system.out.printin(a+4);
}
/*
if语句的第一种格式
if(条件表达式)
{
执行语句;
}
*/
int x=3;
if(x>1)
//{
system.out.printin("yes");
//}


/*


if语句的第二种格式
if(条件表达式)
{
执行语句;
}
else//否则
{
执行语句;
}
*/
int x=1;
if(x>1)
{
system.out.printin("yes");
}
else
{
system.out.printin("no");
}


//实例
int a=3,b;
if(a>1)
  b=10;
else
          b=20;
 //b=a>1?10:20; 当ifelse运算后,有一个具体的结果则用三元运算符
system.out.printin("b="+b);


/*
if语句的第一种格式
if(条件表达式)
{
执行语句;
}
else if(条件表达式)
{
执行语句;
}
......
else
{
执行语句;
}
*/
int x=3;
if(x>1)
//{
system.out.printin("a");//执行完毕
//}
else if(x>2)
system.out.printin("b");
else if(x>3)
system.out.printin("c");
else
system.out.printin("d");


int x=3;
if(x>1)
//{
system.out.printin("a");//执行
//}
if(x>2)
system.out.printin("b");//执行
if(x>3) //执行
system.out.printin("c");
else
system.out.printin("d");


  //实例
  /*
根据用户指定的具体数据,判断该数据对应的星期。


  */
}
}

三、局部代码块 作用域

class VarDemo
{
public ststic void main(string[] args)
{
     {//局部代码块 作用域功能
int m=89;
system.out.printin("hello"+m);
     }
     system.out.printin("over"+m);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值