Interview notes

本文深入探讨了Java中构造器与普通方法的区别、序列化的应用场景、实例变量与静态变量的不同之处,以及final、finalize和finally等关键字的区别。此外,还介绍了运行时异常与检查型异常的区别、MVC模式的概念、Servlet生命周期方法的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The difference between constructors and regular methods

1.Constructors have the same name as the class name and cannot return a value. Regular methods can have any name and can be called any number of times.

2.Constructors are called only once per creation of an object while regular methods can be called many times.

 

When to use serialization

1.A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database.

2.The objects stored in an HTTP session should be serializable.

3.Objects are passed in RMI across network using serialization.

 

Serialize an object to a file

Car car = new Car();

FileOutputStream fos = new FileOutputStream(filename);

ObjectOutputStream out = new ObjectOutputStream(fos);

out.writeObject(car);

out.close();

 

The difference between an instance variable and a static variable

1.Instance variables are non-static and there is one occurrence of an instance variable in each class instance.

2.Class variables are called static variables. There is only one occurrence of a class variable per JVM per class loader.

 

The difference between final, finalize and finally

final

1.A final class can’t be extended.

2.A final method can’t be overridden when its class is inherited.

3.Can’t change value of a final variable.

finally, handles exception

finalize, method helps in garbage collection.

 

The difference between Runtime(unchecked) exceptions and checked exceptions

Unchecked exceptions:

1.represent defects in the program.

2.subclasses of RuntimeException.

3.a method is not obliged to handle the unchecked exceptions.

Checked exceptions:

1.represent invalid conditions in areas outside the immediate control of the program.

2.subclasses of Exception.

3.a method is obliged to handle all the checked exceptions.

 

MVC

Model, represent the core business logic and state. A model commonly maps to data in the database and will also contain core business logic.

View, renders the contents of a model. A view accesses the data from the model and adds display logic to present the data.

Controller, acts as the glue between a model and a view.

 

The life cycle methods of a servlet

The Web container is responsible for managing the servlet’s life cycle.

1.The Web container creates an instance of the servlet and then the container calls the init() method.

2.At the completion of the init() method the servlet is in ready state to service requests from clients.

3.The container calls the servlet’s service() method for handling each request by the spawning a new thread for each request from the Web container’s thread pool.

4.Before destroying the instance the container will call the destroy() method.

 

The difference between doGet() and doPost() or GET and POST

1. The request parameters are transmitted as a query string appended to the request. This is a security risk. GET is not appropriate when large amounts of input data are being transferred.

2. The request parameters are passed with the body of the request. More secured. Can send much more information to the server.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值