JAVA基础面试题:
1: String Stringbuffer StringBuilder区别
String为字符串常量,而StringBuilder和StringBuffer为字符串变量,String对象一旦创建之后改对象不可改变,而后面两者的对象是变量,是可以改变的。在线程安全上,StringBuilder是线程不安全的,而StringBuffer是线程安全的,使用效率,String适用于少量的字符串操作的情况,StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况,StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况。
2: ArrayList 和LinkedList区别
LinkedList和ArrayList都是List接口的实现类,ArrayList是动态数组的数据结构,顺序表的地址是连续的,LinkedList是链表的数据结构,
链表的存储地址是不连续的,每一个存储地址都是通过指针指向,效率方面,随机访问get和set,ArrayList优于LinkedList,在数组中新增和删除操作LinkedList占优势,因为ArrayList要移动数据。
3: 数组有没有length()这个方法,String 有没有length()这个方法
数组没有length()这个方法,但是有length属性,String有length()方法
4:int 和 Integer 有什么区别
Int是Java的基本数据类型,Integer是int包装类,Integer变量必须实例化才能使用,而int变量不需要。Integer的默认值是null,int的默认是0.
5:面向对象特性
面向对象的四大特征是抽象,封装,继承,多态,抽象,封装,继承是多态的基础,多态是抽象,封装,继承的表现。
封装:良好的封装能够减少耦合,类内部的结构可以自由修改,可以对成员变量精确的控制,隐藏信息,实现等细节。
继承:子类拥有父类的非private的属性和方法。
子类拥有自己的属性和方法,即子类对父类进行扩展。
子类可以用自己的方式实现父类的方法。
6:异常处理的时候,finally代码块的重要性是什么?
异常处理无论是否抛出异常finally都会被执行,finally主要作用是在抛出异常的时候释放资源,比如I/O缓冲区,数据库连接。
7:Java中的两种异常类型是什么?他们有什么区别?
Java异常类型分为两种一种是Checked异常,另一种是Runtime异常,所有RuntimeException类以及子类的实例被称为Runtime异常,不属于改范畴的异常则被称为CheckedException,常见的Checked异常有ClassNotFoundException,IOException等。我们比较熟悉的RumtimeException类的子类有ClassCastException,NullPointerException。
8:HashSet和TreeSet有什么区别?
HashSet是set接口的实现类,存储是无序唯一的对象,由于没有顺序list很多方法他都没有,所有不能用普通的for循环来遍历,只有加强型for和迭代器联众遍历方法。
TreeSet是有序的Set集合,他继承与AbstractSet类,支持两种排序方式,自然排序或者根据创建的TreeSet提供的Comparator进行排序。TreeSet是有序的Set集合,因此支持add,remove,get等方法。
9:HashMap和Hashtable有什么区别?
HashMap基于hashing原理,我们通过put()和get()方式储存对象 HashMap是非Synchronized,HashTable是Synchronized,意味着Hashtable是线性安全的,多个线程可以共享一个HashTable。List集合允许有重复对象,set集合不允许有重复对象。
Hashmap原理我们put()方法传递键和值时,我们相对键调用hashCode()方法,hashCode找到bucket位置来存储entry对象。
10:深拷贝和浅拷贝的区别:
深拷贝:引用对象的值等信息,复制一份一样的。
浅拷贝:只复制引用,另一处修改,当下的对象也会修改。
算法题(6题):
1:泡沫排序算法
2:按照矩阵顺时针打印数字
3:输入一个字符串打印改字符串数组的所有排序算法
4:编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
5:编写一个程序,输入n,求n!(用递归的方式实现)。
6:给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。
类似代码:
1:泡沫排序算法
/************************** 1:泡沫排序算法 ***************************/
/***
* 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,
* 将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。重复第一趟步骤,直至全部排序完成。
***/
public static void method_1() {
// 第一趟比较的时候,最后的一个数字是那个数中的最大数,第二趟比较时,最后那个数字不参与比较;第二趟比较:倒数第二个数字在第三趟比较的时候不参与比较,以此类推:
// 例如:比较数组 int[] aar={100,32,24,545,4,109};
int[] array = { 100, 32, 24, 545, 4, 109 };
System.out.println("***************排序前数组********************");
for (int k = 0; k < array.length; k++) {
System.out.print(array[k] + " ");
}
for (int k = 0; k < array.length - 1; k++) {
for (int i = 0; i < array.length - 1 - k; i++) {
if (array[i] > array[i + 1]) {
int temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
System.out.println();
System.out.println("***************排序后数组********************");
for (int k = 0; k < array.length; k++) {
System.out.print(array[k] + " ");
}
}
2:按照矩阵顺时针打印数字
/************************** 1:按照矩阵顺时针打印数字 ***************************/
/***
* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
* 28 29 30 31 32 33 34 35 36
*
***/
public static void method_2() {
int[][] num = new int[100][100];
int n = 6;
int count = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
num[i][j] = count++;
}
}
printNumber(num, 0, n - 1);
}
// 一个顺时针轮回打印的函数
public static void printNumber(int[][] num, int start, int end) {
// 如果起始点大于结束点,并且结束点小于0表示顺时针打印结束了
if (start >= end || end <= 0)
return;
// 横向打印从左到右
for (int i = start; i <= end; i++) {
System.out.print(num[start][i] + " ");
}
// 纵向打印从上到下
for (int i = start + 1; i <= end; i++) {
System.out.print(num[i][end] + " ");
}
// 横向打印从右到左
for (int i = end - 1; i >= start; i--) {
System.out.print(num[end][i] + " ");
}
// 纵向打印从下到上
for (int i = end - 1; i > start; i--) {
System.out.print(num[i][start] + " ");
}
printNumber(num, start + 1, end - 1);
}
3:输入一个字符串打印改字符串数组的所有排序算法
/**************************
* 3:输入一个字符串打印该字符串数组的所有排序算法
***************************/
public static void method_3() {
char[] cs = { 'a', 'b', 'c'};
int length = cs.length;
recursionSwap(cs, 0, length);
}
public static void swap(char[] cs, int index1, int index2) {
char temp = cs[index1];
cs[index1] = cs[index2];
cs[index2] = temp;
}
public static void recursionSwap(char[] cs, int start, int length) {
if (start >= length - 1) {
print(cs);
return;
}
for (int i = start; i < length; i++) {
swap(cs, start, i);
recursionSwap(cs, start + 1, length);
swap(cs, start, i);
}
}
public static void print(char[] cs) {
for (int i = 0; i < cs.length; i++) {
System.out.print(cs[i]);
}
System.out.println();
}
4:编写一个程序,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
/**************************
* 4:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
***************************/
public static void method_4() {
int i = 0;
int j = 0;
int k = 0;
for(i = 1;i <=4;i++){
for(j = 1;j<=4;j++){
for(k = 1;k <=4;k++){
if(i != k && i != j && j != k){
System.out.println(i * 100 + k * 10 + j);
}
}
}
}
}
5:编写一个程序,输入n,求n!(用递归的方式实现)。
/**************************
* 5:编写一个程序,输入n,求n!(用递归的方式实现)
***************************/
public static void method_5() {
int m = number(3);
System.out.println("num:" + m);
}
public static int number(int num){
if(num < 1){
System.out.println("请传入大于1的整数");
}
if(num == 1){
return 1;
}else{
return num * number(num - 1);
}
}
6:给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。
/***
* (6)给定String,求此字符串的单词数量。字符串不包括标点,大写字母。例如 String str="hello world hello hi";单词数量为3,分别是:hello world hi。
***/
public static void method_6(){
int count = 0;
String str="hello world hello she not sss";
String newStr="";
HashMap<String,String> m=new HashMap<String,String>();
String [] a=str.split(" ");
for (int i=0;i<a.length;i++){
if(!m.containsKey(a[i])){
m.put(a[i],"1");
count++;
newStr=newStr+" "+a[i];
}
}
System.out.println("这段短文单词的个数是:"+count+","+newStr);
}