Java中的类型转换

类型转换 (Typecasting)

  • Typecasting is a term which is introduced in all the language similar to java.

    Typecasting是一个用与Java类似的所有语言引入的术语。

  • When we assign primitive datatype to another datatype.

    当我们将原始数据类型分配给另一个数据类型时。

  • In java Typecasting is of two types:

    在Java中,类型转换具有两种类型:

    1. Widening Typecasting
    2. Narrowing Typecasting

We will study both typecasting with examples...

我们将通过示例研究两种类型转换。

a)加宽型铸 (a) Widening Typecasting)

  • When we convert a smaller size datatype to the larger size datatype.

    当我们将较小的数据类型转换为较大的数据类型时。

  • In this typecasting no data loss is there.

    在这种类型转换中,没有数据丢失。

  • It is done by the compiler (i.e automatic). It is not done by the user.

    它是由编译器完成的(即自动的)。 它不是由用户完成的。

  • Hierarchy of widening typecasting is described below:

    扩展类型转换的层次结构描述如下:

    byte → short → char → int → long → float → double

    字节→短→字符→整数→长→浮点→双精度

Example of Widening Typecasting

加宽型铸件的例子

public class WideningTypecast {
public static void main(String[] args) {
int num1;
byte num2 = 20;
// We are assigning smaller datatype 
// byte to larger datatype 
num1 = num2;
// Print the output 
System.out.println("The value of num1 is :" + num1);
}
}

Output

输出量

D:\Programs>javac WideningTypecast.java
D:\Programs>java WideningTypecast
The value of num1 is :20

b)缩小类型转换 (b) Narrowing Typecasting)

  • When we convert a larger size datatype to the smaller size datatype.

    当我们将较大的数据类型转换为较小的数据类型时。

  • In this typecasting data loss is there.

    在这种类型转换中,存在数据丢失。

  • It is not done by the compiler (i.e manually). It is done by the user.

    它不是由编译器(即手动)完成的。 它是由用户完成的。

  • Hierarchy of narrowing typecasting is described below:

    缩小类型转换的层次结构描述如下:

    double → float → long → int → char → short → byte

    double→float→long→int→char→short→字节

Example of Narrowing Typecasting

缩小类型转换的示例

public class NarrowingTypecast {
public static void main(String[] args) {
int num1;
double num2 = 20.8;
// We are assigning larger size datatype 
// long to smaller size datatype 
num1 = (int) num2;
// Print the output 
System.out.println("The value of num1 is :" + num1);
}
}    

Output

输出量

D:\Programs>javac NarrowingTypecast.java
D:\Programs>java NarrowingTypecast
The value of num1 is :20

翻译自: https://www.includehelp.com/java/typecasting-in-java.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377100.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

让crash文件中的内存地址变成函数名称,

假如程序员编译了inhouse给测试。 如果在测试过程中出现奔溃现象,我想程序员一般会来看Device Log 也就是 crash文件 如果crash文件遇到如下的情况,在重要的地方看不到函数名称。我想是一件很奔溃的事情。 1 Exception Type: EXC_BAD_ACCESS (SIGSEGV)2…

《dp补卡——多重背包》

多重背包简介: 有N种物品和一个容量为V的背包。第i种物品最多有Mi件可用,每件耗费的空间为Ci,价值为Wi。求解将哪些物品装入背包可使得这些物品耗费的空间总和不超过背包容量,且价值总和最大。 将Mi件摊开,就是一个01背…

kafka消息确认ack_什么是确认(ACK)? ACK代表什么?

kafka消息确认ackACK:致谢 (ACK: Acknowledgment) An acknowledgment (ACK) is a signal that is passed among the communicating processes, computers, or devices to indicate acknowledgment, or delivery of the message, as a component of a communications…

CocoaAsyncSocket 套接字

CocoaAsyncSocket 套接字 https://github.com/robbiehanson/CocoaAsyncSocket Asynchronous socket networking library for Mac and iOS 用于iOS以及Mac的异步套接字网络库。 TCP GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key…

谷歌浏览器设置缓存方法

谷歌浏览器设置缓存方法: 1、在桌面Google Chrome快捷方式,目标:找到 C:\Users\Splendid\AppData\Local\…\Application\chrome.exe 在这后面加上-Disk-Cache-Dir”Z:\TEMP” 注意: -Disk前面有空格,”Z:\TEMP” 是文件存放在Z盘T…

《dp补卡——买卖股票问题》

