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中實現多態的機制是?答:父類或接口定義的引用變量指向子類具體實現類的實例對象,引用變量指向具體的實例對象。

小編相信這裏有很多學習java的朋友,小編整理了一份java方面的學習資料,想要獲取的可以加我的java學習羣的喲,928204055。歡迎愛學習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; falsepublic 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函數執行靜態靜態mainclass 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(); }}//resultparent靜態代碼塊child靜態mainparent非靜態代碼塊parent構造方法child非靜態child構造方法

28.說說IO?

//第一種:輸入流輸出流//第二種:字節流字符流//第三種:節點流處理流//FileInputStreamclass 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和BufferedReaderclass 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章