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. 洛谷 P1708 天然气井 题解

    https://www.luogu.org/problemnew/show/P1708 这道题还是比较好的. 读完题目我们先想想如何计算某个天然气井($x_1,y_1$)和中转站($a_1,b_1$) ...

  2. MySQL存储过程实践

    对employees数据库建立存储过程 创建不含有输入输出变量的存储过程 DELIMITER // -- 设定语句结束分隔符 DROP PROCEDURE IF EXISTS GetEmployees ...

  3. 用python的Requests库模拟http请求

    一.先了解几个重要的http请求头或响应头信息 Request Headers: Host: 描述请求将被发送的目的地,包括,且仅仅包括域名和端口号. Origin: 说明请求从哪里发起的,包括,且仅 ...

  4. Jenkins中部署Sonar代码检查

    1 安装并启动sonarqube docker pull sonarqube:7.5-community docker run \ --name sonarqube \ --network ci \ ...

  5. transformer模型解读

    最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...

  6. 学习vue之windows下安装live-server 超级详细篇

    最近项目要求用vue2.0所以开始着手学习. 前期准备: 下载Node.js 地址:http://nodejs.cn/download/ 选择自己对应的版本,我下载的是.msi 64位的 然后就双击下 ...

  7. C语言学习11

    直接插入排序 //直接插入排序 #include <stdio.h> void main() { ], i; int insort(int a[], int n); printf(&quo ...

  8. SQL 一次插入多条记录

    本文介绍如何快速插入多条数据到数据表中,以满足sql语句学习或项目测试的需要. 本文非原创,是对移步原文的重新整理. 如有以下表格,如图: 1,原始添加记录的方式,sql语句如下: insert in ...

  9. ES6(字符串)

    ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...

  10. 【Ural1297】Palindrome(后缀数组)

    题意:求一个字符串的最长回文子串 n<=1000 思路:这是一道论文题 需要注意的细节: 1.奇偶分类 2.中间的分割符与最后的附加字母都是最小值,但两者不能相同,否则height可能会出现问题 ...