Java数据类型、直接量、变量的命名、类型转换、运算符和三种控制结构 、continue 、break

数据类型

名称类型字节数位数
布尔型boolean18
字符型char216
短整型short216
整型int432
长整型long864
单精度浮点型float432
双精度浮点型double864
字符串类型String

直接量

boolean的直接量为 truefalse
char 的直接量有四种写法:

  1. 采用ASILL码整数直接量 : 65 (asciil码65是A)
  2. 单个字符:‘a’
  3. Unicode 字符:’\u0061’, ‘\u005a’
  4. 转义字符:换行 ‘\n’, 制表 ‘\t’, 回车 ‘\r’, 退格 ‘\b’, 换页 ‘\f’, 单引号 ‘’’, 双引号’"’ 和 反斜杠 ‘\’

短整型:(short)123
整形有三种格式:

  • 十进制:123
  • 八进制:0123
  • 十六进制:0x1a(在十进制为26)

长整形:-7L
单精度浮点型:1.23f, -1.23f;
双精度浮点型:1.23或1.23d, -1.24或-1.24d

例子: 数据类型与直接量

	class test{
		public static void main(String args[]){
			boolean a = true;
			char b1 = 65;
			char b2 ='a';
			char b3 = '\u005a';
			short c = (short )123;
			int d1 = 123;
			int d2 = 0123;
			int d3 = 0xabc;
			long e1 = 123L;
			long e2 = 0123L;
			long e3 = 0x123L;
			float f = 1.23f;
			double g = 1.34;
			double g1 = 1.34d;
			System.out.println("b1的值为: "+b1);
		}
	}
			

变量的命名

  • 开头只能是字母和下划线:如 _a , hello …
  • 变量可以是字母、数字和下划线组成,不能是其他特殊符号
    可以用函数Character.isJavaIdentifierStart( )判断
public class test{
	public static void main(String args[]){
		char c = '猫';
		if(Character.isJavaIdentifierStart(c){
			System.out.println("字符\'"+c+"\'可以做标识符的首字母");
		else 
			System.out.println("字符\'"+c+"\'不可以做标识符的首字母");
		if(Character.isJavaIdentfierPart(c)){
			System.out.println("字符\'"+c+"\'可以做标识符除首字符外的组成字符");
		else 
			System.out.println("字符\'"+c+"\'不可以做标识符除首字符外的组成字符");
	}
}

类型转换

隐式转换和显示转换。
隐式转换:同一类型中低字节数可以自动转换成高字节数,如 short a = 123,int b =a;
显示转换:int a = 123 char b = (char)a;

class test1{
	public static void main(String args[]){
		int a = 123;
		long b = a;//隐式转换
		System.out.println("b的值为:"+b);	//结果为123
		a = 65;//后面的a会覆盖前面的a
		char c = (char)a;//显式转换或强制转换,转换为Assill码65,对应是A
		float d = (float)a;//显式转换
		System.out.println("c的值为:"+c);
		System.out.println("c的值为:" +d);
}

运算符及运算符

  • 算式运算符: ()、 * 、 /、 %、 +、 - 、++、- -、*=、/=
  • 关系运算符:< 、 <= 、 > 、 >=、==、!= 、
  • 布尔逻辑运算符:&&、||、!
  • 位运算符:&、|、~、^、>>、>>>、<<
  • 条件运算符: ?:

注意:乘方用乘方函数pow(2,3)就是2的3次方。

	class test{
		public static void main(String args[]){
			int a = 2;
			int b = a + 2*3;
			int c = 4/2; 	//除法,整数类型结果为整数2
			int c1 = 6/4;	//结果为商1
			int c2 = 2/4;	//结果为商0
			int d = 4%3;	//取余,结果为余数1

			int n = 3;
			n++;	//相当于n=n+1
			n--;	//相当于n=n-1;
			++n;	//相当于n+1,然后n=n+1;
			--n;	//相当于n-1,然后n=n-1;

			n*=2;	//相当于n=n*2;
			n/=2;	//相当于n=n/2;
			
			if(a > 1){
				System.out.println("a 大于1 ");
			}
			else if(a == 1 ){
				System.out.println("a 等于1 ");
			}
			else {
				System.out.println("a 小于1 ");
			}

			if(b > 0 && b !=5){
				System.out.println("b大于0同时b不等于5");
			}
			
			if(c == 1 || c == 2){
				System.out.println("c等于1 或者c等于2时执行该语句");
			}
			
			if(c==2? m =1:m=2){
				System.out.println("m的值:"+m);
			}
			if(c==1 ? m =1:m=2){
				System.out.println("m的值:"+m);
			}

			double h1,h2,h3,h4,h5;
			h1 = 1+4/5+(int)5.2/2.5;	//1+0+5/2.5=3.0
			h2 = 1+4/5+(int)(5.2/2.5);	//1+0+2=3.0
			h3 = 1+4/5+5.2/2.5;	//1+0+2.08=3.08;
			h4 = 1.0+4/5+5.2/2.5;	//1.0+0+2.08=3.08
			h5 = 1+4/5.0+5.2/2.5;	//1+0.8+2.08=3.88
			System.out.println(+h1);
		

		}
	}

三种控制结构

  1. 顺序结构:全局默认
  2. 选择结构:if……;if……else……;switch(){case __: ……}
  3. 循环结构:for语句,while语句,do……while语句

break语句

break语句跳出循环体,往下面语句执行

continue语句

continue 语句是跳过本次循环,回到循环开头

class test{
		public static void main(String args[]){
				char grade= 'a';
				switch(grade)
				{
				case 'A':
				case 'a':
					System.out.println("优秀”);
					break;	//跳出循环体
				case 'B':
				case 'b':
					System.out.println("良好!");
					break;	//跳出循环体
				case 'C':
				case 'c':
					System.out.println("及格");
					break;
				case 'D':
				case 'd':
					System.out.println("不及格");
					break;
				default:
					System.out.println("成绩有误!");
				}
		}
}
除了3的倍数的0~10的数加起来
public class test{
	public static void main(String args[]){
		int sum=0;int i=0;
		for (int i =0; i<=10;i++){
			if(i%3==0){
				continue;
			}
			sum +=i;
			System.out.println("sum的和为:"+sum);
		}

		//while
		while(i<=10{
			if(i%3==0){
				continue;
			}
			sum +=i;
		}

	//do……while语句
	do{
		if(i%3==0){
			continue;
		}
		sum +=i;
	}while(i<10);
	}
}

注意: while与do……while的区别:
while先判断条件,在执行循环体。
do……while是先执行一次循环体,在判断条件,再执行循环体。
只要条件相同,执行超过一次循环,那么while与do……while执行的效果是一模一样的。

扩展:
输入函数:Scanf s =new Scanf(“请输入:”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值