java如何键入char,在Java中键入从char到short 的类型

本文探讨了Java编程中为何不能直接将char值作为参数传递给接受short类型的方法,尽管char和short的存储大小相同。主要原因是char是无符号的,而short是有符号的,导致没有从char到short的隐式转换。然而,由于int可以容纳char的所有值,因此存在从char到int的转换。详细解释了Java语言规范中关于不同类型转换的规定,包括扩展转换和缩小转换的概念。
摘要由CSDN通过智能技术生成

Why can we not pass a char value as a argument to any method that accepts a short parameter

因为在调用上下文中没有从char到short的隐式转换.

whereas we can pass a char value to another method whose parameter is of int type?

那是因为在调用上下文中有一个从char到int的隐式转换.

Why does type casting not take place from char to short, given that the sizes are equal? I hope short can also store as much values as short can.

虽然char和short的大小相同,但char是无符号的,而short是有符号的.这就是为什么没有从char到short的隐式转换.

从char toint的转换是扩展的原始转换(JLS 5.1.2),而从char到short的转换是缩小的原始转换(JLS 5.1.3).特别是(强调我的):

A narrowing conversion of a char to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though chars represent 16-bit unsigned integer values.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值