Classic Java Basic Questions collection, welcome to the collection and sharing.
Question: What if the Main method is declared private?
Answer: Can compile normally, but when running will prompt "Main method is not public".
Question: What is the difference between passing references and passing values in Java?
Answer: Passing a reference refers to the address rather than the value itself, the value is a copy of the transfer value.
Question: What else should you consider if you want to rewrite the Equals method for an object?
Answer: Hashcode.
Question: How does Java "write once, run everywhere" be implemented?
The answer: Java programs are compiled into byte-code class files that can run on any platform, so Java is platform independent.
Question: Explain the role of each keyword in the declaration of public static void main (String args[])
Answer: The Public:main method is the first method invoked when the Java program is run, so it must be visible to the Java environment. So the visibility is set to Pulic.
The Static:java platform calls this method without creating an instance of the class, so this method must be declared static.
The Void:main method does not have a return value.
String is the type of the command line to pass the argument to, args refers to an array of strings that the command line passes in.
Question: The difference between equals and = =
Answer: = = Compare two objects in memory is not the same object, that is, the storage location in memory consistent. Two string objects store the same values, but it is possible to store them in different places in memory.
= = Comparison is the reference and the Equals method compares the content. public boolean equals (object obj) This method is provided by object objects and can be overridden by subclasses. The default implementation returns true only if the object is compared to itself, which is equivalent to = =. String, Bitset, Date, and file all override the Equals method, and for two string objects, equality of values means they contain the same sequence of characters. For a wrapper class of the base type, equality of values means the same value for the underlying type.
public class Equalstest {public
static void Main (string[] args) {
String S1 = "abc";
String s2 = S1;
String S5 = "abc";
String s3 = new String ("abc");
String S4 = new String ("abc");
System.out.println ("= = Comparison:" + (S1 = = S5));
System.out.println ("= = Comparison:" + (S1 = = s2));
System.out.println ("Using equals method:" + s1.equals (S2));
System.out.println ("= = Comparison:" + s3 = = S4);
System.out.println ("Using equals method:" + s3.equals (S4));
}
Results:
= = Comparison:true
= = Comparison:true
Using equals method:true
False
Using equals method:true
Question: What happens if you remove the static modifier for the main method?
Answer: The program can compile normally. The runtime throws Nosuchmethoderror exceptions.
Question: Why is Oracle Type4 drive called Thin drive?
Answer: Oracle provides a type 4 JDBC driver, known as thin drive. This driver contains a NET8 implementation of TCP/IP, which is fully implemented by Oracle itself, so it is platform-independent and can be downloaded at runtime by the browser and not dependent on any client Oracle implementations. The client connection string is the address port of TCP/IP, not the tnsname of the database name.
Question: Introduce Finalize method
Answer: Final: constant declaration. Finally: Handling exceptions. Finalize: Help with garbage collection.
The variables declared in the interface are final by default. The final class cannot inherit, that is, no subclasses. This is done for security considerations of the underlying type, such as String and Integer. This also allows the compiler to make some optimizations that make it easier to ensure thread security. The final method cannot be overridden. The value of the final variable cannot be changed. The Finalize () method is invoked before an object is destroyed and reclaimed. Finally, usually used for exception handling, whether or not an exception is thrown will be executed to. For example, closing a connection is usually done in a finally block.
Question: What is a Java API?
Answer: The Java API is a collection of software components that provide a number of useful features, such as GUI components.
Question: What is the GregorianCalendar class?
Answer: GregorianCalendar provides support for the traditional Western calendar.
Question: What is the ResourceBundle class?
Answer: ResourceBundle is used to store resources for the specified locale, and the application can load these resources based on the runtime's locale to provide presentation in different languages.
Question: Why are there no global variables in Java?
Answer: Global variables are globally visible, and Java does not support globally visible variables because: global variables break the principle of referential transparency. A global variable caused a namespace conflict.
Question: How do I convert a string type to a number type?
Answer: The valueof method of the integer class can convert a string to number. The following is a code example:
String numstring = "1000″;"
int id=integer.valueof (numstring). Intvalue ();
Question: What is the SimpleTimeZone class?
Answer: SimpleTimeZone provides Gregorian date support.
Question: What is the difference between a while loop and a Do loop?
Answer: The while structure at the beginning of the loop determines whether the next iteration should continue. The do/while structure at the end of the loop determines whether the next iteration will continue. The do structure performs at least one loop body at a time.
Question: What is the locale class?
Answer: The locale class is used to dynamically adjust the output of a program based on the locale.
Question: What is the principle of object-oriented programming?
Answer: There are mainly three points, polymorphism, inheritance and encapsulation.
Question: Introduction to the principles of inheritance
Answer: Inheritance allows an object to get the properties of another object. By using inheritance, you can reuse a fully tested feature, and you can modify it once, and all the inherited places take effect at the same time.
Question: What is implicit type conversion?
Answer: Implicit type conversion is simply a type assignment to another type, without explicitly telling the compiler that a transformation has occurred. Not all types support implicit type conversions.
code example:
int i = 1000;
Long j = i; Implicit casting
Question: is sizeof the Java keyword?
Answer: No.
Question: What is the native method?
Answer: The native method is a non-Java code implementation method.
Question: In System.out.println (), System, out, println, what is the difference?
Answer: System is the predefined final class provided by the systems, out is a PrintStream object, println is an overloaded method inside the Out object.
Question: encapsulation, inheritance, and polymorphism?
Answer: In simple terms, polymorphism refers to a variety of implementations of a name. Polymorphism enables an entity to implement different operations in a common way. The specific operation is determined by the actual implementation.
Polymorphism is represented in Java in three ways: Method overloading overrides the method through the Java interface by inheriting the implementation method.
Question: What is an explicit type conversion?
Answer: Explicit type conversion is the explicit telling of the compiler to do object conversion.
code example:
Long i = 700.20;
int j = (int) i; Explicit Casting
Question: What is a Java virtual machine?
Answer: Java virtual machines are software systems that can be ported to different hardware platforms.
Question: What is type down conversion?
Answer: Downward conversion is the conversion of a common type into a specific type, down on the inheritance structure.
Question: What is the Java access modifier?
Answer: The access modifier is a keyword that indicates the type of access permission for a class member. Use these keywords to qualify program methods or variable access rights. They contain:
Public: All classes can access the protected: private can be accessed within the same package and all subclasses: Only classes that belong can access the default: The Attribution class and subclasses under the same package are accessible
Question: What is the parent class for all classes?
Answer: Object.
Question: What are the basic types of Java?
Answer: Byte,char, short, int, long, float, double, Boolean.
Question: What are the characteristics of static types?
Answer: Static variables are bound to a class, not an instance object of a class. Each instance object shares the same static variable. That is, a class has only one static variable, no matter how many objects it has. A class variable, or static variable, is declared by using the keyword static. A class variable is usually used as a constant. Static variables are usually accessed through class names. When the program is running, the variable is created until the program is finished before it is destroyed. The scope of the class variable and the instance variable are the same. Its initial values and member variables are the same, and when the variable is not initialized, depending on its data type, there is a default value. Similarly, a static method is a method of a class, not a class object, and its invocation does not work on the class object, nor does it need to create any class instances. The static method is final in itself, because the override only occurs on the class instance, and the static method is bound to the class, not the object. Static methods of the parent class are masked by the static method of the class, as long as the original method is not declared final. Non-static methods cannot override static methods, that is, you cannot change a static method to an instance method in a subclass.
A non-static variable has a separate value on each instance of the object.
What is the difference between the question:& operator and the && operator?
Answer: When a & expression is evaluated, two operands will be evaluated,&& more like a shortcut to an operator. When a && expression evaluates, the first operand is evaluated, and the second operand is evaluated if it returns TRUE. If the first operand takes a value of Fale, the second operand is not evaluated.
Question: How does Java handle overflow and underflow of an integral type?
Answer: Java stores the corresponding Low-order bytes in the computed results to the corresponding values, depending on the size of the type.
Question: What if public static void is written as static public void?
Answer: The program compiles and runs normally.
Question, what is the difference between declaring a variable and defining a variable?
Answer: Declare a variable we provide only the type and name of the variable and are not initialized. The definition includes declaring and initializing two phases of string s; just a variable declaration, string s = new String ("Bob"); or string s = "Bob"; is the variable definition.
Question: What type of parameter delivery does Java support?
Answer: Java parameters are all passed values. For an object, the passed value is a reference to the object, which means that the original reference and the copy of the parameter reference point to the same object.
Question: What is the principle of object encapsulation?
Answer: Encapsulation is the code that binds data and manipulating data to a separate unit. This ensures the security of the data and prevents external code from being used incorrectly. Objects allow encapsulation of programs and data to reduce potential interference. Another understanding of encapsulation is to act as a protective layer of data and code to prevent arbitrary access to the code outside the protective layer.
Question: How do you understand variables?
Answer: A variable is a named area of memory for the program to access. Variables are used to store data, and the stored data may change as the program executes.
Question: What is the value enhancement?
The answer: Numeric elevation means that data is converted from a smaller data type to a larger data type for integer or floating-point operations. In the process of numeric elevation, the Byte,char,short value is converted to the int type. The int type may also be promoted to long when needed. Long and float are likely to be converted to double types.
Question: What is the type conversion of Java?
Answer: Converting from one data type to another is called type conversion. Java has two types of conversions, one is explicit type conversions and one is implicit.
Question: Inside the parameter of the main method, what is the first argument of the string array?
Answer: The array is empty and has no elements. Unlike C or C + +, the first element defaults to the program name. If the command line does not provide any arguments, the string array in the main method is empty, but not null.
Question: How do I determine if the array is null or empty?
Answer: Output array.length value, if 0, description array is empty. If it is null, a null pointer exception is thrown.
Question: Can a program allow multiple classes to have a main method at the same time?
Answer: Yes. When the program is running, we specify the class name to run. The JVM will only look for the main method in the class you specify. Therefore, there is no problem with naming conflicts for multiple classes owning the Main method.
Question: When does a static variable load? Compile or run period? What is the timing of the static code block loading?
Answer: Static variables are created when the class loader loads the class into the JVM, regardless of whether the object was created. When static variables are loaded, memory space is allocated. The code for a static code block is only executed once for the first time the class is initialized. A class can have more than one static code block, it is not a member of a class, has no return value, and cannot be called directly. Static code blocks cannot contain this or super, and they are usually initialized with static variables.
Question: Can a class have more than one main method?
Answer: Yes, but only one main method has the following signature:
public static void Main (string[] args) {}
Otherwise the program will not compile. The compiler warns you that the main method already exists.
Question: How does the JVM work under a simple description?
Answer: The JVM is an abstract computer, like a real computer, that will first compile the. java file into a. class file (A. class file is a bytecode file), and then use its interpreter to load the byte code.
Question: What if the value of two variables is exchanged in situ?
Answer: First, add two values to the first variable, then subtract the second variable with the resulting result and assign it to the second variable. Then subtract the second variable with the first variable, and assign the value to the first variable. The code is as follows:
int a=5,b=10;a=a+b; B=a-b; A=a-b;
The use of XOR or operation can also be exchanged. The first method may also cause an overflow. The method of XOR is as follows: int a=5,b=10;a=a+b; B=a-b; A=a-b;
int a = 5; int B = 10;
A = a ^ b;
b = a ^ b;
A = a ^ b;
Question: What is the encapsulation of the data?
Answer: One way to encapsulate data is to create set and get methods in the class to access the object's data variables. In general, variables are private, and get and set methods are public. Encapsulation can also be used to validate data when it is stored, to compute the data, or to use it as an introspection (such as using JavaBean in struts). The encapsulation of data and functionality into a separate structure is called data encapsulation. Encapsulation is the encapsulation of data and associated operations into a separate unit so that the associated methods are used to access the data. Encapsulation provides data security, which is actually a way of hiding data.
Question: What is the Reflection API? How is it implemented?
Answer: Reflection refers to the ability to view the state and characteristics of a class at run time and to perform dynamic management. These features are provided through some built-in reflection APIs such as Class,method,field, constructors, and so on. Example used: The GetName method using the Java Reflection API can get the class name.
Question: Does the JVM itself maintain caching, is it allocating objects in the heap, the operating system heap, or the heap that the JVM manages itself? Why?
Answer: Yes, the JVM itself manages caching, which creates objects in the heap and then references those objects in the stack.
Question: What is virtual memory?
Answer: Virtual memory is also called extended memory, in fact, there is no real physical memory.
Question: Can the method be static and synchronized at the same time?
Answer: Yes. If you do this, the JVM acquires a lock on the Java.lang.Class instance associated with this object. Doing this equals:
Synchronized (Xyz.class) {
}
Question: What is the difference between string and StringTokenizer?
Answer: StringTokenizer is a tool class for dividing strings.
StringTokenizer st = new StringTokenizer ("Hello World");
while (St.hasmoretokens ()) {
System.out.println (st.nexttoken ());
}
Output:
Hello
World
Question: What are the characteristics of the transient variable?
Answer: Transient variables are not serialized. For example, a class that implements the serializable interface is serialized to Objectstream, the transient type of variable is not written to the stream, and the value of the corresponding variable is null when the deserialization returns.
Question: Which containers use the border layout as their default layout?
Answer: Window, Frame, Dialog.
Question: How do you understand what synchronization is?
Answer: Synchronization is used to control the access of shared resources between multiple threads to ensure that only one thread can access the resource at the same time. When a thread is modifying a shared variable in an asynchronous-protected multithreaded program, another thread may be using or updating its value. Synchronization avoids the generation of dirty data.
To synchronize a method:
Public synchronized void Method1 () {
//appropriate method-related code.
}
To synchronize a block of code within a method:
Public MyFunction () {
synchronized (this) {
//synchronized code here.
}
}
The above is the Java interview problem of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!