Wiky's Blog

Java笔试面试常考常问题总结

本来想着写一些笔试自己遇到的多数的坑,然而写着写着就想到了很多面试遇到的比较有意思的问题,在此特意总结一下,希望自己以后可以吃一暂长一智吧。
switch
1.switch支持的数据类型:

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
byte bb=1;
char cc='测';
short ss=12;
int i=0;
long ll=12333L;
float f=1.3f;
double d=12.3;
boolean b1=true;
String string="123";
switch(bb){
}
switch(cc){
}
switch(ss){
}
switch(i){
}
//以下情况会编译报错
switch (ll){
}
switch(f){
}
switch(d){
}
switch(b1){
}
switch(string){
}

可以看到,Java的switch语句支持的数据类型包括8种数据类型中的4种(这四种不需要强制类型转换都能够转换成int类型),另外还支持String类型。不支持的基本数据类型有float,double,long,boolean。

##HashMap##
感觉这个数据结构是大多数面试官考察的重点。所以这一部分不得不说是一个很值得去探讨的方面。
首先HashCode和equals究竟是长啥样的?

###hashcode和equals###
1.equals返回true,那么hashcode返回值也是一样。但是反之不成立。
2.Object的默认实现:
hashcode返回的是对象的内存地址,equals实现是x==y;

1
2
3
4
5
6
7
Object b=new Object();
Object a=new Object();
Object c=a;
System.out.println(b.hashCode()+" "+a.hashCode());//366712642 1829164700
System.out.println(a.equals(b));//false
System.out.println(a==c);//true
System.out.println(a.equals(c));//true

*基本数据类型的封装类型的hashcode和equals
基本数据类型的封装类型hashcode返回的是值,equals比较的是
值相等。

###发表一下你对Spring,SpringMVC,MyBatis框架的理解###
1.好吧,那天我的语言组织能力有点混乱,然后答得不是很好。
这些问题其实也是很考验我们的能力的,所以会努力的组织一下。

###数据库###
1.问了jdbc如何获取后台数据
那天不记得很多具体代码实现了,所以答得也不是特别的好;

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package jdbc;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;;
public class JDBCTest {
//驱动
//private static String jdbDriver="com.mysql.jdbc.Driver";
private static String jdbcDriver="";
//数据表的位置
//private static String jdbUrl="jdbc:mysql://localhost:3306/myuser";
private static String jdbcUrl="";
//用户
//private static String jdbUser="root";
private static String jdbcUser="";
//密码
//private static String jdbpwd="123456";
private static String jdbcpwd="";
//连接
private static Connection conn;
//操作数据库的对象
private static java.sql.Statement st;
static{
FileInputStream iStream=null;
try{
iStream=new FileInputStream("database.properties");
Properties properties=new Properties();
properties.load(iStream);//在根目录下
jdbcDriver=properties.getProperty("jdbcDriver");
jdbcUrl=properties.getProperty("jdbcUrl");
jdbcUser=properties.getProperty("jdbcUser");
jdbcpwd=properties.getProperty("jdbcpwd");
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(iStream!=null){
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static Connection getMyConn(){
try {
Class.forName(jdbcDriver);
conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcpwd);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//根据驱动去获得连接
public static Connection getConnection(){
Connection con=null;
try {
//加载驱动器
Class.forName(jdbcDriver);
//驱动利用驱动url,用户名,密码创建连接
con=(Connection) DriverManager.getConnection(
jdbcUrl,jdbcUser,jdbcpwd);
}catch (ClassNotFoundException e) {//加载驱动器异常
e.printStackTrace();
} catch (SQLException e) {//创建连接异常
e.printStackTrace();
}
return con;
}
//创建表,可以这么做但是无法准确得知结果
public static boolean createTable(){
boolean isSucceed=false;
conn=getConnection();
try{
//INSERT INTO `staff` (`ID`, `name`, `age`, `sex`, `address`, `depart`, `worklen`, `wage`)
//VALUES (NULL, 'lisi', '62', 'm', 'ACM', 'personnel', '10', '3000');
//创建表的SQL语句
String sql="CREATE TABLE JDBCTEST "+
"(ID INTEGER NOT NULL,"+
" FIRST VARCHAR(255),"+
" LAST VARCHAR(255),"+
" AGE INTEGER,"+
" PRIMARY KEY( ID))";
//连接创建数据库操作对象,Statement
st=conn.createStatement();
//Statement利用executeUpdate方法执行sql语句
//executeUpdate返回的是影响的行数,对Create/drop table等不操作行的操作返回0;
//int flag=st.executeUpdate(sql);
int flag=st.executeUpdate(sql);
if(flag==0){
isSucceed=false;
}else{
isSucceed=true;
}
}catch(SQLException e){
e.printStackTrace();
}finally{//最后关闭连接,放在finally里面可以保证抛出异常时连接能够正确关闭
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isSucceed;
}
//插入删除查找数据
public static boolean operate(){
boolean isSucceed=true;
conn=getConnection();
try{
//插入一个记录
//String sql="INSERT INTO jdbctest(first,last,age)"+" VALUES('programer','ceo',25);";
//删除一行
//String sql="DELETE from jdbctest where id=1;";
//修改一行
String sql="update jdbctest set first='cffo' where id=2;";
//连接创建数据库操作对象,Statement
st=conn.createStatement();
//Statement利用executeUpdate方法执行sql语句
//executeUpdate返回的是影响的行数,对Create/drop table等不操作行的操作返回0;
int flag=st.executeUpdate(sql);
if(flag==0){
isSucceed=false;
}else{
isSucceed=true;
}
}catch(SQLException e){
e.printStackTrace();
}finally{//最后关闭连接,放在finally里面可以保证抛出异常时连接能够正确关闭
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isSucceed;
}
//查找数据
public static ArrayList<Data> query(){
ArrayList<Data> result=new ArrayList<Data>();
conn=getConnection();
ResultSet rs;
Data sData;
try {
st=conn.createStatement();
String sql="select * from jdbctest;";
rs = st.executeQuery(sql);
while(rs.next()){
sData = new Data(rs.getString("first"), rs.getString("last"), rs.getInt("age"));
result.add(sData);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
//getConnection();
//insert();
System.out.println("1.创建表\n2.操作数据\n3.查询数据");
Scanner in=new Scanner(System.in);
int t=in.nextInt();
switch(t){
case 1:
if(createTable()){//这里会一直返回false
System.out.println("创建成功");
}else{
System.out.println("创建失败");
}
break;
case 2:
if(operate()){
System.out.println("操作成功");
}else{
System.out.println("操作失败");
}
break;
case 3:
ArrayList<Data> sArrayList=query();
for(Data aData:sArrayList){
System.out.println(aData);
}
break;
default:
break;
}
//createTable();
}
}

为了证明我自己曾经也是很认真的手写了jdbc的连接,获取数据的代码。所以觉得有必要把它放到这里来,然后以后可以重温一下。

热评文章