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 ...
随机推荐
- luogu P1866 编号
题目描述 太郎有N只兔子,现在为了方便识别它们,太郎要给他们编号.兔子们向太郎表达了它们对号码的喜好,每个兔子i想要一个整数,介于1和Maxnumber[i]之间(包括1和Maxnumber[i]). ...
- 手动编译openslide
1.下载openslide源代码, 2.转到openslide代码目录: ./configure 3.安装依赖库: sudo apt-get update sudo apt-get install l ...
- [JOYOI] 1096 数字组合
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...
- php file_get_contents json_decode 输出为NULL
解决办法一:不小心在返回的json字符串中返回了BOM头的不可见字符,某些编辑器默认会加上BOM头,如下处理才能正确解析json数据: $info = json_decode(trim($info,c ...
- 【转】C# windows服务的创建与调试
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- ms sql server 大批量导入
BULK INSERT 文章:BULK INSERT如何将大量数据高效地导入SQL Server 可以首先在数据库建一个表Temp_tb,这个表作为导入数据的表,然后使用bulk insert导入,导 ...
- 【同余】HDU 6108 小C的倍数问题
http://acm.hdu.edu.cn/showproblem.php?pid=6108 [题意] 给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的充分必要条件是每一位加起来的和是B的 ...
- 【ZJOI2017 Round1练习】D2T2 iqtest(排列组合)
题意: 思路: 根据欧拉定理,a^(phi(n)-1)为a mod n的逆元 ..]of longint; s,ans,x,mo,k,phi,tmp:int64; i,m,n,j:longint; f ...
- 利用PHP SOAP实现WEB SERVICE[转载]
php有两个扩展可以实现web service,一个是NuSoap,一个是php 官方的soap扩展,由于soap是官方的,所以我们这里以soap来实现web service.由于默认是没有打开soa ...
- CentOS6 Install kafka
https://www.cnblogs.com/caoguo/p/5958608.html