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 debugging skills of Java programs?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what Java program debugging skills are, I hope you will gain something after reading this article, let's discuss it together!

One: a very simple method is to use a boolean quantity such as: debugMode. When you need to look at the output of the program, you can use: if (debugMode) System.out.println ("..")

The disadvantage of setting the debugMode variable to false; at the end of the project is that the code must be changed in order to start or disable debug

Second, a slightly better than the above method is to use Java-D: for example: Java-Ddebug=true MyClass, when using this method, you must add System.getProperty ("debug") to all your class to get the debug flag, so that the use of * method will be changed to:

Public static final boolean debug; static {String sDebug=System.getProperty ("debug"); if (sDebug.equalsIngnoreCase ("true")) debug=true; else debug= false;}

This way you can directly modify the Java program debugging flag without changing the source code, but it is not flexible enough, for example, you can not specify which part of the debugging function to use, but just to use the debugging function must be used in all the code, which leads to a third debugging method, which can define a class for debugging.

Public class DebugManager {public static final String SYSTEM_DEBUG_KEY= "system.Debug"; public static final boolean debug; static {debug = toBoolean (SYSTEM_DEBUG_KEY);} private static boolean toBoolean (String key) {boolean debug; String sDebug = System.getProperty (key); if (sDebug! = null & & sDebug.equalsIgnoreCase ("true") debug = true; else debug = false; return debug;} public static boolean getSystemDebug () {return debug } public static boolean getSystemDebug (String applicationKey) {if (debug | | toBoolean (applicationKey)) return true; else return false;}}

This allows you to call the dentition function for an application (Test):

Boolean debug = DebugManager.getSystemDebug (applicationKey)

To turn on the debugging function of Test, simply:

Java-DTest.Debug=true Test

Of course, this class can be further extended, such as changing it into log management, controlling the output location of debugging information, and so on.

Third, use assertions

Assertion is a new feature in Merlin (jdk1.4) that does not use assertions by default, but programmer can enable or disable this feature at any time, so that assertion verification can be enabled during testing and disabled when deployed, and re-enabled when the program runs if the user encounters a problem. Assertion check, which is not thrown when it fails

Exception, but throw AssertionError

1. Two grammatical forms of assertions:

(1) assert booleanExpression

(2) assert boolExpression:message; / / message will be displayed as the result of the error thrown

Assert must bit AssertTest in the executable fast. You cannot use assert with the class variable of the instance, but you can put it in any method.

2, enable assertions in Java program debugging:

Compilation: javac-source 1.4 AssertTest.java

Run: java-ea AssertTest (parameter)

-ea is an easy way to enable assertions, but-ea cannot be used with system classes

(1) to use assertions on a system class:-enablesystemassertions, or-esa

(2) to enable assertions on this package: add (:, the name of package,...) after-ea. If you enable assertions on the test.util package and all of its subpackages, you can java-ea:test.util... AssertTest

After reading this article, I believe you have a certain understanding of "what are the debugging skills of Java programs?" if you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

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