Java基本运算

开学了,这学期学习java课程,我会把课堂上和自学的内容总结并记录下来,加深记忆,同时帮助有需要的人。

1.自增和自减运算符

public class zizengzijian {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=5,n2=8;
		System.out.println("n1++:"+(n1++));
		System.out.println("n2--:"+(n2--));
		System.out.println("++n1:"+(++n1));
		System.out.println("--n2:"+(--n2));
	}

}
------------------------------
n1++:5
n2--:8
++n1:7
--n2:6

2.比较运算符


public class test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=5;
		int n2=6;
		/*依次将n1与n2比较结果输出*/
		System.out.println("n1>n2的返回值为:"+(n1>n2));
		System.out.println("n1<n2的返回值为:"+(n1<n2));
		System.out.println("n1==n2的返回值为:"+(n1==n2));
		System.out.println("n1!=n2的返回值为:"+(n1!=n2));
		System.out.println("n1>=n2的返回值为:"+(n1>=n2));
		System.out.println("n1<=n2的返回值为:"+(n1<=n2));
	}

}
------------------------------
n1>n2的返回值为:false
n1<n2的返回值为:true
n1==n2的返回值为:false
n1!=n2的返回值为:true
n1>=n2的返回值为:false
n1<=n2的返回值为:true

3.逻辑运算符

public class test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=21;
		int n2=30;
		/*声明boolean型变量,保存运算符的返回值*/
		boolean result1=((n1>25)&&(n2<35));
		boolean result2=((n1>25)||(n2<35));
		System.out.println(result1);
		System.out.println(result2);
	}

}
------------------------------
false
true

4.三元运算符

条件式?值1:值2

public class test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=21;
		int n2=30;
		boolean result=n1<n2?true:false;
		
		System.out.println(result);
	}

}
------------------------------
true

相当于

public class test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=21;
		int n2=30;
		boolean result;
		if (n1<n2)
			result=true;
		else
			result=false;
		
		System.out.println(result);
	}

}
-----------------------------
true

5.数据类型转换


public class test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int n1=(int)3.25;
		long n2=(long)456.6F;
		double n3=(double)14;
		System.out.println(n1);
		System.out.println(n2);
		System.out.println(n3);
	}

}
-----------------------------
3
456
14.0

6.注释

单行注释://注释内容
多行注释:/*注释内容*/
文档注释:/**注释内容*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据攻城小狮子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值