java重载中的基本类型的自动类型转换
- 当传递到函数的参数的数据类型表示的范围小于函数形参的参数类型遵循如下原则 :
- char类型比较特殊, 直接转换为int: char ->int ->long->float->double
- 其他的基本数据类型都遵循这个规则: byte->short->int->long->float->double
- 浮点型常量: 比如0.5等没有明确指出是何种类型的常量. 直接处理成double类型, 而不是float
- 如果希望是某一种明确的类型, 最后显示地给出, 比如0.5F,0.5f.0.5D,1L等等.
- 当传递导函数的参数类型表示的范围大于函数形参的参数类型, 那么就必须显示地进行窄化转换
package BaseType; public class OverloadTest
{
void f1(byte x){ System.out.println("f1(byte)") ; }
void f1(char x){ System.out.println("f1(char)") ; }
void f1(short x){ System.out.println("f1(short)") ; }
void f1(int x) { System.out.println("f1(int)") ; }
void f1(float x){ System.out.println("f1(float)") ; }
void f1(double x){ System.out.println("f1(double)") ; }
void f1(long x){ System.out.println("f1(long)") ; } void f2(byte x){ System.out.println("f2(byte)") ; }
void f2(short x){ System.out.println("f2(short)") ; }
void f2(int x) { System.out.println("f2(int)") ; }
void f2(float x){ System.out.println("f2(float)") ; }
void f2(double x){ System.out.println("f2(double)") ; }
void f2(long x){ System.out.println("f2(long)") ; } void f3(short x){ System.out.println("f3(short)") ; }
void f3(int x) { System.out.println("f3(int)") ; }
void f3(float x){ System.out.println("f3(float)") ; }
void f3(double x){ System.out.println("f3(double)") ; }
void f3(long x){ System.out.println("f3(long)") ; } void f4(int x) { System.out.println("f4(int)") ; }
void f4(float x){ System.out.println("f4(float)") ; }
void f4(double x){ System.out.println("f4(double)") ; }
void f4(long x){ System.out.println("f4(long)") ; } void f5(float x){ System.out.println("f5(float)") ; }
void f5(double x){ System.out.println("f5(double)") ; }
void f5(long x){ System.out.println("f5(long)") ; } void f6(float x){ System.out.println("f6(float)") ; }
void f6(double x){ System.out.println("f6(double)") ; } void f7(double x){ System.out.println("f6(double)") ; } void f8(char x){ System.out.println("f8(char)") ; }
void f8(byte x){ System.out.println("f8(byte)") ; }
void f8(int x) { System.out.println("f8(int)") ; }
void f8(float x){ System.out.println("f8(float)") ; }
void f8(double x){ System.out.println("f8(double)") ; }
void f8(long x){ System.out.println("f8(long)") ; } void f9(float x){System.out.println("f9 float");} void testConstVal()
{
System.out.println("5: ") ;
f1(5) ; f2(5) ; f3(5) ; f4(5) ; f5(5) ; f6(5) ; f7(5) ;
System.out.println() ;
} void testChar()
{
char x = 'x' ;
System.out.println("char x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testByte()
{
byte x = 0 ;
System.out.println("byte x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testShort()
{
short x = 0 ;
System.out.println("short x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testInt()
{
int x = 0 ;
System.out.println("int x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testLong()
{
long x = 0 ;
System.out.println("long x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testFloat()
{
float x = 0 ;
System.out.println("float x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} void testDouble()
{
double x = 0 ;
System.out.println("double x: ") ;
f1(x) ; f2(x) ; f3(x) ; f4(x) ; f5(x) ; f6(x) ; f7(x) ;
System.out.println() ;
} public static void main(String args[])
{
OverloadTest t = new OverloadTest() ;
t.testConstVal();
t.testByte();
t.testChar();
t.testShort();
t.testInt();
t.testLong();
t.testFloat();
t.testDouble(); double d = 0.9d ;
// t.f9(d) ; 这么写是错误的
t.f9((float)d) ; //必须显示地进行窄化转换
} }
3. 涉及自动装箱和拆箱的重载
- 自动装箱:
- java的自动装箱和拆箱仅限于基本数据类型和包装数据类型之间!
- 对于char类型: char ->int ->long->float->double->Character->Object->可变参数类型, 并且它不能提升为Integer等其他包装类型, 因为无论如何进行自动类型转换,char类型始终是char类型, 装箱之后也只能是Character, Character是无法转换为Integer等其他包装类型的;
- 与此类似, 其他的基本数据类型也是如此
- 自动拆箱:
- 对于Character类型: Character->Object->char ->int ->long->float->double->可变参数类型(由于Character是Object类型的子类, 所以优先)
- 对于其他的包装类型: 都是首先寻找自动拆箱之后的数据类型, 然后是自动类型提升之后可以使用的基本数据类型
- 包装类型之间是无法互相转换的!
- 自动装箱:
总之一句话, 对于基本数据类型, 如果能在基本数据类型之间转换, 就不使用包装类型; 包装类型能转换成自身拆箱之后的基本数据类型就转换成为基本数据类型, 如果不能就转成对应基本数据类型相邻最近的基本数据类型.(比较绕,本人能力有限í﹏ì)
临时补充: 对于包含可变参数列表, 遵循该参数列表的类型和待转换的类型而定, 允许基本数据类型和包装数据类型之间的转换和基本数据类型的自动提升
//代码来自《thingking in java》。
//《thinking in java》笔记, 如果有什么不对的地方, 欢迎指正^_^
java重载中的基本类型的自动类型转换的更多相关文章
- 我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换
今天看到两个面试题,居然都做错了.通过这两个面试题,也加深对三目运算是的自动类型转换的理解. 题目1.以下代码输出结果是(). public class Test { public static vo ...
- java字节中的基本类型的职业的数目 (采访总是问)
因为移动装置存储器中的移动开发的局限性,数据的字节数需要考虑往往在占领中使用的类型. 下面介绍下一个Java,以加深记忆. 在Java中一共同拥有8种基本数据类型,当中有4种整型,2种浮点类型,1种用 ...
- JAVA泛型中的有界类型(extends super)(转)
JDK1.5中引入了泛型(Generic)机制.泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Ja ...
- java web中日期Date类型在页面中格式化显示的三种方式
一般我们经常需要在将服务器端的Date类型,传到页面进行显示,这就涉及到一个如何格式化显示Date类型的问题,一般我们有三种方式进行: 1)在服务端使用SimpleDateFormat等类格式化成字符 ...
- 关于JAVA泛型中的通配符类型
之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...
- JAVA数据类型中的char类型
1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a'; //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=11 ...
- java对象中含有Integer类型字段转json字符串问题
问题:对于含有Integer类型字段的java对象,在通过下面这种方式转为json字符串时,Integer类型的字段如果为空的情况下,会默认转化为0,但是我想让它为空的时候直接转化为null,不要默认 ...
- java类中使用quartz,设置自动任务Demo
package com.tech.jin.jobScheduler; import java.text.ParseException; import java.util.ArrayList; impo ...
- QT中|Qt::Tool类型窗口自动退出消息循环问题解决(setQuitOnLastWindowClosed必须设置为false,最后一个窗口不显示的时候,程序会退出消息循环)
为application 设置setQuitOnLastWindowClosed属性,确实为true: 将其显示为false; 退出该应该程序不能调用QDialog的close消息槽,只能调用qApp ...
随机推荐
- LInux设备驱动分析—— kmalloc和kzalloc函数
今晚在研究EVM5728开发板上面Linux系统的IIC设备驱动程序,偶然之间看到驱动程序中有一处使用了kzalloc函数,本人之前都是使用Linux内核提供的kmalloc / kfree函数来给设 ...
- C#对象初始化的探讨
最近在弄MQ的性能监测数据埋点,无疑中用到一个Nstatsd的客户端,看到里面写过里面一种嵌套类的写法.代码如下: 客户端Client是一个密封的类,并且构造函数私有访问.然后又用一个嵌套类Curre ...
- 第二章:systemverilog声明的位置
1.package 定义及从package中导入定义(***) verilog中,对于变量.线网.task.function的声明必须在module和endmodule之间.如果task被多个modu ...
- 昨天去面试,这5个Python面试题都被考到了,Python面试题No6
第1题:字符串的拼接–如何高效的拼接两个字符串? 字符串拼接的几种方法 加号 逗号 直接连接 格式化 join 多行字符串拼接() 加号 print('Python' + 'Plus') 逗号 pri ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- STM32F407 独立看门狗 个人笔记
什么是看门狗 如果程序跑飞了怎么办? 可以用看门狗来监控. 看门狗是: 一个递减的计数器,如果不按时给计数器赋值,计数器的值减到一定程度,就会使系统复位. 也就是说如果程序运行异常,无法正常给计数器赋 ...
- 大数据学习——linux常用命令(四)
四 查到命令 1 查找可执行的命令所在的路径 which ls 查ls命令所在的路径 2 查找可执行的命令和帮助的位置 whereiis ls 3 从某个文件夹开始查找文件 find / -name ...
- COJ 1208 矩阵快速幂DP
题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...
- Cloud BOS平台-自定义用户联系对象
适用业务场景:新增用户时,联系对象类型默认为:职员.客户.供应商.客户需要增加一类"承运商",类型选择"承运商"时,联系对象只显示相应的承运商."承运 ...
- j_spring_security_check 404错误
折腾了好久,还是写一篇备忘 折腾了好久,还是写一篇备忘 首先检查路径 <form class="form-signin" method="POST" ac ...