A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 丁青松 黑马帝   /  2012-1-14 10:54  /  1984 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 丁青松 于 2012-1-14 20:11 编辑

昨晚想到了,发了下没说清楚。今天重新发一下:如下图代码
当有char x时调用的是char x,当没有char x时会调用int x,这涉及到类型提升,请分析下原因。。。。。

评分

参与人数 1技术分 +1 收起 理由
王德云 + 1

查看全部评分

3 个回复

正序浏览
兄弟我是啥高手,~我是零基础,刚学也没几天~我进度比较慢~苦熬呢在~
回复 使用道具 举报
荣凯旋 发表于 2012-1-14 12:54
你的问题我在CSDN上看啦下
当传入常数时,会被当作int处理,例如:

额,终于等到了撒。这个还有窄转换问题。。。。哈哈。。。厉害,高手啊。。。。
回复 使用道具 举报
你的问题我在CSDN上看啦下
当传入常数时,会被当作int处理,例如:

[java] view plaincopyprint?public class Test {  
  
    void f(char i){System.out.println("arg is char!");};  
    void f(byte i){System.out.println("arg is byte!");};  
    void f(short i){System.out.println("arg is short!");};  
    void f(int i){System.out.println("arg is int!");};  
    void f(long i){System.out.println("arg is long!");};      
    void f(float i){System.out.println("arg is float!");};  
    void f(double i){System.out.println("arg is double!");};  
      
    public static void main(String[] args) {  
        // TODO Auto-generated method stub   
        Test t = new Test();  
        t.f(5);  
    }  
  
}  
public class Test {

        void f(char i){System.out.println("arg is char!");};
        void f(byte i){System.out.println("arg is byte!");};
        void f(short i){System.out.println("arg is short!");};
        void f(int i){System.out.println("arg is int!");};
        void f(long i){System.out.println("arg is long!");};       
        void f(float i){System.out.println("arg is float!");};
        void f(double i){System.out.println("arg is double!");};
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
        Test t = new Test();
                t.f(5);
        }

}
  

结果会是 arg is int!

那如果没有int形式的参数呢?重载类型会自动提高,例如:

[java] view plaincopyprint?public class Test {  
  
    void f(char i){System.out.println("arg is char!");};  
    void f(byte i){System.out.println("arg is byte!");};  
    void f(short i){System.out.println("arg is short!");};  
    //void f(int i){System.out.println("arg is int!");};   
    void f(long i){System.out.println("arg is long!");};      
    void f(float i){System.out.println("arg is float!");};  
    void f(double i){System.out.println("arg is double!");};  
      
    public static void main(String[] args) {  
        // TODO Auto-generated method stub   
        Test t = new Test();  
        t.f(5);  
    }  
  
}  
public class Test {

        void f(char i){System.out.println("arg is char!");};
        void f(byte i){System.out.println("arg is byte!");};
        void f(short i){System.out.println("arg is short!");};
        //void f(int i){System.out.println("arg is int!");};
        void f(long i){System.out.println("arg is long!");};       
        void f(float i){System.out.println("arg is float!");};
        void f(double i){System.out.println("arg is double!");};
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
        Test t = new Test();
                t.f(5);
        }

}

结果会是 arg is long!

其他情况类似,会一直提升类型直到能匹配到为止,例如从byte提高到了double:

[c-sharp] view plaincopyprint?public class Test {  
  
    void f(char i){System.out.println("arg is char!");};  
    //void f(byte i){System.out.println("arg is byte!");};   
    //void f(short i){System.out.println("arg is short!");};   
    //void f(int i){System.out.println("arg is int!");};   
    //void f(long i){System.out.println("arg is long!");};     
    //void f(float i){System.out.println("arg is float!");};   
    void f(double i){System.out.println("arg is double!");};  
      
    public static void main(String[] args) {  
        // TODO Auto-generated method stub   
        byte x = 0;  
        Test t = new Test();  
        t.f(x);  
    }  
  
}  
public class Test {

        void f(char i){System.out.println("arg is char!");};
        //void f(byte i){System.out.println("arg is byte!");};
        //void f(short i){System.out.println("arg is short!");};
        //void f(int i){System.out.println("arg is int!");};
        //void f(long i){System.out.println("arg is long!");};       
        //void f(float i){System.out.println("arg is float!");};
        void f(double i){System.out.println("arg is double!");};
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
        byte x = 0;
                Test t = new Test();
                t.f(x);
        }

}

结果会是 arg is double!

char比较特殊,如果匹配不到会提升至int,如果还匹配不到则继续提升。

那如果传入参数的类型大于形参呢?答案是编译器会告诉你此路不通  
我给你看个自动提升的表,你思考下吧~呵呵,加油兄弟

QQ截图20120114125205.png (47.58 KB, 下载次数: 49)

QQ截图20120114125205.png

评分

参与人数 1技术分 +1 收起 理由
王德云 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马