对于char,short和byte类型的运算
对于char,short和byte这些类型在计算时都会提升到int型来计算,所以a+b=3(这个3是int型的,所以我们需要将它强转成为byte类型,才不会出错。但是使用 += 或者 ++ 运算符可以执行隐式类型转换。
//精度高的转化为低的这叫向下转。
public class PlusEquals {
public static void main(String[] args) {
byte a = 1;
byte b = 2;
a = a + b;
System.out.println(a);
}
}
What does this program print?
Did you guess 3? Too bad, this program won't compile. Why? Well, it so happens that addition of bytes in Java is defined to return an int. This, I believe was because the Java Virtual Machine doesn't define byte operations to save on bytecodes (there is a limited number of those, after all), using integer operations instead is an implementation detail exposed in a language.
But if a = a + b doesn't work, that would mean a += b would never work for bytes if it E1 += E2 was defined to be E1 = E1 + E2. As the previous example shows, that would be indeed the case. As a hack to make += operator work for bytes and shorts, there is an implicit cast involved. It's not that great of a hack, but back during the Java 1.0 work, the focus was on getting the language released to begin with. Now, because of backwards compatibility, this hack introduced in Java 1.0 couldn't be removed.
对于char,short和byte类型的运算的更多相关文章
- C#中byte类型运算
首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...
- Java中Byte类型数据在运算中的问题
比如: byte a=1; byte b=2; byte c; c=a+b; //这样是计算不出c,是错误的 c=a+1; //这样也是不能计算c的 c=64+1; //为什么这样就能计算c,在Jav ...
- char,short ,int ,long,long long,unsigned long long数据范围
from:http://www.cnblogs.com/A123456A/archive/2013/01/25/2876634.html char,short ,int ,long,long long ...
- Java基本类型与运算
问题及答案来源自<Java程序员面试笔试宝典>第四章 Java基础知识 4.4基本类型与运算 1.Java提供了哪些基本数据类型? Java一共提高了八种原始的数据类型:byte.shor ...
- 【Java基础】基本类型与运算【重要】
0. Java基本数据类型 Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long.int.short.char和 byte,位运算符具体如下表 ...
- [置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long
这些数据类型的sizeof具体长度依赖于编译器和操作系统(32-bit or 64-bit) 1: 首先,参见c99标准 标准中没有定义这些数据类型的长度,而是定义了这些数据类型能表达的大小范围的最小 ...
- C#byte类型
byte类型的范围是0~255转换为二进制是00000000~11111111 ---------------------------------------------------------- C ...
- java基础 6 基本类型与运算
1 包装类型 Integer的缓存池为 -128 - 127: 八个基本类型 占bit 与字节 8 bit = 1 字节 boolean 1 byte 8 char ...
- byte类型的取值为什么是-128~127
参考:https://blog.csdn.net/qq_22771739/article/details/84496115 https://blog.csdn.net/boatalways/artic ...
随机推荐
- Vue基础(1)
Vue简介 1.JavaScript框架 2.简化Dom操作 3.响应式数据驱动 Vue基础 通过下面代码引用vue: <script src="https://cdn.jsdeliv ...
- vue自定义指令 默认图片
/** * 检测图片是否存在 * @param url */ function imageIsExist(url) { return new Promise((resolve) => ...
- day80:luffy:短信sdk接入&点击获取验证码&注册功能的实现&Celery实现短信发送功能
目录 1.短信sdk接入 2.前端点击获取验证码效果 3.注册后端接口实现 4.注册-前端 5.Celery 6.Celery完成短信发送功能 1.短信sdk接入 1.准备工作 1.下载云通讯相关的文 ...
- Hadoop高可用
一.原因 - NameNode是HDFS的黑心配置HDFS有事hadoop的核心组件 NameNode 在Hadoop及群众至关重要 - NameNode的宕机导致集群的不可用 二.解决方案 其中 N ...
- Apache Kylin远程代码执行漏洞复现(CVE-2020-1956)
Apache Kylin远程代码执行(CVE-2020-1956) 简介 Apache Kylin 是美国 Apache 软件基金会的一款开源的分布式分析型数据仓库.该产品主要提供 Hadoop/Sp ...
- Sql 解析XML 解决方案
1. 1.@XML 为数据传入的XML格式 2. root 为根目录 3. <A>为对应需要插入的表,详见一对多或者多对多的xml格式 ...
- 关于ubuntu出现的一些问题的解决方法
1. (1)现象: dpkg: 处理软件包 linux-image-4.15.0-36-generic (--configure)时出错: 子进程 已安装 post-installation 脚本 返 ...
- 使用css控制table的cellspacing和cellpadding属性
HTML默认的表格样式之间有间隙,每次为了解决这些问题,总要在table标签里添加cellspacing和cellpadding,你是否也很厌倦这样的写法, 那么有没有对应的CSS属性能达到相同的效果 ...
- JavaScript一元运算符、二元运算符和三元运算符
在JavaScript中,运算符可以根据其实际操作数的个数进行分类. JavaScript中的大多数运算符是一个二元运算符(binary operator),将两个表达式合并成为一个稍复杂的表达式.譬 ...
- 手写@koa/router源码
上一篇文章我们讲了Koa的基本架构,可以看到Koa的基本架构只有中间件内核,并没有其他功能,路由功能也没有.要实现路由功能我们必须引入第三方中间件,本文要讲的路由中间件是@koa/router,这个中 ...