• Java面试集合(三)-30道面试题


    前言

    大家好,我是 Vic,今天给大家带来Java面试集合(三)的概述,希望你们喜欢

    1.在Java中是否可以含有多个类?
    答:可以含有多个类,但只有一个是public类,public类的类名与文件名必须一致。

    2.说说&和&&的区别?
    答:&&短路与,当第一个表达式为false时,第二个表达式不会进行。&,当一个表达式为false时,第二个表达式会进行。

    3.char变量类型,能否存储一个中文汉字?
    答:可以储存一个汉字,因为char是用Unicode编码来存储的,所以可以存储。

    4.final关键字修饰变量时,是引用不变,还是引用对象不变?
    答:使用final关键字修饰变量时,是引用变量不能变,引用变量所指对象中的内容是可以改变的。

    5.静态变量和实例变量的区别?

    答:静态变量前要加static修饰,而实例变量不用。在静态变量中不需要实例对象来调用输出,而实例变量则需要进行实例化,才能使用。

    public class Test{
       public int i = 1;
       public static int j = 0;
       public static void main(String[] args) {
           Test in = new Test();
           System.out.println(in.i);
           System.out.println(j);
       }   
    }

    6.如何理解Math类中的ceil,floor,round?
    答:ceil为天花板,则向上取整为取大的值,补充到整数;floor为地板,则向下取整为取最近小的整数;round为周围,四舍五入,原则在原来基础上+0.5,超过0.5的进位,不超过0.5就取进小整数。

    7.Overload和Override的区别?
    答:Overload为重载,Override为覆盖,重写。

    8.请说说分层设计的好处?
    答:

    • 实现了软件之间的解耦

    • 便于进行分工

    • 便于对软件组件的重用

    • 便于进行维护

    • 便于对功能的扩展

    9.Java中实现多态的机制是?
    答:父类或接口定义的引用变量指向子类具体实现类的实例对象,引用变量指向具体的实例对象。

    10.说说abstract,interface?
    答:

    • abstract修饰class为抽象类,抽象类不能创建实例对象,抽象类中的方法不必要抽象abstract修饰,但是含有abstract修饰的方法的类则必须是抽象类。
      abstract class内可以没有抽象方法,不可以被实例化,但是可以被声明。

    • interface接口中的所有方法必须是抽象的,接口中方法默认为public abstract类型,接口中变量类型默认public static final类型。
      接口中的成员变量必须定义初始化,实现接口类必须在该类实现所有的方法。

    在抽象类中有构造方法,接口中没有;抽象类中有普通成员变量,接口中没有;抽象类中可以有静态方法,接口中不能有静态方法。

    11.什么是内部类?
    答:内部类是在一个类的内部定义的类,静态内部类可以有静态成员变量,而非静态内部类不能有静态成员;内部类可以在外部类中的方法中定义,也可以在外部类的方法外定义。

    静态内部类中对外部类进行引用,只有非静态外部类才能对外部类进行引用。

    在静态内部类中不需要进行外部类的实例,就可以进行实例化,而非静态内部类需要在外面创建内部类的实例对象,创建时,一定要先创建外部类的实例对象,然后用外部类的实例对象去创建内部类的实例对象。

    12.String是否可以被继承?
    答:不可以被继承,因为java.lang.String类是final类型的,不能继承的类,被final关键字修饰的类,并且不能被修改,不能改变!

    当一个final类型中,String s = "Vic";s = s + " love ";表示原有的对象并没有被改变而是该引用转向了新的对象,原有的s引用不在指向原有的对象了。

    13.1到99累加的String和StringBuffer效率?

    StringBuffer sb = new StringBuffer();
    for(int i = 0; i<100; i++){
    sb.append(i);
    }
    String str = new String();
    for(int i = 0; i<100; i++){
    str = str + i;
    }

    StringBuffer只创建一个对象,而String创建了多个对象。

    14.说说final,finally,finalize?
    答:final用于修饰属性,方法,类,被修饰的属性是不可以变的,被修饰的方法是不可被覆盖的,被修饰的类是不可以被继承的。

    finally这个是在异常处理语句中的一部分,finally中的语句是总要执行的。

    finalize是垃圾回收集机制的,记住这点就够了。

    15.在Java中有几种方法来实现线程?
    答:

    //new Thread(){}.start();
    new Thread(){
    public void run(){
    }
    }.start();
    
    new Thread(new Runnable(){
    public void run(){
    }
    }).start();

    16.说说迭代器模式?

    答:

    public static void print(String str){
    Iterator it = str.iterator();
    while(it.hasNext()){
     String str = it.next();
     System.out.println(str);
    }
    }
    17.说说装饰者模式?

    答:

    public interface Person{
    void eat();
    }
    public class Student implements Person{
    public void eat(){
     System.out.println("eating");
    }
    }
    public class Me implements Person{
    private Student student;
    Me(Student student){
     this.student=student;
    }
    public void eat(){
    System.out.println("study");
    student.eat();
    System.out.println("sleeping");
    }
    }
    public class PersonDemo{
    public static void main(String[] args){
     Student student = new Student();
     Me me = new Me(student);
     me.eat();
    }
    }

    18.说说单例模式?

    //私有构造方法;私有静态引用,返回值为静态的公有方法
    public class Singleton{
    private static Singleton singleton = new Singleton();
    private Singleton(){
    }
    public static Singleton getInstance(){
     return singleton;
    }
    }

    19.说说工厂模式?

    public interface School{
    void Study();
    }
    public class Studentone implements School{
    public void Study(){
     System.out.println("studyone");
    }
    }
    public class Studenttwo implements School{
    public void Study(){
     System.out.println("studytwo");
    }
    }
    public interface AllSchool{
    School getSchool();
    }
    
    public class Studentone implements AllSchool{
    public School getSchool(){
     return new Study();
    }
    }
    public class Studenttwo implements AllSchool{
    public School getSchool(){
     return new Study();
    }
    }
    
    public class SchoolDemo{
    public void test(){
     AllSchool allSchool = null
     //one
     allSchool = new Studentone();
     Studentone one = allSchool.getSchool();
     one.Study();
     //two
     allSchool = new Studenttwo();
     Studenttwo two = allSchool.getSchool();
     two.Study();
    }
    }

    20.说说原型模式?
    答:实现Cloneable接口,重写Object类中的clone方法

    21.说说生成器模式?
    答:

    class Ym{
    public void ymRequest(){
     System.out.println("getYm");
    }
    }
    interface Target{
    public void request();
    }
    class Ym extends Ym implements Target{
    public void request(){
     super.ymRequest();
    }
    }
    public class Test{
    public static void main(String[] args){
     Target adapter = new Adapter();
     adapter.request();
    }
    }

    22.说说代理模式?

    //静态
    public interface Info{
    public void infoOne():
    public void infoTwo();
    }
    
    public class TrueInfo implements Info{
    public void infoOne(){
    }
    public void infoTwo(){
    }
    }
    
    public class FlaseInfo implements Info{
    private Info trueInfo;
    public FlaseInfo(Info trueInfo){
     this.trueInfo = trueInfo;
    }
    public void infoOne(){
    }
    public void infoTwo(){
    }
    }
    
    public class Test{
    public static void main(String[] args){
     Info trueInfo = new TrueInfo();
     Info flaseInfo = new FlaseInfo(trueInfo);
     flaseInfo.infoOne();
     flaseInfo.infoTwo();
    }
    }

    23.说说外观模式?

    public class Studentone{
    public void One(){
     System.out.println("Studentone one");
    }
    public void Two(){
     System.out.println("Studentone two");
    }
    }
    
    public class Studenttwo{
    public void One(){
     System.out.println("Studenttwo one");
    }
    public void Two(){
     System.out.println("Studenttwo two");
    }
    }
    
    public class School{
    public void Study(){
     Studnetone one = new Studentone();
     one.One();
     one.Two();
     Studenttwo two = new Studnettwo();
     two.Ono();
     two.Two();
    }
    }
    
    public class Test{
    public static void main(String[] args){
     School school = new School();
     school.Study();
    }
    }

    24.说说冒泡排序?

    public class Demo{
    public static void main(String[] args){
    int[] nums = { 3,1,7,5,8,9,23,45};
    for(int i = 0; i< nums.length-1;i++){
     for(int j = 0;j<nums.length-1-i;j++){
      if(nums[j]>nums[j+1]){
      int temp = nums[j];
      nums[j] = nums [j+1];
      nums[j+1] = temp;
    }
    }
    for(int j = 0; j<nums.length;j++){
    Systm.out.println(nums[j]);
    }
    }

    25.说说选择排序?

    //这种就是排序算法,比如有6个人,第一轮要进行5次比较
    //第一轮
    for(int index=1;index<arr.length;index++)
    {
    if(arr[0]>arr[index])
    {
     int temp = arr[0];
     arr[0] = arr[index];
     arr[index] = temp;
    }
    }
    print(arr);
    
    //第二轮
    for(int index=2;index<arr.length;index++)
    {
    if(arr[1]>arr[index])
    {
     int temp = arr[1];
     arr[1] = arr[index];
     arr[index] = temp;
    }
    }
    print(arr);
    
    //第三轮
    for(int index=3;index<arr.length;index++)
    {
    if(arr[2]>arr[index])
    {
     int temp = arr[2];
     arr[2] = arr[index];
     arr[index] = temp;
    }
    }
    print(arr);
    
    //第四轮
    for(int index=4;index<arr.length;index++)
    {
    if(arr[3]>arr[index])
    {
     int temp = arr[3];
     arr[3] = arr[index];
     arr[index] = temp;
    }
    }
    print(arr);
    
    //第五轮
    for(int index=5;index<arr.length;index++)
    {
    if(arr[4]>arr[index])
    {
     int temp = arr[4];
     arr[3] = arr[index];
     arr[index] = temp;
    }
    }
    print(arr);
    
    //第六轮没有,我们arr.length=6举例
    //int index = 6;index<arr.length; false
    
    public static void selectionSort(int[] arr)
    {
    for(int count=1;count<arr.length;count++)
    {
    for(int index=count;index<arr.length;index++)
    {
     if(arr[count-1]>arr[index])
     {
       int temp = arr[count-1];
       arr[count-1] = arr[index];
       arr[index] = temp;
     }
    }
    }

    26.说说substring()?
    答:substring(int beginIndex, int endIndex),返回字符串从beginIndex到endIndex-1的内容。

    27.static关键字的用途?
    答:“ static方法就是没有this的方法。在static方法内部不能调用非静态方法,反过来是可以的。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。” ---《Java编程思想》

    static代码块,只会在类加载的时候执行一次。static变量不需要创建对象就可以引用。

    静态成员变量可以通过对象访问,只要访问权限足够就可以。

    静态代码块,随着类的加载而执行,只执行一次。

    class StaticDemo{
    static{
    System.out.println("静态代码块");
    }
    void show(){
    System.out.println("方法");
    }
    }
    
    public class Test{
    public static void main(String[] args){
     new StaticDemo().show();
     new StaticDemo().show();
     }
    }
    //result
    静态代码块
    方法
    方法
    public class Test{
    static{
    System.out.println("静态");
    }
    public static void main(String[] args){
    System.out.println("静态main");
    }
    }
    //result 静态代码块优先于main函数执行
    静态
    静态main
    class StaticDemo{
    static{
    System.out.println("parent静态代码块");
    }
    {
    System.out.println("parent非静态代码块");
    }
    StaticDemo(){
    System.out.println("parent构造方法");
    }
    
    public class Test extends StaticDemo{
     static{
     System.out.println("child静态");
    }
    {
    System.out.println("child非静态");
    }
    Test(){
    System.out.println("child构造方法");
    }
    public static void main(String[] args){
    System.out.println("main");
    new Test();
    }
    }
    
    //result
    parent静态代码块
    child静态
    main
    parent非静态代码块
    parent构造方法
    child非静态
    child构造方法

    28.说说IO?

    //第一种:输入流输出流
    //第二种:字节流字符流
    //第三种:节点流处理流
    //FileInputStream
    class Test{
    public static void main(String args[]){
     FileInputStream fis = null;
     try{
     fis = new FileInputStream("e:/read.txt");
     byte[] buffer = new byte[100];
     fis.read(buffer,0,buffer.length);
     for(int i = 0;i<buffer.length;i++){
      System.out.println(buffer[i]);
     }
    }
    catch(Exception e){
    System.out.println(e);
     }
    }
    }
    class Test{
    public static void main(String args[]){
     FileInputStream fis = null;
     FileOutputStream fos = null;
    try{
     fis = new FileInputStream("e:/read.txt");
     fos = new FileOutputStream("e:/write.txt");
     byte[] buffer = new byte[100];
     int temp = fis.read(buffer,0,buffer.length);
     fos.write(buffer,0,temp);
     }
      catch(Exception e){
       System.out.println(e);
      }
     }
    }
    class Test{
    public static void main(String args[]){
     FileInputStream fis = null;
     FileOutputStream fos = null;
     try{
      fis = new FileInputStream("e:/read.txt");
      fos = new FileOutputStream("e:/write.txt");
     byte[] buffer = new byte[1024];
     while(true){
      int temp = fis.read(buffer,o,buffer.length);
      if(temp = -1){
       break;
      }
      fos.write(buffer,0,temp);
     }
     }catch(Exception e){
      System.out.println(e);
    }finally{
     try{
     fis.close();
     fos.close();
    }catch(Excepiton e){
    System.out.println(e);
     }
    }
    }
    }
    //字符流
    public class TextChar
    public static void main(String args[]){
     FileReader fr = null;
     FileWriter fw = null;
     try{
     fr = new FileReader("e:/read.txt");
     fw = new FileWriter("e:/write.txt");
      
      char[] buffer = new char[100];
      int temp = fr.read(buffer,0,buffer.length);
      fw.write(buffer,0,temp); 
      }
      catch(Exception e){
       System.out.println(e);
      }finally{
        try{
          fr.close();
          fw.close();
          }
      catch(Excepiton e){
       System.out.println(e); 
      }
     }
    }
    //FileReader和BufferedReader
    class Test{
    public static void main(String args[]){
     FileReader fileReader = null;
     BufferedReader bufferedReader = null;
    try{
     fileReader = new FileReader("e:/read.txt");
     bufferedReader = new BufferedReader(fileReader);
     String line = null;
      while(true){
      line = bufferedReader.readLine();
      if(line == null){ 
        break;
      }
      System.out.println(line);
     }
     }catch(Exception e){
     System.out.println(e); 
    }
     finally{
      try{
        bufferedReader.close(); 
        fileReader.close();
       }
       catch(Exception e){
        System.out.println(e);
       }
      }
     }
    }
    public class Test{
    public static void main(String[] args) throws Exception{
     //字节流
    FileInputStream in = new FileInputStream("c:/read.txt");
    FileOutStream out = new FileOutputStream("c:/write.txt");
    byte[] buffer = new byte[1024];
     int len;
     while( (len = in.read(buffer)) != -1){
     out.write(buffer,0,len);
     }
     in.close();
     out.close();
     //字符流
     BufferedReader bf = new BufferedReader(new FileReader("c:/read.txt");
     BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt");
    String str;
    while( (str=bf.readLine()) != null ){
     bw.write(str);
     bw.newLine();
    }
    bf.close();
    bw.close();
     }
    }

    29.同步和异步什么时候用?
    答:如果数据将在线程间共享,进行同步存取;如果应用程序在对象上调用时间长,建议使用异步。

    30.说出一些常用的类,包,接口?
    答:类:BufferedReader,BufferedWriter,String,Integer,System,Class,FileReader
    包:java.util,java.io,java.lang,java.sql,javax.servlet
    接口:List,Map,Set,Remote,Document

    总结

    • 本文讲了Java面试集合(三),如果您还有更好地理解,欢迎沟通

    • 定位:分享 Android&Java知识点,有兴趣可以继续关注

  • 相关阅读:
    朱晔和你聊Spring系列S1E10:强大且复杂的Spring Security(含OAuth2三角色+三模式完整例子)
    朱晔和你聊Spring系列S1E9:聊聊Spring的那些注解
    朱晔和你聊Spring系列S1E8:凑活着用的Spring Cloud(含一个实际业务贯穿所有组件的完整例子)
    朱晔和你聊Spring系列S1E7:简单好用的Spring Boot Actuator
    朱晔和你聊Spring系列S1E6:容易犯错的Spring AOP
    朱晔和你聊Spring系列S1E5:Spring WebFlux小探
    朱晔和你聊Spring系列S1E4:灵活但不算好用的Spring MVC
    朱晔和你聊Spring系列S1E3:Spring咖啡罐里的豆子
    朱晔和你聊Spring系列S1E2:SpringBoot并不神秘
    朱晔的互联网架构实践心得S1E10:数据的权衡和折腾【系列完】
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932691.html
Copyright © 2020-2023  润新知