【Java】基本数据类型以及其转换
整理了一下Java基本数据类型和面试可能涉及的知识。
| 字节数(byte) | 位数(bit) | 取值范围 | ||
| 整型 | byte | 1 | 8 | -2^7 ~ 2^7 -1 |
| short | 2 | 16 | -2^15 ~ 2^15-1 | |
| int* | 4 | 32 | -2^31 ~ 2^31-1 | |
| long | 8 | 64 | -2^63 ~ 2^63-1 | |
| 浮点型 | float | 4 | 32 | |
| double* | 8 | 64 | ||
| 字符型 | char | 2 | 16 | 0~2^16-1 |
| 布尔型 | boolean | 1 |
整型的取值范围:
最高位为符号,正负各2^(位-1)个数,0归为正数,故正数范围为0~2^(位-1)-1,负数为-2^(位-1)~-1
浮点型的取值范围:
float和double的范围是由指数的位数来决定的。没有搞清楚这个,迟点复习再看。
https://www.cnblogs.com/BradMiller/archive/2010/11/25/1887945.html 这篇写得蛮好的
1. 基本数据类型之间的转换
参考:https://www.cnblogs.com/liujinhong/p/6005714.html
| 低精度 | →→ 自动转换 →→ | 高精度 | ||
| byte、char、short、int、long、float、double | ||||
| ←← 强制转换 ←← | ||||
由于Java中默认整型数据使用int,浮点型使用double,故书写规范:
byte a = 1; //自动转换
byte b = (byte)128; //数值超出范围需作强制转换
long c = 10000000000L; //强制转换
float d = 3.5f; //强制转换
double e = 3.5;
进行数学运算时,数据类型会转换为涉及数据的最大形式
int a = 2;
byte b = 2;
byte c = (byte)(a+b);
double d = 5;
char ch = (char)('a'+1);
char型运算时会自动提升为int类型
char a = 55;
char b = 'a';
char c = (char)(a+b);
char d = 'a'+'a';
System.out.println(a);//7
System.out.println(b);//a
2. 基本数据类型的包装类及其转换
| 基本数据类型 | boolean | char | byte | short | int | long | float | double |
| 包装类 | Boolean | Character | Byte | Short | Integer | Long | Float | Double |
装箱与拆箱
Integer i = 10; //装箱 基本类型 → 包装器类型
int n = i; //拆箱 包装器类型 → 基本类型
查看对应.class文件可发现,以上代码实际调用的方法:
Integer i = Integer.valueOf(10);
int n = i.intValue();
① Integer(指向缓存/常量池) 和 new Integer(指向堆)由于指向内存位置不一样,比较时两者不等。
② Integer(非new)互相比较时,数值位于int取值范围内,比较结果为true,超出范围则为false
③ int和Integer/new Integer比较时,由于Integer会首先作拆箱处理与int比对,所以比较结果一定为true
注:Double比较有所差别,所有Double的比较都为false
详细可参详:https://www.cnblogs.com/dolphin0520/p/3780005.html
相关题目:
1.
short s1 =1; s1 = 1+1; short s1 = 1; s1 +=1; 上述语句能否编译通过,为什么?
short s1 = 1;
s1 = 1+1; //计算时会转换成int类型,由于左方是short类型的数据,右边运算完没有进行强制转换,所以编译不通过 short s1=1;
s1 += 1; //因为“+=”是赋值运算符,不牵涉与其它类型的数字计算,也不会转成 int 类型的,所以编译通过
【Java】基本数据类型以及其转换的更多相关文章
- Java基础——数据类型之间的转换
Java数据类型分为三大类,即布尔型.字符型和数值型.其中数值型又分为整型和浮点型.Java的基本数据类型(8种)为布尔型boolean(1字节):字符型char(2字节):整型byte(1字节).s ...
- java各种数据类型之间的转换
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseIn ...
- java --基本数据类型间的转换
public class changetype { public static void main(String[] args) { String ar = "true"; //S ...
- java代码---数据类型的强制转换----不懂啊
总结:看写的测试代码 字符到整型必须进行强制转换 package com.a.b; //byte→int 可以 int范围大,不必转换 B.short→long //C.float→double 这个 ...
- java 各数据类型之间的转换
String —> Date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date ...
- java 各种数据类型的互相转换
StringBuilder转化为String StringBuilder stb = new StringBuilder(); String str=stb.toString(); //方法1 Str ...
- Java中数据类型及其之间的转换
Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种:1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits).2 ...
- java中的、标识符、运算符以及数据类型之间的转换。
---恢复内容开始--- 数据类型之间的转换: 1:自动转换:就是不用说出要转换成什么类型,由java中的虚拟机自动将小数据类型转换成大数据类型,但大数据中的数据精度有可能被破坏. 2:强制转换:强制 ...
- Java中的基本数据类型和基本数据类型之间的转换
在Java中有8中基本数据类型,分别为: 整型: byte.short.int.long 浮点型:float.double 布尔型:boolean 字符型:char. byte: 8位, 封装 ...
- Java中数据类型及其之间的转换(转)
Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种:1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits).2 ...
随机推荐
- 自定义滚动条插件 mCustomScrollbar 使用介绍
引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...
- chapter06
/** * Created by EX-CHENZECHAO001 on 2018-03-30. */class Chapter06 { } // 6 对象// 用对象作为单例或存放工具的方法// 类 ...
- jdbc取出表名 名称
package com.dataconnect.test.util; import java.sql.Connection; import java.sql.DatabaseMetaData; imp ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- OpenStack local.conf
# Sample ``local.conf`` for user-configurable variables in ``stack.sh`` # NOTE: Copy this file to th ...
- C++中的swap函数
最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符 template <class T> void swap ( T& a, T& b ) { T c(a); a ...
- Reactor Pattern and Non-blocking IO--reference
reference from:http://www.cs.bgu.ac.il/~spl051/Personal_material/Practical_sessions/Ps_12/ps12.html ...
- 项目上传至Github
到https://github.com/ 注册用户,然后点 Start a project,创建仓库 记住这个 地址. 再去 https://git-scm.com/downloads 下载git 安 ...
- 19.CentOS7下PostgreSQL安装过程
CentOS7下PostgreSQL安装过程 装包 sudo yum install postgresql-server postgresql-contrib 说明: 这种方式直接明了,其他方法也可以 ...
- Java集合框架—Map
Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V& ...