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.pswp.cn/news/377100.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377100.shtml
英文地址,請注明出處:http://en.pswp.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…