Home  >  Article  >  Java  >  Java basics: Java class knowledge (1)

Java basics: Java class knowledge (1)

黄舟
黄舟Original
2017-02-06 11:21:481069browse

1

Analysis: Although you don’t know what cross-platform is, you can also use the Java language for programming, but for a Java programmer, understanding the cross-platform features can help you master the Java language more deeply, so in enterprises Candidates are often required to at least understand this feature.

Reference answer: Java’s cross-platform features are also called portability, platform independence, or write once and run everywhere. What he means is that if you write an application in Java language, it can run on different platforms without the need to develop it separately for different platforms. The reason why it can achieve cross-platform features. Mainly thanks to the Java Virtual Machine (JVM), the JVM interpreter interprets Java applications according to the current platform when running them, and interprets them into machine code that conforms to the current platform specifications, so the same application can be run on different platforms.

Please list the main features of the JAVA language 2

Analysis: To understand a language, one often starts by becoming familiar with the main features of the language. Therefore, companies often use candidates’ understanding of the JAVA language features. To judge whether the language foundation is solid based on the degree of mastery.

Reference answer: JAVA language has many features, mainly including:

① Cross-platform: An application can run directly on different platforms without modification.

 ②Object-oriented: JAVA language is an object-oriented language. You can use the properties and behaviors of objects, analyze and design using object-oriented ideas, and implement the entire application.

 ③When interpreting and executing a JAVA application, the interpreter in the JVM will interpret the class file and generate bytecode that conforms to the current platform.

 ④Automatic recycling: Garbage collection in JAVA applications is performed automatically. The background thread in the JVM will monitor the use of data in the memory. When the data in the memory is no longer referenced, it will be collected as garbage. , without requiring programmers to manually recycle.

Please explain the elements included in the JAVA class? 3 Please explain the main elements included in a JAVA class? And explain the role of each element.

Analysis: Whether it is a simple or complex JAVA application, it is composed of several classes, so a class is the component unit of a JAVA application. Understanding the main elements contained in a class can give you a clear understanding of the class. There are often five elements in a class, namely properties, methods, constructors, blocks and inner classes. In fact, blocks and inner classes are relatively rare.

Reference answer: JAVA classes mainly include attributes, methods, constructors, blocks and internal classes.

Properties are used to define the data of the object;

Methods are used to define the behavior of the object;

Constructors can be used to create objects;

Blocks can Used to perform operations when a class is loaded or to perform common operations before each instantiation;

The inner class exists as a member of the class and can access the properties and methods of the outer class.

Please explain the role and characteristics of the constructor method 4 Analysis: A class is a template for an object. To use a class, you often need to instantiate the class first, that is, create an object. To create an object, you must use the new keyword to call the constructor. The constructor often instantiates properties and can also implement other necessary functions, such as reading property files.

The constructor has unique characteristics: the name must be the same as the class name and is case-sensitive, and the constructor cannot declare a return value. These two characteristics are indispensable. When using the Eclipse tool, you can use the menu Generate Constructor to automatically generate different constructors.

Reference answer: The constructor method is used to create objects and is called using the new keyword. The name of the constructor must be the same as the name of the class and is case-sensitive. At the same time, the constructor cannot declare a return value type. It can use any access modifier, but cannot use other modifiers for modification, such as static, final, abstract, etc. Modifiers can modify constructors.

What is method overloading (overload)? 5 Analysis: If a certain behavior of a class has different algorithms and logic, for example, the method of calculating the absolute value of the Math class can both calculate the int type The absolute value of a numerical value can also be calculated. The absolute value of a double type value can also be calculated. In this case, if each type defines a different method name, such as abInt, absDouble, the readability of the class will be poor. When using it, you must be familiar with the method name corresponding to each type of parameter. However, if you use the same method name but use different formal parameters to distinguish these methods, it will be very readable, such as abs(int i), abs(double d), etc. Highly readable code is the basic requirement of enterprises for developers, and method overloading can make the code very readable.

Reference answer: Method overloading means that multiple methods with the same name can be declared in a class, and the formal parameters of the methods are different. When calling these methods with the same name, the JVM will bind to different methods based on the actual parameters.

Please list the permission access modifiers in Java 6

Please list the permission access modifiers in the Java language and explain the meaning of each permission.

