Java基础02:符号、数据转换

1 注释、标识符、关键字

1.1 注释

Java中有三种:

单行注释: //注释内容
多行注释: /* 注释内容 */
文档注释: /** 注释内容 /

注释修改

Editor编辑器 Color Scheme颜色方案 Comments注释

在这里插入图片描述

1.2 标识符及命名原则

Java 所有的组成部分都需要名字。
类名变量名以及方法名都被称为标识符

// Hello是类名
public class Hello {
    // main是方法名
    public static void main(String[] args) {
        // job是变量名
        String job = "IT";
        System.out.println("Hello,World!");
    }
}

1.2.1 标识符命名原则

查看相关变量命名规范
(1) 所有的标识符都以字母(A~Z或a-z),美元符( ) 、下划线 ( ) 开始; ( 2 ) 首字符之后可以是字母 ( A   Z 或 a − z ) ,美元符 ( )、下划线(_) 开始; (2) 首字符之后可以是字母(A~Z或a-z),美元符( )、下划线()开始;(2)首字符之后可以是字母(A Zaz),美元符()、下划线(_) 、或数字的任何字符组合;
(3) 不能使用关键字作为变量名或方法名
(4) 标识符是大小写敏感
(5) 合法标识符举例:age$salary_value__1_value
(6) 非法标识符举例:123abc-salary#abc等,开头不是英文字母$_的。
(7) 可以使用中文 命名,但是一般不建议使用(,也不建议是用拼音)。

// 下面是合法命名(大小写敏感型)
        String hobby = "IT";
        String Hobby = "IT";
        String $hobby = "IT";
        String _hobby = "IT";
        String 爱好 = "IT";
        String aihao = "IT";

        /* 下面是非法命名
        String 1hobby = "IT";
        String -hobby = "IT";
        String #hobby = "IT";
        String _#hobby = "IT";
        */

1.3 关键字

在这里插入图片描述

2 数据类型

2~4连着看
**Java是强类型语言:**要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用

2.1 Java数据类型分类:

基本数据类型(primitive type)
引用数据类型(reference type)
注意: String(字符串)不是关键字,是类
数据类型

2.2 基本数据类型

2.2.1 数值类型—整数型

类型字节取值范围(二进制)取值范围(十进制)
byte1字节-27~27-1-128~127
short2字节-215~215-1-32768~32767
int4字节-231~231-1-2147483648~2147483647
long8字节-263~263-1-9223372036854775808~9223372036854775807
  • 注意:int为整数的默认类型,如需为 long类型赋值,需要在值的后面追加 L
/**
 *	整型数据类型 四种  byte short int long
 */
public class Test2{
	public static void main(String [] args){
		// byte -128 ~ 127 
		byte b1;
		b1 = 20;
		System.out.println(b1);
		byte b2 = 127; // 赋值超过byte取值范围的数 编译出错 从int转换为byte会有损失 
		System.out.println(b2);
		
		// short 取值范围 -32768 ~ 32767
		short s1 = 200;
		short s2 = 32767; // 赋值超过short取值范围的数 编译出错 从int转换为short会有损失
		System.out.println(s1);
		System.out.println(s2);
		
		// int 取值范围 -2147483648 ~  2147483647
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		
		int i1 = -200;
		// 赋值超过int范围的数 编译出错 过大的整数
		int i2 = 2147483647; 
		System.out.println(i1);
		System.out.println(i2);
		
		
		// long 类型  -9223372036854775808 ~ 9223372036854775807
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MIN_VALUE);
		// 如果取值范围没有超过int  L 可加可不加
		long l1 = 2000L;
		// 当long类型取值超过int范围的情况 必须在值末尾加上L,大小写都可以,推荐大写
		long l2 = 2147483648L;
		System.out.println(l1);
		System.out.println(l2);	
	}
}

2.2.2 数值类型—浮点型

基本数据类型(小数/浮点数)

类型字节负数取值范围正数取值范围
float4字节-3.4E+38~-1.4E-451.4E-45~-3.4E+38
double8字节-1.7E+308~4.9E-3244.9E-324~1.7E+308
  • 浮点型数值采用科学计数法表示:
    2E3 等价于 210^3 (结果:2000.0)
    3E5 等价于 3
    10^5 (结果:300000.0)
  • 注意:double为浮点数的默认类型(常用于毫秒),如需要 float类型赋值,需要在值得后面追加 “F”。
/**
 *	浮点类型(小数) double float 所有的小数默认为double类型
 */
public class Test4{
	public static void main(String [] args){
		// float 负数取值范围
		float f1  = -340000000000000000000000000000000000000F;
		System.out.println(f1);
		
		float f2 = -0.0000000000000000000000000000000000000000000014F;
		System.out.println(f2);
		
		// float正数取值范围1.4E-45 ~ 3.4E+38
		float f3 = 0.0000000000000000000000000000000000000000000014F;
		System.out.println(f3);
		
		
		float f4 = 340000000000000000000000000000000000000F;
		System.out.println(f4);
	}
}

2.2.3 数值类型—字符型

1.char类型 字符类型 取值范围为 0~ 65535

