Analysis of problems encountered by Java

Source: Internet
Author: User

Here are some basic Java questions to explain. Where the blue part is the subjective view of Handsomecui

what are the parameters in synchronized (obj)?

The synchronized parameter represents an "object lock" . represents a different thread in the synchronized block,
At the same time, only one thread can execute the block of code, and the different instances of the class do not affect each other.
For example, person A = new person () and person B = new person (). Between A and B is not subject to synchronized restrictions.

Two. What are the different ways to create a thread?

There are three ways to create threads:

1. Inheriting the thread class

2. Implement the Runnable interface

3. The application can use the executor framework to create a thread pool

Three. What does the "static" keyword mean? Is it possible to overwrite (override) a private or static method in Java?

The "static" keyword indicates that a member variable or a member method can be accessed without an instance variable of the class to which it belongs.

The static method in Java cannot be overridden because method overrides are dynamically bound based on the runtime , while the static method is statically bound at compile time .

  The static method is not relevant to any instances of the class , so it is conceptually not applicable.

Four. Can I access non-static variables in the static environment?

  The static variable belongs to the class in Java, and it has the same value in all instances .

  static variables are initialized when the class is airborne into Java virtual .

If your code tries to access a non-static variable without an instance, the compiler will give an error because the variables have not been created yet and are not associated with any instances.

(Note that non-static variables are created after they are instantiated, and static variables are created when the class is created )

Five.the difference between int and integer:

int is the basic data type
  Integer is its wrapper class, note is a class.
  Why do you want to provide the packaging class???
1. In order to convert between the various types, call through various methods. Otherwise, you cannot convert directly from a variable. For example int x = integer.valueof ("66");

2. Generics List<integer> Nums;
Here <> need class, to use integer

Six. What is the difference between the three keywords in the Java intValue,parseint,Valueof ?

   int a = Integer.parseint ("123"); (equivalent to converting "123" to basic data)
Integer B = integer.valueof ("123"); (equivalent to converting "123" to an Integer object)
int c = B.intvalue (); (Converts the object representation of 123 to basic data 123)

That is integer.valueof ("123"). Intvalue () equivalent to Integer.parseint ("123")

Seven. Java does not support multiple inheritance, but it can implement multiple interfaces.

Eight. Synchronized is a coarse-grained lock used at the method level, and is fine-grained on a block of code.

Nine.what are the basic interfaces of the Java Collection Class framework?

    • Collection: Represents a set of objects, each of which is its child element.
    • Set: A collection that does not contain duplicate elements.
    • List: A sequence of collection, and can contain repeating elements.
    • Map: You can map a key (key) to an object with a value, and the key cannot be duplicated.

ArrayList and LinkedList implement the list interface, HashSet implements the set interface, which are more commonly used;

HashMap and Hashtable implement the Map interface, and Hashtable is thread-safe, but HashMap performance is better

  

java.util.Collection [I]

  |—java.util.List [I]    |—java.util.ArrayList [C]    |—java.util.LinkedList [C]    |—java.util.Vector [C]        |—java.util.Stack [C]  |—java.util.Set [I]    |—java.util.HashSet [C]    |—java.util.SortedSet [I]        |—java.util.TreeSet [C]

Java.util.Map [I]

  |—java.util.SortedMap [I]    |—java.util.TreeMap [C]  |—java.util.Hashtable [C]  |—java.util.HashMap [C]    

Analysis of problems encountered by Java

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.