本文共 8746 字,大约阅读时间需要 29 分钟。
一、阅读代码题:
1.
public static void main(String[] args) {
int i=128;//(int)占4个字节 00000000 00000000 00000000 1000 0000 128
short s=28;//(short)占2个字节 000000000 0001 1000 28
byte b=(byte)i;//(byte)占1个字节1000 0000 -128 java规定-0(10000000)为-128。
int x=b+s;//-100
System.out.println(x);
}
答案:-100
2.
public static void main(String[] args) {
int i=128;
List list=new ArrayList<>();
list.add(new Object());//java.lang.Object@4e25154f
list.add("hi");//hi
list.add(i);//128
System.out.println(list.get(1));
}
答案:hi
3.
public class Test5 {
int id;
String name;
void display() {
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Test5 t1=new Test5();
Test5 t2=new Test5();
t1.display();
t2.display();
}
}
答案:
0 null
0 null
4.class Vehicle{
Vehicle() { //父类构造方法
System.out.println("Vehicle is created");
}
}
class Test5 extends Vehicle{
Test5() { //子类构造方法
System.out.println("constructor is invoked");
}
int speed;
Test5(int speed){ //子类有参构造方法
this.speed=speed;
System.out.println(speed);
}
//普通的初始化块
{
System.out.println("instance initializer block invoked");
}
public static void main(String[] args) {
Vehicle b1=new Vehicle();// Vehicle is created
Test5 b=new Test5(128);//Vehicle is created instance initializer block invoked 128
Test5 b2=new Test5(256);//Vehicle is created instance initializer block invoked 256
}
}
答案:
Vehicle is created
Vehicle is created
instance initializer block invoked
128
Vehicle is created
instance initializer block invoked
256
5.
public class Test6{
public static void main(String[] args) {
new Test6().doSomething();
}
public void doSomething() {
int i=5;
Thread t=new Thread(new Runnable() {
public void run() {
for(int j=0;j<i;j++) {
System.out.print(" " +j);
}
}
});
t.start();
}
}
答案: 0 1 2 3 4
6.
public class Test6{
public static void main(String[] args) {
Integer i=127,j=127;
Integer a=128,b=128;
Integer m=new Integer(127);
Integer n=new Integer(127);
System.out.println(i==j);
System.out.println(a==b);
System.out.println(m==n);
}
/*常量池在java中用于保存编译期已经确定的,它包括了关于类,方法,接口中的常量,也包括字符串常量。例如
String s = "Java" 这种声明的方式。产生的这种"常量"就会被放到常量池,常量池是JVM的一块特殊的内存空间。
使用Java常量池技术,是为了方便快捷地创建某些对象,当你需要一个对象时候,就去这个池子里面找,找不到就在池子里面创建一个。
但是必须注意 如果对象是用new 创建的。那么不管是什么对像,它是不会放到池子里的,而是向堆申请新的空间存储。
java中基本类型的包装类的大部分都实现了常量池技术,这些类是Byte,Short,Integer,Long,Character,Boolean,另外两种浮点数类型的包装类则没有实现。
另外Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时才可使用对象池。超过了就要申请空间创建对象了
”==“ 我们知道比较的是引用的地址(其实java不要允许用户直接对内存进行操作的的)。
*/
}
7.
public class Test6{
public static void main(String[] args) {
/*
* objList.remove(0);//asList()返回的是arrays中私有的终极ArrayList类型,它有set,get,contains方法,但没有增加和删除元素的方法,所以大小固定,会报错
objList.add(0);//由于asList返回的list的实现类中无add方法,所以会报错
*/
String[] s= { "a","b","c"};
List<String> list=Arrays.asList(s);//将数组转成list
list.add("d");
System.out.println(list.size());
}
}
答案:运行时异常
8.
public static void main(String[] args) {
BigDecimal a=new BigDecimal(2);
BigDecimal b=new BigDecimal("3");
a.multiply(b);//乘
System.out.println(a);
}
答案:2
9.
class Bike{
final void run() {
System.out.println("running");
}
}
public class Test6 extends Bike{
void run() {
System.out.println("running safely with 100kmh");
}
public static void main(String[] args) {
Test6 honda=new Test6();
honda.run();
}
}
答案:编译错误,final修饰的方法不能被继承,重写
10.
class Bike2{
int speedlimit=90;
public void display(){
System.out.println(this.speedlimit);
}
}
class Test11 extends Bike2{
int speedlimit=150;
public void display(){
System.out.println(this.speedlimit);
}
public static void main(String[] args) {
Bike2 obj=new Test11();
System.out.println(obj.speedlimit);
obj.display();
}
}
答案:90
150
11.
public static void main(String[] args) {
int n;
for(n=1;n<5;++n) {
break;
}
System.out.println(n);
}
答案:1
12.
public static void main(String[] args) {
int n;
for(n=1;n<5;++n) {
continue;
}
System.out.println(n);
}
答案:5
13.
public class Test11 implements Runnable {
public void run() {
System.out.println("running");
}
public static void main(String[] args) {
Test11 c=new Test11();
Thread t=new Thread(c);
t.start();
}
}
答案:注意是implements而不是extends
14.
class A{
void msg1() {
System.out.println("Hello");
}
}
class B{
void msg() {
System.out.println("Welcome");
}
}
public class Test15 extends A,B{
Test15 c=new Test15();
c.msg();
}
答案:编译错误 不能继承两个父类
15.有个List<People>集合,如何实现按age进行排序输出
class Person {
private String id;
private String name;
private int age;
public Person(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class PersonSort {
public static void main(String[] args) {
List<Person> plist = new ArrayList<Person>();
//创建3个Person对象,年龄分别是32、20、25,并将他们依次放入List中
Person p1 = new Person("0001","zhangsan",32);
Person p2 = new Person("0002","lisi",20);
Person p3 = new Person("0003","wangwu",25);
plist.add(p1);
plist.add(p2);
plist.add(p3);
System.out.println("排序前的结果:"+plist);
Collections.sort(plist, new Comparator<Person>(){
/*
* int compare(Person p1, Person p2) 返回一个基本类型的整型,
* 返回负数表示:p1 小于p2,
* 返回0 表示:p1和p2相等,
* 返回正数表示:p1大于p2
*/
public int compare(Person p1, Person p2) {
//按照Person的年龄进行升序排列
if(p1.getAge() > p2.getAge()){
return 1;
}
if(p1.getAge() == p2.getAge()){
return 0;
}
return -1;
}
});
System.out.println("排序后的结果:"+plist);
}
}
16.有个List<People>集合,得到其中年龄大于28的姓名和年龄的map对象
17.如何实现同步控制
(1)同步方法:即有synchronized关键字修饰的方法
由于java的每个对象都有一个内置锁,当用此关键字修饰方法时,内置锁会保护整个方法。在调用该方法时,需要获得内置锁,否则就处于阻塞状态。
代码如:public synchronized void save{}
注: synchronized关键字也可以修饰静态方法,此时如果调用该静态方法,将会锁住整个类
(2)同步代码块:即有synchronized关键字修饰的语句块
被该关键字修饰的语句块会自动被加上内置锁,从而实现同步。
代码如:synchronized(object){}
注:同步是一种高开销的操作,因此应该尽量减少同步的内容。
通常没有必要同步整个方法,使用synchronized代码块同步关键代码即可。
(3)使用特殊域变量(volatile)实现线程同步
a.volatile关键字为域变量的访问提供了一种免锁机制,
b.使用volatile修饰域相当于告诉该域可能会被其他线程更新,
c.因此每次使用该域就要重新计算,而不是使用寄存器中的值
d.volatile不会提供任何原子操作,它也不能用来修饰final类型的变量 (4)使用重入锁实现线程同步
在JavaSE5.0中新增了一个java.util.concurrent包来支持同步。ReentrantLock类是可重入、互斥、实现了Lock接口的锁, 它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力。
18.创建一个枚举类
public enum Weekday {
SUN,MON,TUS,WED,THU,FRI,SAT
}
19.创建一个自定义注解类Test,包含一个value注解项
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)//注解的作用范围,就是注解是用在什么地方的
@Retention(RetentionPolicy.RUNTIME)//注解的级别,就是注解能留存到什么时候
@Documented//用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
@Inherited//@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
//注意:接口和方法上的注解不能被继承
public @interface Weekday {
public String value();//注解可以接收的参数
}
20.mysql修改table1的A字段varchar型变成int型
解:修改字段用alter命令,如果该列中存在超过INT最大范围值(-32767到+32768)或非法INT数据,则需要事先将数据处理,否则提示相关错误。
alter table table1 alter column A
21.mysql中int型的数字怎么转换成字符串
/* 比如将123转换为char类型 */
SELECT CAST(123 AS CHAR);
/* 或者使用concat方法 */
SELECT CONCAT(123,'');
22.mysql删除table1的记录(1)delete from 表名 where 条件;,这个语句带条件的删除数据,可以删除指定的数据记录。(2)truncate table 表名;,这个语句可以快速删除表中的所有数据,是不带条件的删除。
23.简述常用的设计模式及应用场景,并编码写出一个单例模式
按照目的来分,设计模式可以分为创建型模式、结构型模式和行为型模式。
创建型模式用来处理对象的创建过程;结构型模式用来处理类或者对象的组合;行为型模式用来对类或对象怎样交互和怎样分配职责进行描述。
创建型模式用来处理对象的创建过程,主要包含以下5种设计模式:
Ø 工厂方法模式(Factory Method Pattern)
Ø 抽象工厂模式(Abstract Factory Pattern)
Ø 建造者模式(Builder Pattern)
Ø 原型模式(Prototype Pattern)
Ø 单例模式(Singleton Pattern)
结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:
Ø 适配器模式(Adapter Pattern)
Ø 桥接模式(Bridge Pattern)
Ø 组合模式(Composite Pattern)
Ø 装饰者模式(Decorator Pattern)
Ø 外观模式(Facade Pattern)
Ø 享元模式(Flyweight Pattern)
Ø 代理模式(Proxy Pattern)
行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,主要包含以下11种设计模式:
Ø 责任链模式(Chain of Responsibility Pattern)
Ø 命令模式(Command Pattern)
Ø 解释器模式(Interpreter Pattern)
Ø 迭代器模式(Iterator Pattern)
Ø 中介者模式(Mediator Pattern)
Ø 备忘录模式(Memento Pattern)
Ø 观察者模式(Observer Pattern)
Ø 状态模式(State Pattern)
Ø 策略模式(Strategy Pattern)
Ø 模板方法模式(Template Method Pattern)
Ø 访问者模式(Visitor Pattern)
单例模式实现1:
public class Singleton {
// 类共享实例对象
private static Singleton singleton = null;
// 私有构造方法
private Singleton() {
System.out.println("-- this is Singleton!!!");
}
// 获得单例方法
public synchronized static Singleton getInstance() {
// 判断 共享对象是否为null ,如何为null则new一个新对象
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
单例模式实现2:
public class Singleton {
// 类共享实例对象 实例化
private static Singleton singleton = new Singleton();
// 私有构造方法
private Singleton() {
System.out.println("-- this is Singleton!!!");
}
// 获得单例方法
public static Singleton getInstance() {
// 直接返回共享对象
return singleton;
}
}
转载地址:https://codingchaozhang.blog.csdn.net/article/details/79684957 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!