中级农民
- 积分
- 104
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-8-13
- 最后登录
- 1970-1-1
|
OOP:
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Abstraction. Abstraction means using simple things to represent complexity. In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times.
Encapsulation. This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself.
Inheritance. This is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.1
Polymorphism. This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables.
static vs non-static:
static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects
Interface in Java?
An interface is a completely “abstract class” that is used to group related methods with empty bodies;
Like abstract classes, interfaces cannot be used to create objects.
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma
Functional Interface
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces.
Override & Overload
Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with different signature in the class. In method overloading, return type can or can not be be same, but we must have to change the parameter because in java, we can not achieve the method overloading by changing only the return type of the method.
Method Overriding is a Run time polymorphism. In method overriding, derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, return type must be same or co-variant (return type may vary in same direction as the derived class).
Is java pass by reference or by value
Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it.
Abstract class and method
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class) Abstract classes cannot be instantiated, but they can be subclassed.
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).:
An abstract class can have both abstract and regular methods:
Abstract class and interface
with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.
In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
在 abstract class 方式中,Demo 可以有自己的数据成员,也可以有非 abstarct 的成员方法,而在 interface 方式的实现中,Demo 只能够有静态的不能被修改的数据成员(也就是必须是 static final 的,不过在 interface 中一般不定义数据成员),所有的成员方法都是 abstract 的。从某种意义上说,interface 是一种特殊形式的 abstract class
Comparable / Comparator
Comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface to compare its instances. Consider a Movie class that has members like, rating, name, year. We can implement the Comparable interface with the Movie class, and we override the method compareTo() of Comparable interface
Unlike Comparable, Comparator is external to the element type we are comparing. It’s a separate class. We create multiple separate classes (that implement Comparator) to compare by different members. Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.
What is garbage collector and how it works
The programmer need not to care for all those objects which are no longer in use. Garbage collector destroys these objects.
System.gc()
GC是Java中用于自动管理内存空间的一种守护线程。因为守护线程服务于用户线程,在用户线程结束之后才完成工作,所以正好符合GC的工作要求,GC是守护线程最广泛的应用。
What is transaction?
information processing in computer science that is divided into individual, indivisible operations called transactions. Each transaction must succeed or fail as a complete unit; it can never be only partially complete.
Do you know java bean
JavaBeans are classes that encapsulate many objects into a single object (the bean).
Must implement Serializable.
It should have a public no-arg constructor.
All properties in java bean must be private with public getters and setter methods.
How many types of memory areas are allocated by JVM
JVM (Java Virtual Machine) is an abstract machine, In other words, it is a program/software which takes Java bytecode and converts the byte code (line by line) into machine understandable code.
Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack
What is constructor?
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
Java的概念基本补全啦 这样大家就不用去下载文档了(虽然文档里面有链接可能会更详细一点),但是可以帮大家省下来一个大米哈 谢谢大家的大米啦! |
|