当前位置:网站首页>标识符和类型转换
标识符和类型转换
2022-04-22 06:21:00 【爱喝咖啡的佑佑】
标识符
定义:是给类,方法,变量等起名字的符号。
规则:1.是由数字,字母,美元符和下划线(—)组成。
2.开头不能是数字
3.不能为关键字
4.注意区分大小写
常见的命名规则:
小驼峰命名:主要针对方法和变量
1.当标识符为一个单词的时候,首字母大写。
2.当标识符由多个单词组成的时候,第一个单词首字母小写,其它单词的首字母大写。
大驼峰命名:主要针对类
1.标识符是一个单词的时候,首字母大写。
2.标识符由多个单词组成的时候,每个单词的首字母大写。
见名知意
类型转换
分类:自动转换,强制转换
数据类型有小到大
char(16)
byte(8)-----short(16)-----int(32)-----long(64)-----float(32)-----double(64)
其中char直接转换为int类型,不可以转换为byte和short类型。
别忘了int类型可以直接转换为double类型。
自动转换类型:把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量。
自动转换类型就是低级的可以直接转换到高级的
public class change{
public static void main(String[]args){
int a = 10:;
System.out.println(a);
double b =10;
System.out.println(b);
//其中a输出的数字会是10,但是b输出的却是10.0
//这就是int类型自动转换成double
}
}
其实你还可以去实验一下byte转short这种自动转换。
记住byte和short不能转换为char类型的哟。
强制转换:把一个表示数据范围大的数据或者变量赋值给另一个表示数据范围小的变量。
格式:目标数据类型 变量名 = (目标数据类型)数值或者变量
Public class change{
public static void main (String []args){
//定义一个double类型的数强转为int类型
//int c = 66.66;
// System.out.println(c);
//如果这样写的化会展示不兼容所以这样是错误的应强转
int a = (int)66.66;
System.out.println(a);
}
}
上面的那个代码错误已经屏蔽掉啦,下面的强转是可以运行的。
但强制转换有一点不好就是会有数据丢失。就像上面的66.66转换为int类型后就只剩66了。
版权声明
本文为[爱喝咖啡的佑佑]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_64131008/article/details/121920383
边栏推荐
- Redis的设计与实现(3):持久化策略RDB、AOF
- 1. Jam packed (Game 5 of 2021 training League warm-up training competition)
- Strong net cup 2019 random note
- Dom 文档对象模型
- APC (IV)
- Detailed explanation of linked list
- Task section & task gate
- F. Find 3-friendly integers (2021 Niuke summer multi school training camp 1)
- Kotlin learning 1. Scope functions let, with, run, apply, also
- Leetcode - 1 - (substructure, combination, spiral matrix and full arrangement of tree < Ⅰ Ⅲ >)
猜你喜欢

X64 Foundation (I)

On time complexity and space complexity

系统日志文件过大优化

Abstract classes and abstract methods

B. Ball dropping (simple geometric calculation / similar triangle) (2021 Niuke summer multi school training camp 1)

并发编程的艺术(3):深入理解Synchronized的原理

SQL注入(一)

Drive communication with R3

A. Binary seating (the fifth game of 2021 training League warm-up training competition)

Pointer structure const summary
随机推荐
Links summary qwq
Kotlin协程+Flow+Retrofit实现网络请求
APC(二)
信息安全数学基础
Tree + detailed explanation of binary tree + Top-k problem
set方法和get方法
Kotlin 协程Flow、StateFlow、ShareFlow
[method overload]
The way to learn the strongest operator (detailed explanation of C language)
Kotlin Flow实现线程切换
区别区别区别
Redis的设计与实现(3):持久化策略RDB、AOF
System log collection series
Mathematical basis of information security
On time complexity and space complexity
Leetcode - 2 - (parenthesis generation, longest palindrome string, ring linked list, reverse linked list, nodes in pairwise exchange linked list)
1232. Minimum number of arrows for exploding balloons
Hide the drive by driving the broken chain
B. Cutting corners (simple geometry / sign in) (Game 5 of 2021 Training Alliance warm-up training competition)
1242 · non overlapping interval