目录121. 买卖股票的最佳时机贪心dp思路滚动数组优化122. 买卖股票的最佳时机 II123. 买卖股票的最佳时机 III188. 买卖股票的最佳时机 IV309. 最佳买卖股票时机含冷冻期714. 买卖股票的最佳时机含手续费121. 买卖股票的最佳时机 贪心 取最左最小值,取最右最大值&…

oo0ooo0ooo0oo_OoO的完整形式是什么?

oo0ooo0ooo0ooOoO:外出 (OoO: Out of Office) OoO is an abbreviation of "Out of Office". OoO是“不在办公室”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email…

SP2010开发和VS2010专家食谱--第三章节--高级工作流(2)--为沙盒解决方案创建自定义活动...

尽管沙河解决方案功能有限,你仍然可以开发自定义活动,在SharePoint Designer中使用而不用改变web.config或添加.ACTION文件到根文件夹。 转载于:https://www.cnblogs.com/crazygolf/p/3856795.html

sql where 1=1和 0=1 的作用

where 11; 这个条件始终为True,在不定数量查询条件情况下,11可以很方便的规范语句。 一、不用where 11 在多条件查询中的困扰 举个例子,如果您做查询页面,并且,可查询的选项有多个,同时,还让用户…

j@2ff4f00f_J4F的完整形式是什么?

j2ff4f00fJ4F:只是为了好玩 (J4F: Just For Fun) J4F is an abbreviation of "Just For Fun". J4F是“ Just For Fun”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceboo…

《dp补卡——子序列问题》

目录300. 最长递增子序列674. 最长连续递增序列718. 最长重复子数组1143. 最长公共子序列53. 最大子序和392. 判断子序列115. 不同的子序列583. 两个字符串的删除操作72. 编辑距离647. 回文子串 (与 5.最长回文子串思路差不多)516. 最长回文子序列300. 最…

[LeetCode] Maximal Rectangle

Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle containing all ones and return its area. 在做 Largest Rectangle in Histogram的时候有人说可以用在这题,看了一下还真是,以每行为x轴,每列往上累计的连续的…

什么是alpha测试_什么是ALPHA?

什么是alpha测试Α (ALPHA) Alpha is the first and foremost letter of the Greek alphabet. In the classification of Greek numerals or numbers, it constitutes a value of 1. Alpha是希腊字母的第一个也是最重要的字母 。 在希腊数字或希腊数字的分类中,它的…

《leetcode : 647. 回文子串 思考分析双指针解法》

647. 回文子串 如何确定是回文串: 找中心然后往两边扩散,判断是否对称即可。 在遍历中心点的时候,注意中心点可以是一个元素也可以是两个元素。 class Solution { public:int cal_two_extend(const string& s,int i,int j,int n){int re…

天草初级班(3)

算术运算指令算术运算指令是反映CPU计算能力的一组指令,也是编程时经常使用的一组指令。它包括:加、减、乘、除及其相关的辅助指令。 该组指令的操作数可以是8位、16位和32位(80386)。当存储单元是该类指令的操作数时,该操作数的寻址方式可以…

4.3.3版本之引擎bug

bug描述:   IOS设备上,当使用WWW www WWW.LoadFromCacheOrDownload(url, verNum); 下载资源时,第一次下载某个资源,www.assetBundle必定为空。 解决办法:   引擎版本降到4.3.2或者升到4.3.4或更高。 这个bug绝对是…

sml完整形式_411的完整形式是什么?

sml完整形式411:信息 (411: Information) 411 is an abbreviation of “Information". 411是“信息”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenger, a…

php 检测用户是否关闭浏览器

1、例子1 echo str_repeat(" ",3000);ignore_user_abort(true); mylog(online);while (true) {/** 1、程序正常结束 connection_status 0* 2、点击浏览器“停止”按钮 connection_status 1* 3、超时 connection_status 2*/echo "test<br>\n&qu…

explain用法

explain用法 EXPLAIN SELECT …… 变体&#xff1a; 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句&#xff0c;运行SHOW WARNINGS 可得到被MySQL优化器优化后的查询语句 2. EXPLAIN PARTITIONS SELECT …… 用于分区表的EXPLAIN 执行计划包含的信息 id…

《位运算技巧以及Leetcode的一些位运算题目》

目录技巧练习位运算[461. 汉明距离](https://leetcode-cn.com/problems/hamming-distance/)[190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/)[136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/)[260. 只出现一次的数字 III](http…