zoukankan      html  css  js  c++  java
  • JAVA 面试题及思考

    ====================================

    =======学而时习之========================

    =====================

    1.

    public class Test {
    
        public static void main(String[] args) {
            String str = "123";
            changeStr(str);
            System.out.print(str);
        }
        
        public static void changeStr(String str){
            str = "abc";
        }
    
    }

    关键词:java内存分配

    2.

    public class Test {
        static boolean foo(char c) {
            System.out.print(c);
            return true;
        }
    
        public static void main(String[] args) {
            int i = 0;
            for (foo('A'); foo('B') && (i < 2); foo('C')) {
                i++;
                foo('D');
            }
        }
    }

     关键词:c for

    3. 

    public class B extends A {  
     // here  
    }  
      
    class A {  
        protected int method1(int a, int b) {  
            return 0;  
        }  
    }  
    //Which two are valid in a class that extends class A? (Choose two)  
    //A. public int method1(int a, int b) { return 0; }  
    //B. private int method1(int a, int b) { return 0; }  
    //C. private int method1(int a, long b) { return 0; }  
    //D. public short method1(int a, int b) { return 0; }  
    //E. static protected int method1(int a, int b) { return 0; } 

    关键词:override , overload

    • The public type A must be defined in its own file
    • Cannot reduce the visibility of the inherited method from A
    • The return type is incompatible with A.method1(int, int)
    • This static method cannot hide the instance method from A
    • Duplicate method method1(int, int) in type A
    • 方法重载是单个类内部,通常说方法调用 a. 方法名    b. 参数
    • 方法重写是继承关系中,全部相同,除了 a. 子可见度>=父可见度    b. 子final可终止继承

    4. 

    public class Outer {  
        public void someOuterMethod() {  
            // Line 3  
        }  
      
        public class Inner {  
        }  
      
        public static void main(String[] args) {  
            Outer o = new Outer();  
            // Line 8  
        }  
    }  
      
    // Which instantiates an instance of Inner?  
    // A. new Inner(); // At line 3  
    // B. new Inner(); // At line 8  
    // C. new o.Inner(); // At line 8  
    // D. new Outer.Inner(); // At line 8  
    // E. new Outer().new Inner(); // At line 8  

    关键词:内部类

    • 构造方法也是方法
    • 构造方法前必须有new 修饰
    • 谁调用方法:实例调用实例方法new,类调用类方法static
    • 还有一种内部类叫静态内部类

    5. 

    CREATE TABLE zxg(a VARCHAR2(10),b VARCHAR2(10))  
      
    INSERT INTO zxg VALUES('a',NULL);  
    INSERT INTO zxg VALUES('b','234');  
    INSERT INTO zxg(a,b) VALUES ('c','');  
    INSERT INTO zxg(a,b) VALUES ('d',' ');  
      
    SELECT * FROM zxg  
    --1 a     
    --2 b   234  
    --3 c     
    --4 d      
    SELECT * FROM zxg WHERE b LIKE '%'  
    --1 b   234  
    --2 d      

    关键词:LIKE , NULL

    • 关于oracle中like ‘%’ 或者 like '%%' 还有 is null  ,is not null
    • 长度为零的字符串即空字符串varchar2 类型时为 null
    • 好比调一个方法的前提是调动者得存在

    6. 

    //final 是形容词最终的,final 类不可以被继承,final 字段不可以被改变,final方法不可以被重写  
    //The type A cannot subclass the final class B  
    //Cannot override the final method from B  
    //The final field A.s cannot be assigned  
    //finally 是副词 try{}catch(){}finally{}  
    //finalize() 是方法,垃圾回收机制 

    关键词:final , finally , finalize

    7.Controlling Access to Members of a Class

    Access Levels
    ModifierClassPackageSubclassWorld
    public Y Y Y Y
    protected Y Y Y N
    no modifier Y Y N N
    private Y N N N

     no modifier (package-private

    关于package的一个问题:

    package a;

    package a.b;

    有父子关系吗?

    在a中定义一个pckage-private 的类,该类能不能被a.b中的类访问?

    答案是:不能。

    需要明确package存在的意义:通过 the fully qualified name 区分不同的类。

    文件夹包含关系是存放的实现,域名倒置是避免重复的实现。

    8. 

    public class Test {
    
        public static void main(String[] args) {
            // 向上转型
            Father f = new Child();
            // f.mygirlfriendis(); 
            // The method mygirlfriendis() is undefined for the type Father
            System.out.println(f.name);
            f.iam();
            // 向下转型
            if (f instanceof Child) {
                Child c = (Child) f;
                System.out.println(c.name);
                c.iam();
                c.mygirlfriendis();
            }
        }
    
    }
    
    class Father {
    
        String name = "Father";
    
        public void iam() {
            System.out.println(this.name);
        }
    
    }
    
    class Child extends Father {
    
        protected String name = "Child";
    
        public void iam() {
            System.out.println(this.name);
        }
    
        public void mygirlfriendis() {
            System.out.print("Xuhua");
        }
    }

     执行结果:

    Father
    Child
    Child
    Child
    Xuhua

    关键词:向上转型,向下转型

    • 把子类的实例赋给父类的引用
    • 把指向子类的父类的引用强转为子类
    • 超类作为方法的参数,目的是简化接口

     9. 

    public static void main(String[] args) {
    
            String s = null;
            System.out.println(String.valueOf(s));
            
            System.out.println(String.valueOf(null));
            
        }

    关键词:方法重载

    10. 引用是怎么回事

    Circle c; c null    
    c = new Circle(); c 0x45f c:Circle
            radius:0

    Circle c; 和 Circle c = null; 的区别

    class Test {
        Person person;
    
        public void f() {
    //        Person xx;
    //        System.out.println(xx); //The local variable xx may not have been initialized
            System.out.println(person);
        }
    }
    
    class Person {
    
    }
    • = 代表初始化
    • 成员变量不用初始化,因为会自动初始化
    • 局部变量需要初始化,不然编译期间会报错
    • 引用用于存储对象的地址
    • 引用位于,对象位于
    • 栈用于程序运行,堆用于数据存储

     

  • 相关阅读:
    Linux:sudo,没有找到有效的 sudoers 资源。
    Python中关于CSV文件中的I/O
    Python数据处理进阶——pandas
    脚本的含义
    Common.Logging.dll----------配置方式,可选引用,用于日志输出
    Net作业调度
    MySQL版本介绍
    在 Windows 上安装Rabbit MQ 指南
    版本控制器
    C# 中的占位符本质
  • 原文地址:https://www.cnblogs.com/zno2/p/4485592.html
Copyright © 2011-2022 走看看