Java 基础1.1

变量

由字母数字下划线美元符号组成
不能以数字开头
不能使用java 关键字

数据类型

String 字符串

char 字符型

float 单精度浮点型

double 双精度浮点型

boolean 布尔型


类型自动转换

public class HelloWorld{
    public static void main(String[] args) {
		double avg1=78.5;
		int rise=5;
		double avg2=avg1+rise;
		System.out.println("考试平均分:"+avg1);
		System.out.println("调整后的平均分:"+avg2);
	}
}

强制类型转换(int)

public class HelloWorld{
    public static void main(String[] args) {
		double heightAvg1=176.2;
		int heightAvg2=(int)heightAvg1;
		System.out.println(heightAvg1);
		System.out.println(heightAvg2);
	}
}

常量的应用 final

public class HelloWorld{
    public static void main(String[] args) {
		
		final String LOVE = "IMOOC";
		final double num = 23.6;
		final char boy = '男';
		final char giil = '女' ;
		
		System.out.println(boy);
		System.out.println(giil);
		
	}
}

赋值运算符

public class HelloWorld{
    public static void main(String[] args) {
	    int one = 10 ;
        int two = 20 ;
        int three = 0 ;
        
        three = one + two;
        System.out.println("three = one + two ==> " + three);
        three += one;
        System.out.println("three += one ==> " + three);
        three -= one;
        System.out.println("three -= one ==> " + three);
        three *= one;
        System.out.println("three *= one ==> " + three);
        three /= one;
        System.out.println("three /= one ==> " + three);
        three %= one;
        System.out.println("three %= one ==> " + three);        

	}
}

比较运算符

public class HelloWorld{
    public static void main(String[] args) {
		int a=16;
		double b=9.5;
		String str1="hello";
		String str2="imooc";
		System.out.println("a等于b:" + (a ==  b));
		System.out.println("a大于b:" + (a > b));
		System.out.println("a小于等于b:" + (a <=  b));
		System.out.println("str1等于str2:" + (str1  == str2));
	}
}

逻辑运算符

public class HelloWorld {
    public static void main(String[] args) {
		boolean a = true; // a同意
		boolean b = false; // b反对
		boolean c = false; // c反对
		boolean d = true; // d同意
	
    System.out.println((a&&b) + "未通过");
    System.out.println((a||b) + "通过");
    System.out.println((!a) + "未通过");
    System.out.println((c^d) + "通过");
	}
}

条件运算符

public class HelloWorld{
    public static void main(String[] args) {
		int score=68;
		String mark = score > 60 ? "及格" : "不及格";
		System.out.println("考试成绩如何:"+mark);
	}
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JwCode

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

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

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

打赏作者

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

抵扣说明:

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

余额充值