2.取值方式 可以直接使用英文的单引号 赋值一个字符、字母、数字、特殊符号、中文等

​ 也支持直接赋值整数

3.同时还支持使用十六进制的Unicode(万国码)编码赋值

类型字节取值范围(无符号数)字符编码
char2字节0~65535Unicode字符集(万国码)
  • Unicode字符集支持 ASCII编码(美国信息交换标准代码)
  • Unicode中每个字符都对应一个十进制整数,从而可以使用多种方式赋值。

字符赋值:char ch1 = ‘A’;(通过’'描述为字符赋值)
整数赋值:char ch2 = 65; (通过十进制数 65在字符集中对应的字符赋值)
进制赋值:char ch3 = ‘\u0041’;(通过十六进制数 41在字符集中所对应的字符赋值)

public class Test5{
	public static void main(String [] args){
		char ch1 = 'A';
		char ch2 = '1';
		char ch3 = '中';
		System.out.println(ch1);
		System.out.println(ch2);
		System.out.println(ch3);
		
		char ch4 = 65;
		System.out.println(ch4);
		
		char ch5 = 66;
		System.out.println(ch5);
		// 直接赋值整数 将参考ASCII 码表(美国标准信息交换码)
		
		char ch6 = 97;
		System.out.println(ch6);
		
		
		char ch7 = 20013;
		System.out.println(ch7);
		
		char ch8 = 20320;
		System.out.println(ch8);
		
		// char ch9 = 0;
		
		// 使用Unicode编码 (万国码)表来映射对应的文字 
		// 中文的取值范围  \u4e00 ~ \u9fa5  
		char ch9 = '\u597d';
		System.out.println(ch9);
	}
}

2.2.4 布尔类型

基本数据类型(布尔)-Boolean

类型字节取值范围描述
Boolean1字节truce/false仅可描述“真”或者“假”
  • 可直接赋值 true/false
  • 也可赋值一个结果为 true/false 的表达式
  • 注意:Java中的 Boolean不能参与 算术运算
/**
 *	布尔类型 取值只有真true 或者假 false
 */
public class Test3{
	public static void main(String [] args){
		// 直接赋值为 布尔类型的值
		boolean bl1 = true;
		boolean bl2 = false;
		
		System.out.println(bl1);
		System.out.println(bl2);
		
		int a = 20;
		int b = 30;
		// 赋值为布尔类型的表达式
		boolean bl3 = a > b;
		boolean bl4 = a < b;
		System.out.println(bl3);
		System.out.println(bl4);
		
		
		// System.out.print(a + bl1); 不能参与算数运算
	}
}

2.2.5 补充:字节、单位换算

位(bit):是计算机内部数据存储的最小单位,如11011010是8位二进制数;
字节(byte):是计算机种数据处理的基本单位,习惯上用大B 来表示,1B(byte,字节) = 8bit;
字符:是指计算机中使用的字母、数字、字和符号;
单位换算:
1bit = 1位;
1Byte = 1B = 8b;
1KB = 1024B;
1M = 1024KB;
1G = 1024M.

32位的电脑和64位的电脑 的区别是:
寻址能力不同;
可扩展内存不同:32位电脑最多是4G,64位电脑是128G常见 无上限;
系统不同:32位电脑只能装32位系统,64位电脑 32位和64位系统都可以装载;

2.2.6 转义字符:\t、\n

所有的字符 其本质还是数字。

编码,最常见的是 Unicode表 2个字节 范围0~65536

  \t:制表符
  \n:换行
  ...
public class demo00 {
    public static void main(String[] args) {
        /**
         * @description 字符拓展
         */
        char F01 = 'a';
        char F02 = '中';
        System.out.println(F01);//a
        System.out.println((int) F01);//97
        System.out.println(F02);//中
        System.out.println((int) F02);//20013
        /**
         * @description 转义字符
         */
        char F03 = '\u0062';
        System.out.println("Hello\tWorld");//Hello	World
        System.out.println(F03);//b
    }
}

2.2.7 数值拓展、进制表示

进制:二进制0b开头;八进制0开头;十进制;十六进制0x开头

public class demo02 {
    public static void main(String[] args) {
    ·	/**
         * @description 整数拓展
         */
        int num01 = 10;
        int num02 = 010;//八进制 0
        int num03 = 0x10;//十六进制 0x
        System.out.println(num01+";"+num02+";"+num03+"------");//10;8;16

        float num04 = 0.1f;
        double num05 = 1.0/10;
        System.out.println(num04 == num05);//false
        System.out.println(num04);//0.1
        System.out.println(num05+"========");//0.1

        /*float 是有限个的离散值,其值是近似等于而不是确定等于,
        * 金融业钱的表示不用浮点数,而用 BigDecimal
        */
        float num06 = 52565624565f;
        float num07 = num06 + 1;
        System.out.println(num06 == num07);//true
        System.out.println(num06);//5.2565623E10
        System.out.println(num07);//5.2565623E10
    }
}

注意事项:金融业钱的表示不用浮点数,而用 BigDecimal

2.3 引用数据类型-string类

引用数据类型(字符串、接口和数组)

类型取值范围字符编码
String类任何" "之间的字面值Unicode字符序列
接口————
数组————
  • String 类型的字面取值

String str1 = “你好”;
String str2 = “Hello”;
String str3 = “分布式架构师”;
String str4 = “Java Engineer”;

/**
 *	String类型 字符串 是JDK提供的一个类 任何在英文双引号中间的内容都是 字符串
 */
public class Test7{
	public static void main(String [] args){
		String str1 = "A";
		String str2 = "10";
		String str3 = "世界你好";
		String str4 = "he  sahwq dskwqe cakdewqennxssed e we qewq ";
		System.out.println(str1 + str2);
		System.out.println(str4);
	}
}

2.4 new对象和 Boolean 扩展

new对象 和不new对象的区别,需要从内存分析

public class demo00 {
    public static void main(String[] args) {
        //new对象 和不new对象的区别
        String str01 = new String("helloWorld");
        String str02 = new String("helloWorld");
        String str03 = "helloWorld";
        String str04 = "helloWorld";
        System.out.println(str01 == str02);//false
        System.out.println(str03 == str04);//true

        //boolean扩展
        boolean flag = true;
        //代码要精简易读
        if (flag){} //默认为if (flag==true){}
    }
}

3 类型转换

3.1 类型转换分类

强制转换:是把高容量转换成低容量;
自动转换:是把低容量转换成高容量;

强制转换, 容量:up
自动转换, 容量:down
A
B
byte,short,char
int
long
float
double

注意点:
1.不能对 Boolean(布尔值) 进行转换;
2.不能把对象类型转换成不相关的类型;
3.强制转换的时候可能存在内存溢出丢失精度问题

3.2 数据类型转换

3.2.1 自动类型转换

转换规则:

1.自动类型转换两种类型要兼容

2.目标类型(等号左边的类型) 大于 (等号右边的)类型

public class Test8{
	public static void main(String [] args){
		// 自动提升
		// short s1 = 65;
		// 从short转换为char会有损失 因为short有可能为负数 而char是无符号的字符
		// char ch1 = s1; 
		
		byte b1 = 20;
		short s1 = b1;
		System.out.println(s1);
		
		short s2 = 33;
		int i1 = s2;
		System.out.println(i1);
		
		int i2 = 55;
		long l1 = i2;
		System.out.println(l1);
		
		int i3 = 550;
		float f1 = i3;
		System.out.println(f1);
		
		short s3 = 452;
		double d1 = s3;
		System.out.println(d1);
    }
}

3.2.2 强制类型转换

转换规则:

1.强制类型转换两种类型要兼容

2.目标类型(等号左边的类型) 小于 (等号右边的)类型

/**
 *	强制类型转换  */
public class Test8{
	public static void main(String [] args){
		// 手动下降
		short s4 = 125;
		byte b2 = (byte)s4;
		System.out.println(b2);
		
		int i4 = 5566;
		short s5 = (short)i4;
		System.out.println(s5);
		
		long l2 = 55221;
		int i5 = (int)l2;
		System.out.println(i5);
		
		float f2 = 31.4F;
		int i6 = (int)f2;
		System.out.println(i6);

		double d2 = 20.5;
		long l3 = (long)d2;
		System.out.println(l3);

	}
}
2.1 强制类型转换特殊情况
package com.my.test; // 此处为包的声明
public class Test1 {
	public static void main(String [] args) {
		// 1.丢失精度问题
        // 强制转换,容量由高到低 double-->int
        System.out.println((int) 12.2);// 12
        // 强制转换,容量由高到低 float-->int
        System.out.println((int) 3.14f);// 3
		
		// 2.超出范围的强制类型转换,符号位发生改变
		short s4 = 128;
		byte b3 = (byte)s4;
		System.out.println(s4+"\t"+b3);//128	-128: -128是由于内存溢出造成的
		
		// 3.超出范围 数据截断
		short s5 = 520;
		byte b4 = (byte)s5;
		System.out.println(b4);
		
		// 4.负数 以补码形式来表示
		short s6 = 130;
		byte b5 = (byte)s6;
		System.out.println(b5); 
		
		// 5.超出char取值范围 不能显示正常字符 
		int i = -1;
		char ch1 = (char)i;
		System.out.println(ch1);  
		
		double d1 = 20.5;
		System.out.println(d1 + "220");	
	}
}
2.2 数值较大时注意内存溢出
public class demo03 {
    public static void main(String[] args) {
        //JDK7新特性,数字之间可以使用下划线
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;
        System.out.println("total= "+total);
        long total1 = money*years;
        System.out.println("total1= "+total1);
		
		// 注意Long类型数据在数据结尾,尽量使用大写 L
        long total2= money*(long)years;
        System.out.println("total2= "+total2);
    }
}
2.3 原码、反码、补码
正数的原码 反码 补码 都一样  3 0011  0011  0011
负数的原码:
	原码	-3  1000 0011	
	反码	-3  1111 1100
	补码	-3  1111 1101
3.3 进制转换
十进制的数值转化为二进制 怎么转换 如何显示 
	// 	比如  
	//	3  0000 0011
	// 	6  0000 0110

下一篇:Java基础03:变/常量、运算符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值