java中int转long
时间: 2023-11-18 07:03:44 浏览: 77
在Java中,将int类型转换为long类型可以使用以下方法:
1. 使用Long.parseLong()方法:先将int类型转换为字符串,然后再使用Long.parseLong()方法将字符串转换为long类型。例如:
```
int num = 18;
String str = String.valueOf(num);
long value = Long.parseLong(str);
```
2. 使用强制类型转换:将int类型的变量直接赋值给long类型的变量。例如:
```
int num = 18;
long value = (long) num;
```
需要注意的是,在进行int类型到long类型的转换时,不可以直接强制转换或获取,需要使用中间类型(int/long)进行过渡。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Java 中的 int 型转为 long 型](https://blog.csdn.net/qq_35958249/article/details/85223523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Java中int(Integer)类型与long(Long)类型数据的相互转化](https://blog.csdn.net/weixin_42346767/article/details/85067398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文