OO Concepts


1.What is the difference between a System Thread and a User Thread?

The system creates the system thread. Everything starts with the system thread – it is the first and main thread. The application usually ends when the system thread terminates. User threads are created by the application to do tasks that either cannot or should not be done by the system thread.

In short, system threads are the main threads used in an application, whereas user threads are created to handle different tasks as they come to the application.

举个栗子

System Thread: Application that displays user interfaces, waits for and responses to events

User Thread: Created by application and used to handle user requests and responses.

2.Stack and Heap and Which is Faster

The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.

The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation)

The stack is faster, because it's easy to allocate and deallocate memory (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be - typically - synchronized with "all" other heap accesses in the program.

3.What will happen after I input an url in a browser and hit enter? StackOverflow原题

First the computer looks up the destination host. If it exists in local DNS cache, it uses that information. Otherwise, DNS querying is performed until the IP address is found.(通过DNS查询目标主机的IP地址,DNS is just like a phone book [url:IP])

Then, browser opens a TCP connection to the destination host and sends the request according to HTTP 1.1 (or might use HTTP 1.0, but normal browsers don't do it any more). (浏览器开启本地和主机之间的TCP连接,并基于HTTP向服务器发送请求)

The server looks up the required resource (if it exists) and responds using HTTP protocol, sends the data to the client (=your browser) (服务器端查找请求的资源,如果存在则基于HTTP响应请求,并发送数据给用户端/浏览器)

The browser then uses HTML parser to re-create document structure which is later presented to you on screen. If it finds references to external resources, such as pictures, css files, javascript files, these are delivered the same way as the HTML document itself. (浏览器使用HTML文件解析器,重建HTML的结构并显示)

RESTful - A certain representation of resource is transmitted between client and server.

资源 - HTML, Javascript, CSS, Media/Video等

4.GET 和 POST区别 refer to: http://jkorpela.fi/forms/methods.html

Two commonly used methods for a request-response between a client and server are: GET and POST.

• GET - Requests data from a specified resource or form. (比如render html 并向用户请求数据)

• POST - Get the requested data, process and submit it to a specified resource or form.

The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body. But the specifications also give the usage recommendation that the "GET" method should be used when the form processing is "idempotent", and in those cases only. As a simplification, we might say that "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.

5. C++ 里面short ,int ,long的区别

The sizes of the primitive types int, char, short, long, etc. are implementation-defined and can vary from system to system. All that you're guaranteed is that

sizeof(char) == 1, and

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).

6.如果要sort 很大量的data,但是ram 不够用, 怎么办

external sort有多种方法,比如external merge sort

举个栗子

If there are 900 MB data to be processed, while only 100 MB available memory space.

First, read 100 MB data into the memory, sort it using any algorithms(merge, quick, heap)

Second, write the sorted data to a temp file in disk.

Repeat the above steps, then we could get 9 temp files.

Third, read first 10 MB of each file into the input buffer, and use remaining 10 MB memory as output buffer (9*10+10=100MB).

[k * input buffer + output buffer <= raw memory]

So, we apply a 9-way merge to the 9 input buffers, and store the merged data into output buffer. Once the output buffer is full, write its data to a specified target file, and free the output buffer. Whenever an input buffer is empty, fill it using data from its associated temp file. Finally, we have a 900 MB target file that stores the sorted data.

7. Why String is Immutable?

String is immutable object, but actually it is stored in the executable file, which is unmodifiable.

  • the requirement of String Pool:
    • when a string is created and if the string already existed in the pool, the reference of the existing string will be returned.
  • caching Hashcode
    • The hashcode of a string is frequently used in java, e.g. HashMap or HashSet. Being immutable guarantees that hashcode will always be the same so that it can be cashed without worrying about the changes.That means, there is no need to calculate hashcode every time it is used. This is more efficient.
  • security
    • String is widely used as a parameter for many java classes, e.g. network connection, opening files, etc. Were String not immutable, a connection or file would be changed and this can lead to a serious security threat.
  • naturally thread safe
    • immutable objects can not be changed, they can be shared among multiple threads freely.

8. What's the difference between Interface and abstract class?

In Java:

An interface is an empty shell. It is more abstract by nature. It cannot be instantiated, and cannot define concrete methods.

It can be "inherited" by a subclass(using keyword implements, instead of extends), or by another interface using extends. We can put data and methods (no definition, only declaration) into it. While in abstract class, you can define some concrete methods.

-class class, extends

-interface class, implements (一个类可以实现多个接口,i.e. class W implements A, B, C / class X extends Y, implements D, E )

-interface interface, extends

Abstract classes, can not be instantiated. And, unlike interfaces, they are classes, and you can define concrete methods, or declare abstract methods. Also, they are more expensive to use, because there is a look-up to do when you inherit from them.

抽象需要key word - abstract。

Why need Interface? cause Java class doesn't inherit from multiple classes.

9. What's Encapsulation? refer to: https://www.w3resource.com/java-tutorial/java-object-oriented-programming.php

A class binding its data and methods, so that they're only accessible by those classes/objects we trust. (setter&getter)

other notes: there are three main features in OOP:

  • Encapsulation
  • Inheritance: create classes that share attributes and method of existing classes. ...code reusability
    • basically, it is a is-a relationship
    • has-a relationship
      • Has-a relationship is composition relationship which is a productive way of code reuse.
      • Composition is dynamic binding (run-time binding) while Inheritance is static binding (compile time binding)
  • Polymorphism: allows the same word or symbol to be interpreted differently in different situations based on the context. There are two types of Polymorphism available in Java
    • Static Polymorphism (compile time polymorphism/ Method overloading)
      • allows two or more methods with the same name to perform the different methods implementations just based on the different argument list.
    • Dynamic Polymorphism (run time polymorphism/ Method Overriding)
      • When you create a subclass by extending an existing class, the new subclass contains data and methods that were defined in the original superclass. Sometimes, however, the superclass data fields and methods are not entirely appropriate for the subclass objects; in these cases, you want to override the parent class members.

10. What's overloading?

Overloading is a feature that allows two or more methods with the same name to perform the different logic/functionality, but at least one of their number, type, order of parameters must be different. (return type, decorator of method can be different)

11. What's overriding?

Overriding is a situation when we define a method in a subclass, which has the same name and parameters with its superclass, then the method of superclass is overwritten.

12. What's virtual function?

A virtual function is an inheritable and overridable method. (with a keyword "virtual" prior to that function)

13. What's OOP?

Object-oriented programming is a programming paradigm based on the concept of "objects". Usually, objects are instances of classes. Classes contain data and methods that operate on it. We can use object to operate on those data and methods, or let them to interact with each other, while keep their own status independent.

16. What's OOD?

Object-oriented design is a priciple that helps programmers to logically organize the code. It involves some good features, like encapsulation and inheritance.

17. Hashtable 和 HashMap 的相同与不同

相同点是都是java的集合类,都可以存放java对象。

主要的不同点有二,一是Hashtable是同步的,它里面的对象是线程安全的,而HashMap是异步的,其对象是线程不安全的。同步会影响执行效率,因此,当不需要线程安全的集合时,可以使用HashMap,可以避免同步带来的性能开销,提高效率。

二是Hashtable不同存放null作为key或value,而HashMap可以。

18. ArrayList 和 Vector的相同与不同

相同点是都是java的集合类,动态数组,用来存放java对象。

不同点有二,一是Vector是同步的,而ArrayList不是。

二体现在数据增长,如果元素的数目超出了内部数组目前的长度,它们都会扩展数组的长度。Vector会增长为原来1倍,ArrayList是原来的50%。所以当要存放大量数据时用Vector有一定优势,因为可以设置集合的初始化大小,避免不必要的资源开销。

other simple questions:

1. difference among four access modifies

public: class, variable, method... declared public is visible to any class in the java program.

protected: first, protected cannot be used in class or interface. Then, variable, method... declared protected in super-class can be accessed in other subclass in other package. or accessed in other class in the same package.

default(no-modifiers): class, variable, method... without any declared access modifiers is accessible only by classes in the same package.

private: The private (most restrictive) modifiers can be used for members but cannot be used for classes and Interfaces.

2. what's the difference between class, object, and instance?

the class is like the blueprint, it holds a Description of a particular Category. e.g. HumanBeing firstHuman = new HumanBeing(); it instantiates the HumanBeing with name, age, height,etc., and we create an object of the class... which is created on heap memory and some address is allocated to it. firstHuman holds the reference to that address. in this time, the object is the object of HumanBeing, and also the instance of the HumanBeing.

However, if we make a Doctor class which extends the HumanBeing. when we instantiate the Doctor class in a similar way. Actually, we know the following things:

  • firstDoctor is An Object of class Doctor And An Instance of A class Doctor
  • firstDoctor is Not An Object of class HumanBeing But An Instance of class HumanBeing

refer to: https://stackoverflow.com/questions/3323330/difference-between-object-and-instance

results matching ""

    No results matching ""