Java编程入坑二(数据类型)

7 篇文章 0 订阅

1.数据类型

1.1float和long类型的赋值操作

public class Test1 {

	public static void main(String[] args) {
		
		/**
		 * 第一个赋值:
		 * 为什么这个情况下可以呢
		 * 因为默认的整数类型都是int
		 * 又因为int类型比float类型所占的字节空间小
		 * 所以是可以直接赋值的
		 * 
		 * 第二个赋值:
		 * 不可以的原因是因为
		 * 小数默认类型都是double类型
		 * 又因为double类型字节长度比float大,
		 * 随意不能直接进行赋值
		 * 
		 * 思考:
		 * 	long是8个字节
		 * 	float是4个字节
		 * 
		 * 如果我把long类型的数据赋值给float
		 * 能成功吗?
		 * 记住:浮点型的字节存储和整形的字节存储是不一样的
		 * 		所以浮点型的字节空间不能直接跟整形进行比较
		 * 
		 * float的取值范围:-3.4E38----3.4028235E38
		 * long的取值范围:-9223372036854775808~9223372036854775807 
		 * 
		 * 通过对比发现,float类型他的取值范围要远远大于long类型
		 * 所以这样的赋值是没有问题的
		 * 
		 */
		float a = 15;
		float b = 15.0f;
		
		float c = 250L;
	}

}

1.2.char类型的赋值

-	char类型打印字符和数字的区别
-	编码表的衍变
参考网址:
	http://www.96yx.com/tool/ASC2.htm

1.3.强制类型转换之内存中到底做了什么

-	强制类型转换的格式
-	强制转换类型后的结果如果取出

public class Test3 {
	public static void main(String[] args) {
		byte b1 = 3;
		byte b2 = 4;
		
		byte b3 = (byte) (b1 + b2);
		
		
		//有没有问题?有问题如何解决?结果是多少呢?
		byte  by = (byte) 130;

		
		System.out.println(b3);
		System.out.println(by);
	}
}

1.4.代码的执行顺序

public class Test4 {
	public static void main(String[] args) {
		
//		请写出下列程序结果
//		char   short   byte  在进行运算的时候  会默认提升为int类型
//		字符串跟任何类型进行+都是拼接操作
		
//		代码的执行顺序    从上往下   从左往右
		System.out.println('a');//a
		System.out.println('a'+1);//98
		System.out.println("hello"+'a'+1);//helloa1    helloa + 1  --->   helloa1
		System.out.println('a'+1+"hello");//98hello
		System.out.println("5+5="+5+5);//5+5=55
		System.out.println(5+5+"=5+5");//10=5+5
		
		
//		想要去使用谁,就要先去定义一下
		int a = 33;
		System.out.println(a);

	}
}

2.运算符

2.1算数运算符

  • + - * /的使用

        public class Test5 {
        	public static void main(String[] args) {
        		/**
        		 * +  -   *   /  %(取余数)
        		 * 
        		 * ++ (自动+1)  --(自动减1)
        		 */
        		int a = 14;
        		int b = 5;
        		
        //		int c = a + b;
        //		int d = c + a;
        //		
        //		System.out.println(a + b); //19
        //		System.out.println(a - b); //9
        //		System.out.println(a * b); //70
        //		System.out.println(a * 1.0 / b ); //2
        //		System.out.println(a % b); //4
        //		
        //		System.out.println(c);
        //		System.out.println(d);
        		
        //		System.out.println("--------------------------------");
        		
        //		这是后++
        		int c = a++;
        		int d = b++;
        //		这是前++
        //		int c = ++a;
        //		int d = ++b;
        		
        		System.out.println(c);//15  
        		System.out.println(d);//6	 
        		System.out.println("--------------");
        		System.out.println(a);//15
        		System.out.println(b);//6
        		
        		System.out.println("hello"+"world" + 123);
        		
        		
        		
        	}
        }
    
  • 前加加和后加加的区别

    • 单独使用的时候

      • 前++和后++是没有区别的
    • 参与运算时

      • 前++ 先自增后参与运算
      • 后++ 先参与运算然后自增
    • 案例运算

    • 1:基本小题目
      	int a = 10;
      	int b = 10;
      	int c = 10;
      
      //前++和后++   这里也是需要重点记忆的
      	a = b++;
      	c = --a;
      	b = ++a;
      	a = c--;
      	请分别计算出a,b,c的值
      
      
      2:比较复杂的题目
      int a = 4;
      int b = (a++)+(++a)+(a*10);
      
      

2.2赋值运算符

- 如何使用赋值运算符
= ,
a+=b,  a = a+b
a-=b,  a = a-b
a*=b,  a = a*b
a/=b,  a = a/b
a%=b,  a = a%b
public class Test7 {
	public static void main(String[] args) {
//		赋值运算符
//		= , +=, -=, *=, /=, %=
		
//		创建变量   类型   变量名  =  初始化值
		String s1 = "焦晨涛";
		String s2 = s1;
		
		System.out.println(s1);
		System.out.println(s2);
		
//		int a = 3;
		
//		a = a + 3;
		
//		a += 3;
		
//		+= 和 -=可以做自动类型强制转换
		byte b = 3;
        
        //重点需要理解的地方
//		b = b + 3;
		b += 3;
		
		System.out.println(b);
		
		int i = 14;
		int y = 3;
		
		System.out.println(i -= y);
		System.out.println(i *= y);
		System.out.println(i /= y);
		System.out.println(i %= y);
	}
}

2.3关系运算符

