1.s=s+4;

public class Test  {
public static void main(String[] args){
short s=3;
// s=s+4; //描述 资源 路径 位置 类型类型不匹配:不能从 int 转换为 short Test.java /HelloWorld/src/example 第 6 行 Java 问题 System.out.println(s);
} }

在执行s=s+4;语句时,编译器在编译的时候,默认并没有强制类型转换。所以,s是short类
型,4是int类型,s会自动提升为int类型,相加的和也是int类型,赋值给short类型的变量肯定会损失精
度。这时候就需要进行强制类型转换:s=(short)(s+4);

2,s+=4;

public class Test  {
public static void main(String[] args){
short s=3;
s+=4;
System.out.println(s);
} }

输出:7

执行s+=4;语句时,编译器在编译的时候,默认进行了强制类型转换,也就是将int类型的数
据转换成short类型的数据

随机推荐

  1. 网页加速之Chromium 预载入 Prerendering

    前一篇博文已经介绍通过prefetch预先载入网页的资源来提升网页载入速度,以下我们一起来看一下网页加速之chromium prerendering.在介绍prerendering之前,先介绍两个概念 ...

  2. LINUX 系统初始化脚本

    #!/bin/bash ######the system first start configuretion #####for install  ####copy right by donglei## ...

  3. 大数据时代的新BI系统架构发展趋势

    商业智能(BI,Business Intelligence).它是一套完整的解决方式,用来将企业中现有的数据进行有效的整合,高速准确的提供报表并提出决策根据.帮助企业做出明智的业务经营决策.     ...

  4. SharePoint创建Alternate Access Mapping (AAM)备用訪问映射

    SharePoint创建Alternate Access Mapping (AAM)备用訪问映射         SharePoint的仓库是SQL Server中的内容数据库.这些数据库储存着组织全 ...

  5. Redis封装之Hash

    RedisHashService: /// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直 ...

  6. eclipse启动Tomcat加载项目时报内存溢出错误解决办法

    在eclipse中点击Window->Preferences打开全局属性设置对话框,如下图所示设置Tomcat运行时的JVM参数,添加这段JVM设置:-Xms256M -Xmx768M -XX: ...

  7. 963B:Destruction of a Tree

    You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any ve ...

  8. UVA And Then There Was One

    约瑟夫环问题,只不过每次删除一个后,在移m的倍数. #include <iostream> #include <cstdio> #include <cstring> ...

  9. selenium无法定位元素问题

    在用自动化测试工具selenium完成下载任务时,经常会遇到定位不到元素的情况,总结如下: 1.frame/iframe原因定位不到元素: 这个是最常见的原因,首先要理解下frame的实质,frame ...

  10. spring的事务如何配置

    spring的声明式事务配置: 1. <!-- 配置sessionFactory --> <bean id="sessionFactory" class=&quo ...