In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the basic knowledge of Java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The difference between object-oriented and process-oriented
First of all, there is no specific difference in performance between process-oriented and object-oriented languages, which should be referred to according to the design of each language. Personally, I think the biggest difference between process-oriented and object-oriented is that process-oriented language is structured and object-oriented language is modular. Modular code is easier to maintain, reuse and extend than structured code.
The difference between OracleJDK and OpenJDK
OpenJDK is based on the source code of HotSpot donated by Sun and is open source. OracleJDK is a commercial version of Oracle to JDK, released and maintained by Oracle, so OracleJDK is more reliable than OpenJDK.
Similarities and differences between Java and C++
Java and C++ are based on object-oriented languages.
Java does not provide pointers to access memory, C++ runs pointers to access memory.
Garbage collection mechanism. Java does not require developers to manually release memory, because Java automatically reclaims memory by garbage collection mechanism, while C++ requires developers to release memory manually. Therefore, Java is more secure than C++ in memory management.
Java does not support multiple inheritance, while C++ does.
The difference between JVM, JDK and JRE
JVM: (java virtual machine) is the java virtual machine
JRE: (java runtime environment) is the java runtime environment
JDK: (java development kit) is a java development kit that includes not only jre and jvm, but also other development tools such as javac compilers and javadoc
The characteristics of Java language
object-oriented
Platform independence, that is, cross-platform (relying on JVM)
Garbage collection mechanism (GC)
Support for multithreading
Support convenient network programming
Compile and interpret (JIT)
Safety
Object-oriented features
There are three characteristics of object-oriented: encapsulation, inheritance and polymorphism.
Encapsulation: encapsulation is a technology that hides object properties and implementation details and provides only interfaces that can be accessed or modified. The purpose of encapsulation is to simplify programming and increase the security of the program, so that users do not need to know the specific implementation details of the object.
Inheritance: inheritance is a technique for defining new classes on existing classes. In Java, an existing class is called a base class (parent class) and a new class is called a derived class (subclass). The subclass has all the properties and methods of the parent class. However, subclasses do not own private methods or properties in the parent class and cannot access and use them. The main purpose of inheritance is to reuse zero code.
Polymorphism: polymorphism refers to objects of the same type, calling the same methods and parameters, but in the form of different results. The purpose of polymorphism is for the scalability and maintainability of the program. Inheritance and interface 2 features can be used to implement polymorphism in java.
The difference between overloading and rewriting
Personally, I think reloading and rewriting are not comparable at all. I don't know why people always like to take notes with pictures.
Overloading: overloading describes multiple methods with the same method name in a class, but their parameters, types, return values, and parameter order may be different.
Rewriting: rewriting describes that the subclass rewrites the logic of a method of the parent class, but only the content of the method is rewritten, and the method name, parameters, type, order, and return values remain the same.
The difference between interfaces and abstract classes
Interfaces need to be implemented, while abstract classes need to be inherited.
Methods in interfaces are publicly abstract, while abstract classes allow abstract and non-abstract methods (in JDK8, interfaces are allowed to define defalut methods, and in JDK9, private private methods are also allowed).
A class allows multiple interfaces to be implemented, but only one abstract parent class is allowed to inherit.
The interface is the specification of the class, which standardizes the behavior ability. The abstract class is the abstraction of the class, and the abstract is logic.
What are the Object class methods?
GetClass
Equals
HashCode
ToString
Wait
Wait (long): puts the current object into the TIMED_WATING state
Wait (long,int): puts the current object into the TIMED_WATING state
Notify
NotifyAll
Clone
Finalize
The difference between static property methods and member property methods
Static properties and methods belong to class Class, while member properties and methods belong to instantiated objects.
Static methods can only use static methods and static properties, not member properties and methods. Because static properties and methods exist when the object is not instantiated.
The simple understanding is that a thing that already exists is not allowed to use a thing that does not exist.
Initial good order of subclass attributes and parent class attributes
In any case, static data loads first, so initialize parent static variables and static initial blocks (static variables and static initial blocks are executed in the order in which the source code is written, as well as normal initialization blocks and ordinary member variables). Then initialize subclass static variables and static initialization blocks.
Normal initialization blocks and ordinary member variables take precedence over constructors, so the normal initialization blocks and ordinary member variables of the parent class are then loaded, and then the parent class constructor is called.
Call subclass common code blocks, common variables, and constructors.
Automatic unpacking and packing
Automatic unpacking and boxing is actually a syntactic sugar of the Java compiler.
Automatic boxing refers to the process of transforming the basic data type into the corresponding wrapper class object.
Automatic unpacking refers to changing the packaging type to the corresponding basic data type.
Autoboxing is actually calling the valueof method of the class wrapper class object, such as: Integer.valueof (1)
Automatic unpacking is actually a call to the xxxValue method of the wrapper class, such as: Integer.intValue ()
During automatic boxing, if the wrapper class allows caching and the value is within the scope of the cache, the boxed-produced objects are cached in the constant pool.
The Integer, Byte, Short, Long, and Character wrapper types have cache pools, while the other three: Float, Double, and Boolean do not.
The cache pool cache range of the wrapper class is basically between-128Mui-127except that the cache range of Character is 0Murray 127.
Why is String not changeable?
First of all, let me talk about my opinion: String is one of the most commonly used classes in Java, and if String is variable, then a very large number of ambiguous problems will occur. Such as IP address, person's name, mailbox a lot of sensitive data. If String is changeable, security problems can occur, and string constant pools are out of the question.
String is immutable, so it is also thread-safe in nature. The disadvantage of immutable classes is that each different value needs to create an object.
String is decorated with final to ensure that the class String class cannot be extended. The fields inside the String are decorated with final, and there is no external method to modify the fields. That's why String is immutable.
The role of final keyword
Classes modified by final cannot be inherited, and all member methods of this class are final and cannot be overridden.
Attribute variables modified by final cannot be modified. If the variable is a basic data type, its value cannot be modified after initialization. If the variable is a reference type, the reference can no longer point to another object.
Methods modified by final cannot be overridden by subclasses.
The difference between StringBuilder and StringBuffer
Both StringBuilder and StringBuffer are mutable strings, but StringBuilder threads are not safe.
StringBuffer is thread-safe. Therefore, consider using StringBuilder in the case of single thread and StringBuffer in multithreading.
The relationship between them is like the relationship between HashMap and HashTable.
Equals knowledge points
= = different from equals
= =, if it is a basic data type, it compares the value; if it is a reference data type, it compares the memory address of the object; equals compares the value of the object. So use equals to compare whether the values of two objects are equal in java, determine that two objects are released as one object, and use = =.
The hashCode method returns:
Equals method rewriting requirements
Reflexivity: x.equals (x) = = true holds forever
Non-emptiness: x.equals (null) = = false is always established
Symmetry: if x.equals (y) = = true, then y.equals (x) = = true
Transitivity: if x.equals (y) = = tue and y.equals (z) = = true, then x.equals (z) = = true must be satisfied.
Consistency: if x.equals (y) = = true, then x.equals (y) = = true is always true as long as the values of x and y remain the same
Why do you have to override the equals method when you rewrite the hashcode method?
In a normal environment (not involving the hash table), there is no dime relationship between the equals method and the hashcode method, so it is okay to rewrite the equals but not the hashcode. However, when using hash tables such as map and set, they calculate the position of the object in the hash table based on the object's hashcode. Imagine if the values of two objects are equal, but because they are two objects, the hashcode is not equal. Then even if you put in map,set (map), there will still be duplicate data.
Deep copy and shallow copy
Deep copy: copy everything except the basic data type variable and even the reference type variable.
Shallow copy: copy the basic data type variable, and for the variable of the reference type, return the reference itself directly.
IO flow classification
According to the flow direction, it is divided into: input stream and output stream.
According to the operation unit, it is divided into byte stream and character stream.
Use byte stream or character stream?
For generality, byte streams should be used. If it's just a text file operation, you can use a character stream.
BigDecimal
The type that represents a large floating point number in Java when BigDecimal.
In Java, if you encounter a floating-point judgment, you can use BigDecimal to do the calculation, because the loss of precision is likely to occur if you use a normal data type, and the results may be unexpected.
Java exception architecture
In Java, exceptions are divided into Exception and Error, both of which inherit from Throwable.
Exception: Exception exceptions can be handled by the program itself. Exception is divided into runtime exceptions (RuntimeException) and non-runtime exceptions (CheckedException).
RuntimeException: RuntimeException (runtime exceptions) are exceptions that can occur while the program is running, such as NullPointException, which are often unpredictable, and the compiler does not require you to manually try catch or throws.
CheckedException: CheckedException (non-runtime exception) is an exception other than RuntimeException, such as IOException, which requires a try catch or throws that must be displayed, and if not handled, the compilation will not pass.
Error: Error errors cannot be handled by the program, indicating that there is a serious and unsolvable problem with the program or JVM.
Comparable and Comparator
Comparable: natural sorting interface. The class that implements it means that sorting is supported.
Comparator: external comparator. Instead of having the objects that need to be sorted implement sorting logic, they are sorted according to the logic defined by Comparator. Comparator is more flexible than Comparable.
Why should Arrays.asList () be used cautiously?
Because the Arrays.asList method returns not the ArrayList we expect, but the ArrayList implemented inside the Arrays class. This inner class only supports access and set operations, but does not support modification operations such as remove,add,clear.
Types referenced in Java
There are four reference types in Java: strong reference, soft reference, weak reference, and virtual reference.
Strong Reference: the vast majority of Java programs are strong references, and objects created using the new keyword are generally strong references. As long as strong references exist, strongly referenced objects will not be recycled unless they are unreachable (see the jvm section)
Soft references (Soft Reference): soft references are generally not recycled, but soft reference objects are recycled when there is not enough memory in the heap, such as when OOM is almost happening.
Weak references (Weak Reference): weakly referenced objects are recycled as soon as garbage collection starts.
Phantom Reference (also known as ghost reference): unlike several other references, a virtual reference does not determine the lifecycle of an object and can be recycled at any time.
This is the end of the content of "what are the basics of Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.