Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics that Java must master?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the basics that Java must master". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the basics of Java must be mastered.

Catalogue

1. Anonymous class

two。 Multithreading

3. Synchronization

4. Serialization

Foreword:

Probably everyone has been using Java since they were students, and we have been learning Java, but there are always some concepts in Java that are ambiguous, both for beginner and advanced programmers. Therefore, the purpose of this article is to clarify these concepts.

After reading this article, you will have a deeper understanding of these concepts and be able to figure out all the gray things. In this book, we will discuss anonymous inline classes, multithreading, synchronization, and serialization.

1. Anonymous class

Java anonymous classes are much like local classes or inline classes, except that they don't have a name. We can use anonymous classes to define and instantiate a class at the same time. This should be done only if the local class is used only once.

Anonymous classes cannot have explicitly defined constructors. Instead, each anonymous class implicitly defines an anonymous constructor.

There are two ways to create an anonymous class:

Extend existing classes (either abstract or concrete)

Create an interface

The best way to understand the code is to read it first, so let's look at the code first.

Interface Football {void kick ();} class AnnonymousClass {public static Football football = new Football () {@ Override public void kick () {System.out.println ("Nested Anonymous Class.");}} Public static void main (String [] args) {/ / anomynous class inside the method Football footballObject = new Football () {@ Override public void kick () {System.out.println ("Anonymous Class");}; footballObject.kick (); AnnonymousClass.football.kick ();}}

Anonymous classes can be created in class and function code blocks. As you may know, anonymous classes can be created using interfaces or by extending abstract or concrete classes. In the above example, I first created an interface Football, and then implemented an anonymous class within the scope of the class and the main () method. Football can also be an abstract class or a top-level class juxtaposed with interface.

Football can be an abstract class, see the following code.

Public abstract class Football {abstract void kick ();}

Anonymous classes can be not only abstract classes, but also concrete classes.

/ / normal or concrete classpublic class Football {public void kick () {}} / / end of class scope.

What if the Football class does not have a constructor without arguments? Can we access class variables in anonymous classes? Do we need to overload all methods in anonymous classes?

/ / normal or concrete classpublic class Football {protected int score; public Football (int score) {this.score = score;} public void score () {System.out.println ("Score" + score);} Public void kick () {} public static void main (String [] args) {Football football = new Football (7) {@ Override public void score () {System.out.println ("Anonymous class inside the method" + score);}; football.score ();}} / / end of class scope.

You can use any constructor when creating anonymous classes. Note that the parameters of the constructor are also used here.

Anonymous classes can extend top-level classes and implement abstract classes or interfaces. Therefore, the rules of access control still apply. We can access the protected variable, but if we change it to private, we can't.

Because the Football class is extended in the above code, we don't need to overload all the methods. However, if it is an interface or abstract class, then you must provide an implementation for all unimplemented methods.

Static initialization methods or member interfaces cannot be defined in anonymous classes.

Anonymous classes can have static member variables, but they must be constant.

The purpose of anonymous classes:

Clearer project structure: usually we use anonymous classes when we need to change the implementation of some methods of a class at any time. This eliminates the need to add a new * .java file to the project to define the top-level class. This method is very useful, especially when the top-level class is only used once.

UI event listeners: the most common use of anonymous classes in graphical applications is to create various event handlers. For example, the following code:

Button.setOnClickListener (new View.OnClickListener () {public void onClick (View v) {/ / your handler code here}})

We created an anonymous class that implemented the setOnClickListener interface. Its onClick method is triggered when the user clicks the button.

two。 Multithreading

Multiple threads in Java can execute multiple threads at the same time. Threads are lightweight child processes and the smallest unit of processing. The main purpose of using multithreading is to maximize CPU utilization. We use multithreading instead of multiple processes because threads are lighter and can share memory space within the same process. Multithreading is used to implement multitasking.

Life cycle of a thread

As shown in the figure above, the life cycle of a thread has five main states. Let's explain each state in turn.

New: after creating an instance of a thread, it enters the new state, which is the first state, but the thread is not ready to run.

Runanble: call the start () method of the thread class, and the state changes from new to Runnable, meaning that the thread can run, but when it actually starts running depends on the Java thread scheduler, because the scheduler may be busy executing other threads. The thread scheduler picks a thread from the thread pool in a FIFO (first-in, first-out) fashion.

Blocked: there are many situations that can cause a thread to become in a blocked state, such as waiting for an Icano operation, waiting for a network connection, and so on. In addition, a higher priority thread can change the currently running thread into a blocked state.

Waiting: a thread can call wait () to enter the waiting state. When another thread calls notify (), it returns to the runnable state.

When the Terminated:start () method exits, the thread enters the terminated state.

Why use multithreading?

Using threads allows Java applications to do more than one thing at the same time, thus speeding up the execution. In technical terms, threads can help you implement parallel operations in Java programs. Because modern CPU is very fast and may contain multiple cores, it is impossible for a single thread to use all cores.

Key points to keep in mind

Multithreading can make better use of CPU.

Improve responsiveness and user experience

Reduce response time

Provide services to multiple clients at the same time

There are two main ways to create threads:

Extend the Thread class

Implement the Runnable interface

Create threads by extending the Thread class

Create a class that extends the Thread class. This class should overload the run () method in the Thread class. The thread begins its life cycle in the run () method. We create an object of the new class, and then call the start () method to start executing the thread. In the Thread object, start () calls run ().

Public class MultithreadingTest extends Thread {public void run () {try {System.out.println ("Thread" + Thread.currentThread (). GetName () + "is now running");} catch (Exception ex) {ex.printStackTrace ();}} public static void main (String [] args) {for (int iTuno)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report