public class Test8 {
	public static void main(String[] args) {
		/*
		 * ==  和 equals
		 * !=
		 * <
		 * >
		 * <=
		 * >=
		 */
		
		int  a = 12;
		int b = 5;
		
		boolean bo = (a == b);
		
		System.out.println(a == b);//false
		System.out.println(a != b);//true
		System.out.println(a > b);//true
		System.out.println(a < b);//false
		System.out.println(a >= b);//true
		System.out.println(a <= b);//false
		
		System.out.println(bo);
		
		
		
	}
}

public class Test9 {
	public static void main(String[] args) {
		
//		1:注意2的时候举例
		
		int a = 12;
		int b = 4;
		
		System.out.println(a==b);//false
		System.out.println(a=b);//4

//	2:赋值的时候要注意类型问题
		int x = 10;
		int y = 10;
		boolean flag = (x == y);//true
		int flag1 = (x = y);//

		boolean b1 = true;
		boolean b2 = false;
		boolean b3 = (b1 == b2);//false
		boolean b4 = (b1 = b2);//false
		
		System.out.println(b3);
		System.out.println(b4);

	}
}

2.4逻辑运算符

2.4.1 & | ^ ! 的使用

public class Test10 {
	public static void main(String[] args) {
		/*
		 * 
		 * 逻辑运算符:
		 * &  AND   并且      true & true      true
		 * |   or   或者      true | false	   true
		 * ^   异或			  true ^ true   false     true ^ false   true
		 * !   非			  !true   false
		 */
		
		int a = 10;
		int b = 8;
		int c = 13;
		
		System.out.println(a > b & a < c);//true
		System.out.println(a < b | a > c);//false
		System.out.println(a < b ^ a < c);//true
		System.out.println(!(a < b));//true

		
		
	}
}
#### 	2.4.2短路运算符
		&&     ||
		
		&&  只要前面结果为false   就直接返回false  不在往后面执行
		||  只要前面结果为true    就直接返回true   不在往后面执行
		
		/*
		 * 
		 * 逻辑运算符:
		 * &  AND   并且      true & true      true
		 * |   or   或者      true | false	   true
		 * ^   异或			  true ^ true   false     true ^ false   true
		 * !   非			  !true   false
		 */
		
		int a = 10;
		int b = 8;
		int c = 13;
		
//		System.out.println(a > b & a < c);//true
//		System.out.println(a < b | a > c);//false
//		System.out.println(a < b ^ a < c);//true
//		System.out.println(!(a < b));//true
		
//		System.out.println(++a > b || ++c > b);
	
		
		System.out.println(++a < b && ++c > b);
		System.out.println(a);
		System.out.println(c);
		
		
		
		
#### 	2.4.3 &^|连接数字
	public class Test11 {
	public static void main(String[] args) {
		int a = 10;
		int b = 2;
		
		
//		一个数字同时异或另外一个数字两次  得到的结果是他本身
//		有什么作用  后期我们在讲临时变量或者冒泡  选择排序的时候
//		就会使用这种方式进行值得转换
		
		
		System.out.println(10&2);
		System.out.println(10|2);
		System.out.println(10^2^2);
		
	}
}

2.5位运算符

2.6三目运算符

三元运算符
格式:
	(关系表达式)?表达式1:表达式2;
	
	通过判断第一个关系表达式,然后选择执行后面的两个表达式
	
	3 > 4 ? "helloworld" : "hellojava";

3.键盘录入

import java.util.Scanner;

//需求:想要求三个数中的最大值
//想通过键盘自己录入数字
public class ScannerTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
//		sc.next();   接收一行数据
//		sc.nextInt()   接收一个int类型的值
//		sc.nextBoolean()  接收一个布尔值
//		sc.nextByte()   接收一个字节
//		sc.nextLine()  接收一行数据   也就是接收一行字符串
		
		System.out.println("请输入第一个数字");
//		sc.nextInt()这个函数是接收键盘录入的int类型的值
		int a = sc.nextInt();
		
		System.out.println("请输入第二个数字");
		int b = sc.nextInt();
		
		System.out.println("请输入第三个数字");
		int c = sc.nextInt();
		
		int d = (a > b && a > c) ? a : (b > c ? b :c);
		
		System.out.println("您输入的三个数中最大值为"+d);
		
	}
}
import java.util.Scanner;

/*
 * 需求2:
 * 	录入你的姓名和年龄
 * 	并且打印到控制台
 */
public class ScannerTest2 {
	public static void main(String[] args) {
//		1.创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
//		给用户一个友好的提示信息
		System.out.println("请输入年龄");
		
		int age = sc.nextInt();
		
//		给用户一个友好的提示信息
		System.out.println("请输入姓名");
		
//		2.接收姓名
		String name = sc.next();
		
		
		System.out.println("您的姓名为"+name + ",年龄为"+age);
		
	}
}

作业:

​ 猜数字游戏

​ 只有5次机会

​ 如果猜的数字大了 那就提示数字过大

​ 如果数字笑了 就提示过小

​ 猜中 结束循环

​ 每次给用户提示还剩余几次机会

eclipse的使用

eclipse中的一些配置:

​ 万能快捷键:alt + /

​ 常用快捷键:

​ 注释快捷键:ctrl +/

​ 快速整行复制: ctrl + alt + 上/下箭头

​ 快速移动整行代码:alt + 上/下箭头

​ 快速生成对象或者变量: ctrl + 2 松开 按 L

​ 代码格式化:ctrl + shift + f

​ 快速导包:ctrl + shift + o

​ 批量修改:alt + shift + a 退出再按一次快捷键就行了.js/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值