Concise answers to several java interview questions, concise java interview questions

Source: Internet
Author: User
Tags connect socket

Concise answers to several java interview questions, concise java interview questions

The difference between equal and = in java:

The equal method is used to compare object references unless it is overwritten (such as String class and basic type package class. And ==except for comparing basic types, it is a comparison value. In other cases, it is a comparison reference address.

Java adapter mode?

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

Differences between abstract classes and interfaces

The difference between an abstract class and a common class is that an abstract class contains at least one abstract method and must be added to the Declaration.

An interface is a special abstract class with the following features: member variables: can only be constant member functions: can only be abstract constructor Methods: No relation and difference: Class and Class: inheritance, single inheritance class and interface: single implementation and multi-implementation (this is also a form of Multi-inheritance) interfaces and interfaces: single inheritance, multi-inheritance (this makes up for java's lack of multi-inheritance defects) The most important difference: Design Concept Interface Class: like a (with some specific functions and methods, for example, an animal implements an interface for running and eating. abstract class: is a (belongs to a certain category. For example, a cat belongs to a cat and must inherit from the abstract class of the cat, but cats are just a concept that has not been specifically implemented)

  Java multithreading:

1. thread security problem: cause: multiple threads share data to perform multiple statement operations on the shared data, which can be divided into two situations (1) multiple threads of the same type. For example, resources are shared for ticket sales in multiple windows. (2) multiple Threads of different types, such as naming a person or getting an age operator object, share resources but perform two operations: one is to get information, the other is to set parameters in the above two cases. To ensure thread security, you need to apply a lock to ensure security (1) lock the ticket selling operation (that is, add the synchronized (new object () Statement to the statement that operates on shared resources in the run method, the lock object in the lock object must also be the same for all threads.) (2) Lock the obtained information and set parameters, and ensure the same lock! In fact, the above two cases are essentially the same, that is, the only lock is required for operations on shared resources. 2. thread deadlock problem description: eg: thread 1 and thread 2 respectively have a lock and B lock. Now thread 1 wants to get B lock for B operation, at the same time, thread 2 also wants to get the lock and perform the operation, but the two locks are owned by the other. At this time, the two will think about the opposite lock but will not give up the lock, this causes a deadlock. In general, two or more processes are waiting for each other due to resource competition during execution. If there is no external force, they will not be able to proceed. Another explanation: the deadlock is caused by multi-thread access to shared resources. Due to improper access sequence, A thread usually locks A resource and wants to lock B; in another thread, resource B is locked, and resource A is locked to complete its own operations. Both threads want to get the resources of the other thread rather than release their own resources, this causes the two threads to be waiting and cannot be executed. 

Java Network Programming process:Udp Programming 

Sender:

1. Create the socket object constructor DatagramSocket () 2. create data and package byte [] bts = "helloworld ". getBytes () DatagramPacket dp = newDatagramPacket (bts, bts. getLength, InetAddress. getByName ("192.168.1.12"), 10086) 3. data transmission ds. sent (dp) 4. release ds. close () 

Acceptor:

1. Create the socket object initramsocket ds = new initramsocket (port number [> 1000]) 2. Create the package initrampacket dp = new initrampacket (bytes, bytes. length () // bytes is a byte array byte [], stores data 3, receives data ds. receive (dp) 4, parse the data and display or apply String ip = dp. getAddress (). getHostAddress (); // obtain the IP address String data = String (dp. getData (), 0, dp. getLength () // obtain the string 5 in the data packet and release the resource ds. close () tcp programming:

 Client

1. Create socket object 2, obtain the output stream, write data outputStream3, and release resources. 

Server

1. Create a ServerSocket object 2. Listen to the sokcet of the client and connect socket s = new ServerSocket (). accept (); 3. Obtain the input stream and read data. 4. release resources.

 


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.