1. 当传递到函数的参数的数据类型表示的范围小于函数形参的参数类型遵循如下原则 :
    1. char类型比较特殊, 直接转换为int:  char ->int ->long->float->double
    2. 其他的基本数据类型都遵循这个规则: byte->short->int->long->float->double
    3. 浮点型常量: 比如0.5等没有明确指出是何种类型的常量. 直接处理成double类型, 而不是float
    4. 如果希望是某一种明确的类型, 最后显示地给出, 比如0.5F,0.5f.0.5D,1L等等.
  2.  当传递导函数的参数类型表示的范围大于函数形参的参数类型, 那么就必须显示地进行窄化转换
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. 涉及自动装箱和拆箱的重载

    1. 自动装箱:
      1. java的自动装箱和拆箱仅限于基本数据类型和包装数据类型之间!
      2. 对于char类型: char ->int ->long->float->double->Character->Object->可变参数类型, 并且它不能提升为Integer等其他包装类型, 因为无论如何进行自动类型转换,char类型始终是char类型, 装箱之后也只能是Character, Character是无法转换为Integer等其他包装类型的;
      3. 与此类似, 其他的基本数据类型也是如此
    2. 自动拆箱:
      1. 对于Character类型: Character->Object->char ->int ->long->float->double->可变参数类型(由于Character是Object类型的子类, 所以优先)
      2. 对于其他的包装类型: 都是首先寻找自动拆箱之后的数据类型, 然后是自动类型提升之后可以使用的基本数据类型
      3. 包装类型之间是无法互相转换的!

总之一句话, 对于基本数据类型, 如果能在基本数据类型之间转换, 就不使用包装类型;  包装类型能转换成自身拆箱之后的基本数据类型就转换成为基本数据类型, 如果不能就转成对应基本数据类型相邻最近的基本数据类型.(比较绕,本人能力有限í﹏ì)

临时补充: 对于包含可变参数列表, 遵循该参数列表的类型和待转换的类型而定, 允许基本数据类型和包装数据类型之间的转换和基本数据类型的自动提升

  //代码来自《thingking in java》。

  //《thinking in java》笔记, 如果有什么不对的地方, 欢迎指正^_^

java重载中的基本类型的自动类型转换的更多相关文章

  1. 我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换

    今天看到两个面试题,居然都做错了.通过这两个面试题,也加深对三目运算是的自动类型转换的理解. 题目1.以下代码输出结果是(). public class Test { public static vo ...

  2. java字节中的基本类型的职业的数目 (采访总是问)

    因为移动装置存储器中的移动开发的局限性,数据的字节数需要考虑往往在占领中使用的类型. 下面介绍下一个Java,以加深记忆. 在Java中一共同拥有8种基本数据类型,当中有4种整型,2种浮点类型,1种用 ...

  3. JAVA泛型中的有界类型(extends super)(转)

    JDK1.5中引入了泛型(Generic)机制.泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Ja ...

  4. java web中日期Date类型在页面中格式化显示的三种方式

    一般我们经常需要在将服务器端的Date类型,传到页面进行显示,这就涉及到一个如何格式化显示Date类型的问题,一般我们有三种方式进行: 1)在服务端使用SimpleDateFormat等类格式化成字符 ...

  5. 关于JAVA泛型中的通配符类型

    之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...

  6. JAVA数据类型中的char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a'; //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=11 ...

  7. java对象中含有Integer类型字段转json字符串问题

    问题:对于含有Integer类型字段的java对象,在通过下面这种方式转为json字符串时,Integer类型的字段如果为空的情况下,会默认转化为0,但是我想让它为空的时候直接转化为null,不要默认 ...

  8. java类中使用quartz,设置自动任务Demo

    package com.tech.jin.jobScheduler; import java.text.ParseException; import java.util.ArrayList; impo ...

  9. QT中|Qt::Tool类型窗口自动退出消息循环问题解决(setQuitOnLastWindowClosed必须设置为false,最后一个窗口不显示的时候,程序会退出消息循环)

    为application 设置setQuitOnLastWindowClosed属性,确实为true: 将其显示为false; 退出该应该程序不能调用QDialog的close消息槽,只能调用qApp ...

随机推荐

  1. mongdb数据库的操作

    一.数据库使用 1.使用mongodb服务,必须先开启服务,开启服务使用 mongod --dbpath D:mongdb    (D:mongdb  自己所创建数据库的路径, 在cmd窗口中输入) ...

  2. luogu P1455 搭配购买

    题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...

  3. POJ-1163 递推

    代码很容易看明白,就不详解了. 这个是空间优化的代码. #include <iostream> #include <algorithm> #define MAX 101 usi ...

  4. How far away?

    C - How far away ? HDU - 2586 There are n houses in the village and some bidirectional roads connect ...

  5. 一个关于vue+mysql+express的全栈项目(三)------ 登录注册功能的实现(已经密码安全的设计)

    本系列文章,主要是一个前端的视角来实现一些后端的功能,所以不会讲太多的前端东西,主要是分享做这个项目学到的一些东西,,,,, 好了闲话不多说,我们开始搭建后端服务,这里我们采用node的express ...

  6. 【HIHOCODER 1478】 水陆距离(BFS)

    描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...

  7. POJ 2349 Arctic Network(贪心 最小生成树)

    题意: 给定n个点, 要求修p-1条路使其连通, 但是现在有s个卫星, 每两个卫星可以免费构成连通(意思是不需要修路了), 问修的路最长距离是多少. 分析: s个卫星可以代替s-1条路, 所以只要求最 ...

  8. XV6陷入,中断和驱动程序

    陷入,中断和驱动程序 运行进程时,cpu 一直处于一个大循环中:取指,更新 PC,执行,取指…….但有些情况下用户程序需要进入内核,而不是执行下一条用户指令.这些情况包括设备信号的发出.用户程序的非法 ...

  9. Foundation框架的一些实用方法:替换字符串,去空格,反转

    //定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组 NSMutableString *str = [NSMutableString stringWithForma ...

  10. JPos学习

    基于JPos的消息交换系统 消息交换系统需求解读 消息交换系统不不是一个具体的业务系统,而是业务系统的运转的基础框架: 他的运转是体现在报文交换上的: 要定义一个可被不同业务系统使用的报文规范: 报文 ...