java小白训练营2109-day02-基础语法:关键字+变量+常量+运算符

关键字和标识符

关键字

java中定死的一些名称,英文的单词,这些单词你不能使用,作为开发语言保留字,只能它自己来使用。
关键字必须背过
在这里插入图片描述

标识符

标识符是开发者可以自行定义的名称,类名,方法名,变量名称
1)英文
2)数字,支持数字,数字必须放在字母后面
3)特殊符号,支持个别特殊字符$_,但大多数不支持
4)中文,虽然支持,不让使用,第一不标准,第二会被鄙视

变量和常量

变量

变化的量,能修改

常量

不变化的量,固定的值,不能修改 final

package cn.tedu.syntax;

public class Hello {
	//快捷方式:main+Alt+/
	public static void main(String[] args) {
		//1、变量
		int age = 100;	//变量的定义
		age = 101; 	//变量重新赋值,变量会把原来的值覆盖掉
		age = 102;
		
		//打印快捷方式:sysout+Alt+/
		System.out.println(age);
		
		String firstName = "陈"; //小驼峰
		System.out.println(firstName);
		
		//2、常量 final修饰
		final String address = "北京中关村";
		/*
		 * The final local variable address 
		 * 使用final修饰的局部变量address不能被分配内存空间
		 * cannot be assigned.  不能被创建
		 * It must be blank and
		 *  not using a compound assignment
		 */
		
		//address = "北京";
		System.out.println(address);
		
		//常见的常量的例子
		final double PI = 3.1415926;	//π
		final double GOLDEN = 6.18;  //黄金分割率
		final String SYSTEM_NAME = "京淘电商";
	}
}

运算符

计算机最大的作用就是实现数学计算

数学不好能不能学it?

可以,±*/%
日常应用程序开发,数学不好没有任何关系的,
但是作为大数据工程师,数据分析,数据挖掘,人工智能(算法:概率论、高数、离散)
机器学习,深度学习,背后都是算法

常见运算符

在这里插入图片描述

算术运算符

package cn.tedu.syntax;

//算术运算符:加减乘除、取余%
public class Arith {
	public static void main(String[] args) {
		int a = 10;
		int b = 3;
		
		//a = a+b;  //a=12
		
		System.out.println("加法:" + (a+b));
		//The operator - is undefined for the argument type(s) String, int
		System.out.println("减法:" + (a-b));
		
		System.out.println("乘法:" + a*b);
		System.out.println("整除:" + a/b); //整除
		
		//取余
		System.out.println("取余:" + a%b);
	}
}


package cn.tedu.syntax;

//算术运算符:自加 ++、自减 --
public class Self {
	public static void main(String[] args) {
		int a = 100;
		a++; //a=a+1;
		System.out.println(a);
		System.out.println(--a);  //100
		
		int b = 88;
		System.out.println(++b);  //先加后打 89
		System.out.println(b++);  //先打后加 89
	}
}

赋值运算符

package cn.tedu.syntax;

//赋值运算符:+=、-=、*=、/=
public class Assignment {
	public static void main(String[] args) {
		int x = 100;
		x += 10;  //步长10,x=x+10;
		System.out.println(x);
		
		x -= 2;  //x = x-2;
		System.out.println(x);
		
		x *= 2;	 //x = x*2;
		System.out.println(x);
		
		x /= 10; //x = x/10;
		System.out.println(x);
	}
}

连接运算符

package cn.tedu.syntax;

public class Concat {
	public static void main(String[] args) {
		int a = 5;
		String s = "10";
		s = "abc";
		
		/*
		 * 1.先判断+两边有没有字符串,有字符串就是连接符
		 * 2.再把整数a,先转换为字符串(java自动转换)
		 * 3.把两个字符串拼接字符串(拼串)
		 */
		System.out.println( a+s ); //字符串
	}
}

关系运算符

package cn.tedu.syntax;

//关系运算符:==、!=,也叫比较运算符,结果布尔类型true/false
public class Relation {
	public static void main(String[] args) {
		int a = 100;
		int b = 200;
		System.out.println( a==b ); //false
		System.out.println( a!=b );	//true
	}
}

逻辑运算符

package cn.tedu.syntax;

//逻辑运算符:非、与、或、亦或
public class Logic {
	public static void main(String[] args) {
		boolean b = false;
		System.out.println( !!b );
		
		//与,两边都成立才成立
		boolean b1 = true;
		boolean b2 = false;
		System.out.println( b1 & b2 );
		
		//Ctrl+c 复制代码,Ctrl+v 粘贴代码
		
		System.out.println("-----------");
		
		//与,特点:两边都是true,结果才为true
		System.out.println( true && true );	 //true
		System.out.println( true && false ); //false
		System.out.println( false && true ); //false 短路
		System.out.println( false && false );//false 短路
		
		System.out.println("-----------");
		
		//或,特点:两边只要有一边true,结果就是true
		System.out.println( true || true );	 //true 短路
		System.out.println( true || false ); //true 短路
		System.out.println( false || true ); //true
		System.out.println( false || false );//false
		
		//短路与和或推荐,它的后部分无需计算,直接跳过,它的效率高
		
		//案例:分数阶段判断 score 90~100 优秀 100~120 卓越
		//判断:score>=90,score<=100 与
		int score = 108;
		if( score>=90 & score<=100 ) {		//小括号内为布尔表达式
			//true
			System.out.println("优秀");
		}else if( score>100 & score<=120) {
			System.out.println("卓越");
		}else {
			//false
			System.out.println("未知");
		}
	}
}

三目运算符

package cn.tedu.syntax;

//三目运算符(三元)
//特点:有三个部分组成,格式死板
//格式:布尔表达式 ? 值1(表达式): 值2(表达式)
//结果:如果布尔结果为true,返回值1,为false,返回值2
public class Ternary {
	
	public static void main0(String[] args) {
		//需求:两个值求最大值
		int x = 100;
		int y = 200;
		int max = x>y ? x : y;
		System.out.println("最大值:" + max);
		
		//需求:三个值求最大值
		int z = 500;
		max = max>z ? max : z;
		System.out.println("最大值:" + max);
	}
	
	public static void main(String[] args) {
		int x = 100;
		int y = 600;
		int z = 500;
		
		//x>y ? x : y
		int max = (x>y ? x : y)>z ? (x>y ? x : y) : z;
		System.out.println("最大值:" + max);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值