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 do Java novice developers need to pay attention to

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what do novice developers of Java need to pay attention to". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "what novice developers of Java need to pay attention to" can help you solve the problem.

Java is a versatile programming language, and to some extent, it is used in almost all industries that may involve computers. The big advantage of Java comparison is that it runs in a Java virtual machine (JVM), a layer that translates Java code into bytecode compatible with the operating system. As long as there is JVM on your operating system, whether it is on a server (or "serverless", the same), desktop, laptop, mobile, or embedded device-then Java applications can run on it.

This makes Java a popular language for programmers and users. Programmers know that they only need to write a software version to end up with an application that can run on any platform; users know that applications can run on their computers, regardless of what operating system they are using.

Many languages and frameworks are cross-platform, but do not implement the same abstraction layer. With Java, you are targeting JVM, not the operating system. For programmers, this is the line with the least resistance when faced with some programming problems, but it is only useful when you know how to program Java. If you are new to learning Java programming, here are seven basic tips you need to know.

But first, if you're not sure if you have Java installed, you can find it in a terminal (such as Bash or PowerShell) by running:

$java-- version openjdk 12.0.2 2019-07-16 OpenJDK Runtime Environment 19.3 (build 12.0.2 / 9) OpenJDK 64-Bit Server VM 19.3 (build 12.0.2 / 9, mixed mode, sharing)

If you get an error, or nothing is returned, then you should install the Java Development Kit (JDK) to start Java development. Or, install a Java runtime environment (JRE) if you just need to run the Java application.

1. Java software package

In the Java language, related classes are grouped into a package. The Java base libraries you get when you download JDK will be grouped into packages that start with java or javax. Software packages provide a function similar to folders on a computer: they provide structures and definitions (in programming terms, namespaces) for related elements. Additional packages are available from independent developers, open source projects, and commercial vendors, just as libraries are available for any programming language.

When you write a Java program, you should declare a package name at the top of your code. If you just write a simple application to get started with Java, your package name can simply use your project name. If you are using a Java integrated development environment, such as Eclipse, when you start a new project, it generates a reasonable package name for you.

Package helloworld; / * @ author seth * An application written in Java. , /

In addition, you can determine your package name by looking for its path relative to your project as a whole. For example, if you are writing a set of classes to help with game development, and the collection is called jgamer, you may have some unique classes in it.

Package jgamer.avatar; / * @ author seth * An imaginary game library. , /

At the top of your package is jgamer, and within it each package is a separate derivative, such as jgamer.avatar, jgamer.score, and so on. In your filesystem, the directory structure reflects this. Jgamer is the top-level directory that contains the files avatar.java and score.java.

2. Import Java

As a multilingual programmer, it's fun to find out whether to use include, import, use, require, or some other term to introduce libraries you write no matter what programming language you use. In Java, by the way, use the import keyword when importing the required libraries for your code.

Package helloworld; import javax.swing.*; import java.awt.*; import java.awt.event.*; / * @ author seth * A GUI hello world. , /

The import is based on the Java path of the environment. If Java does not know where the Java library is stored on the system, it cannot be successfully imported. As long as a library is stored in the Java path of the system, the import can succeed, and the library can be used to build and run a Java application.

If a library is not in the Java path (because, for example, you are writing your own library), the library can be tied to your application (licensed by agreement) so that the import works as expected.

3. Java class

The Java class is declared with the keyword public class and a unique class name corresponding to its file name. For example, in a file called Hello.java in the project helloworld:

Package helloworld; import javax.swing.*; import java.awt.*; import java.awt.event.*; / * @ author seth * A GUI hello world. * / public class Hello {/ / this is an empty class}

You can declare variables and functions within a class. In Java, variables in a class are called fields.

4. Java method

The methods of Java are essentially functions in an object. Based on the data types expected to be returned (such as void, int, float, and so on), they are defined as public (meaning they can be accessed by any other class) or private (restricting their use).

Public void helloPrompt (ActionEvent event) {String salutation = "Hello% s"; string helloMessage = "World"; message = String.format (salutation, helloMessage); JOptionPane.showMessageDialog (this, message);} private int someNumber (x) {return xanth2;}

When a method is called directly, it is referenced by its class and method name. For example, Hello.someNumber points to the someNumber method in the Hello class.

5 、 static

The static keyword in Java enables members of your code to be accessed independently of the object that contains them.

In object-oriented programming, the code you write is used as a template for "objects" that are generated when the application is running. For example, instead of writing a specific window, you write a window instance based on the window class in Java (and modified by your code). Since none of the code you write will "exist" until the application generates its instance, most methods and variables (even nested classes) cannot be used until the objects on which they depend are created.

However, sometimes you need to access or use the data in an object before it can be created through the application. (for example, the application cannot generate a red ball unless it knows in advance that the ball is red.) For these cases, use the static keyword.

6. Try and catch

Java is good at catching errors, but it can recover gracefully only if you tell it what to do when it encounters an error. In Java, the cascading hierarchy that tries to perform an action begins with try, falls back to catch when an error occurs, and ends with finally. If the try clause fails, catch is called, and in the end, regardless of the result, it is always up to finally to perform some reasonable action. Here is an example:

Try {cmd = parser.parse (opt, args); if (cmd.hasOption ("help")) {HelpFormatter helper = new HelpFormatter (); helper.printHelp ("Hello", opt); System.exit (0) } else {if (cmd.hasOption ("shell") | | cmd.hasOption ("s") {String target = cmd.getOptionValue ("tgt");} / / else} / / fi} catch (ParseException err) {System.out.println (err); System.exit (1) } / / catch finally {new Hello () .helloWorld (opt);} / / finally} / / try

This is a robust system that tries to avoid irreparable errors or, at the very least, gives you the option of getting users to submit useful feedback. Use it frequently and your users will thank you!

7. Run Java applications

The Java file, which usually ends in .java, can theoretically be run using the java command. However, if an application is complex, whether running a single file will produce meaningful results is another question.

To run a .java file directly:

$java. / Hello.java

Typically, Java applications are distributed as Java archive (JAR) files, ending in .jar. An JAR file contains a manifest file (you can specify the main class, some metadata for the project structure), and all the code parts needed to run the application.

To run a JAR file, you can double-click its icon (depending on your operating system settings), or you can launch it from the terminal:

This is the end of $java-jar. / Hello.jar on "what new Java developers need to pay attention to". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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