Analysis: Correct use of permission access modifiers can effectively control the security of classes and class members. There are four access modifiers in the Java language, including public, same package, protected and private. Generally speaking, in applications Most classes have public permissions, most attributes have private permissions, and most methods have public permissions.

Reference answer: There are four permission access modifiers in the Java language, namely public, protected, same package and private. Public means public permissions, that is, it can be accessed in any package: protected means protected permissions, that is It can be accessed in the same package, and subclasses in the same package can access: The same package permission is the default permission, that is, when the specified permission modifier is not displayed, it is the same package permission, which means that only those in the same package can access: private is the private permission, which means Can only be accessed within this class.

Please list the basic data types in Java 7

Please list the eight basic data types in the Java language and explain the meaning and length of each data type.

Analysis: Data types are a very important knowledge point in any programming language. Properties, method parameters, and method return values ​​must specify their respective data types. Although the Java language is an object-oriented language. However, basic data types are also defined. Basic data types can be directly assigned without using the new keyword to create. Operators can be used directly for calculations. They are often used in programming, and the length of basic data types in the Java language is fixed. Will vary from platform to platform.

Reference answer: The eight basic data types of Java language are: byte type, 8-bit length: short integer type, 16-bit length: int integer type, 32-bit length: long long integer type, 64-bit length: float single-precision floating point type. 32-bit length: double double-precision floating point type, 64-bit length: char character type, 16-bit length: boolean type, representing a logical value, with two values ​​of true and false, representing true and false respectively.

What is a reference type 8 What is a reference type, what is the difference between a reference type and a basic data type?

Analysis: Many junior programmers can understand that the int in int i=20; is A type called an integer. But for the Employee in expressions like Employee e=new Employee();, I feel at a loss. In fact, any class is a type. For example, Employee is a type. It can be said that the type of variable e is Employee. In the Java language, the type of a class is called a reference type, that is, reference type.

It can be said that types other than the eight basic data types are reference types, including all classes in the API, all custom classes, and arrays. The intuitive difference between reference types and basic data types is that reference type variables need to use new to call the constructor to assign values, while basic data types can be assigned values ​​directly using the "=" sign. However, String in the reference type is special. You can use the new keyword to assign a value, or you can directly use "=" to assign a value. Under normal circumstances, you use "=" to assign a value directly.

Reference answer: Types in the Java language other than basic data types are called reference types. The reference type is the type of the class, and all objects are reference types, including array objects. Reference types must use new to call the constructor for assignment. Reference type variables have their own properties and methods, and you can use dots to call your own properties and methods. Basic data types directly use the = sign for assignment and do not have their own properties and methods. They are often used when declaring properties or methods.

For String object 9 For String object, you can use "=" to assign value, or you can use new keyword to assign value. What is the difference between the two methods?

Analysis: String type is the actual work Frequently used types can be divided into data types. String is a reference type and a class defined in the API. Therefore, String type objects can be created using new, for example, String name=new String("ETC"); assign a value to the variable name, and the value is "ETC". However, the String class is somewhat special compared to other classes. You can use "=" to assign values ​​directly, such as String name="ETC", which also assigns a value to the variable name, and the value is "ETC".

There is a difference between these two assignment methods. Using new assignment always creates a new object and initializes the value of the string in the new memory space; while using "=" assignment, it does not always create a new object. Each time, a new string is initialized, but a "string instance pool" is used to find whether there is a string to be assigned. If so, it is quoted directly; if it does not exist, a string is initialized and the "character" is added. String instance pool". In actual programming, "=" is often used to assign values ​​to String type variables.

Reference answer: Using "=" assignment does not necessarily create a new string every time, but looks for the string from the "string instance pool". Using new for assignment creates a new string each time.

String Class 10

The String class is an "immutable class". Please explain the meaning of "immutable class".

Analysis: The String class is an immutable class, that is, the immutable class. The so-called immutable means that after a string is initialized, its value will not be changed. For example, String s=new String("hello") will initialize a string with the value hello. If you call s.toUpperCase(), it will appear that hello will be changed to uppercase HELLO. However, in fact, it will not change the existing string. The hello becomes HELLO, but a HELLO string is initialized in the new space. It is precisely because of this immutability that the use of "string instance pool" can be supported.

Reference answer: The so-called immutable class means that after the string is initialized, it cannot be changed.

The above is the content of Java Basics: Java Class Knowledge (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn