Concise answers to several Java interview questions

Source: Internet
Author: User

The difference between equal and = = in Java:

The equal method is a comparison of object references unless overridden (such as the string class and the base type wrapper class). and = = is the comparison value in addition to the basic type, other cases are compared to the reference address.

Java adapter mode?

The original implementation of all the methods of the interface into the implementation of the interface method of the abstract class (adapter), and then implement the desired method.

The difference between an abstract class and an interface

The difference between an abstract class and a normal class is that an abstract class contains at least one abstract method and an abstract is added to the declaration.

Interface is a special kind of abstract class,has the following characteristics:Member variable: can only be a constant member function: only the abstract constructor method: Nonerelationships and differences:classes and classes: Inheritance, single inheritanceClasses and Interfaces: Single implementation, multiple implementations (this is also a form of multiple inheritance)Interfaces and interfaces: Single inheritance, multiple inheritance (this also makes up for Java's lack of multiple inheritance defects)The most important difference: the concept of designinterface classes: Like a (with some specific functions and methods, such as an animal that implements the interface for running and eating)Abstract class: is a (belongs to a category, such as cats belong to the cat, must inherit from the cat, the abstract class, but the cat is a concept not specifically implemented) java Multithreading:1. Thread safety issues: Multiple threads exist for shared data multiple statement operations on shared data can be divided into two cases (1) the same type of multiple threads such as multiple Windows sell ticket is a shared resource, sell ticket operation (2) different types of multiple lines Cheng to name and obtain Age Operator object is to share resources but do two operations, one to get information, the other is to set the parameters above two cases to ensure that thread security needs to be locked to ensure security (1) to lock the ticket operation (that is, the Run method in the operation of the shared resource statement plus synchronized (New object ()) statement, in which the lock object is also to ensure that all threads are the same) (2) To obtain information and set parameters are locked, and the lock to ensure the same! In fact, the above two situations are essentially the same, that is, the operation of the shared resources need to add a unique lock. 2. Thread deadlock problem description: Eg: thread 1 and Thread 2 respectively have a lock and B lock, now thread 1 want to get B lock for b operation, and thread 2 also want to get a lock for a operation, but the two locks are each other owned, at this time will appear to each other that the other side of the lock does not give up the phenomenon of lock Causes a blocking deadlock state to enter. Generally speaking: refers to two or more than two processes in the implementation process, because of the competition for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to proceed.another way to interpret this:Deadlock is because of the multi-threaded access to shared resources, due to improper access, usually a thread locked a resource A, but also want to lock resource B, and in another thread, the lock resource B, but also to lock resource A to complete its own operations, two threads want the other's resources, Instead of releasing their own resources, two of threads are waiting for a situation that cannot be performed. Java Network Programming approximate process:UDP programming Send side:1, create the Socket object constructor Datagramsocket () 2. Create data and package byte[] bts= "HelloWorld". GetBytes () Datagrampacket Dp=newdatagrampacket (Bts,bts.getlength,inetaddress.getbyname ("1 92.168.1.12 "), 10086) 3. Data transmission Ds.sent (DP) 4. Release Ds.close () Receiving end:1, create socket object Datagramsocket ds=new datagramsocket (port number [>1000]) 2, create package Datagrampacket dp=new Datagrampacket (Bytes,bytes.length ())//bytes is a byte array byte[], holds data 3, receives data Ds.receive (DP) 4, parses the data and displays or applies a String   Ip=dp.getaddress (). gethostaddress ();                     Get the IP address string data=string (Dp.getdata (), 0,dp.getlength ())//Get the strings in the packet 5, release the resource Ds.close () TCP Programming: Client1, new Socket object 2, get output stream, write data outputStream3, release resources Server-Side1, create a new ServerSocket object 2, listen to the client's Sokcet, connect the socket s= new ServerSocket (). Accept (); 3, get the input stream, read the data; 4, release the resources

Concise answers to several Java interview questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.