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 language features of Jython?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares with you is about the language characteristics of Jython. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Jython is released as a single Java class file that contains the installer. Just download jython-21.class and put the file in CLASSPATH, and then run java jython-21. Select the components you want to install (in the example, I selected all the default components), accept its license (this is the open source BeOpen/CNRI license), and specify the installation directory, after which the installer will complete the rest of the installation.

If you encounter problems with installation, refer to the installation information page on the Jython website. For UNIX platforms, you may want to add the selected Jython installation path to the PATH environment variable. As a guide to getting started with Jython, you can now run interactive PATH simply by typing "jython":

Listing 1. Run Jython shell

Jython Jython 2.1on java1.4.0_01 (JIT: null) Type "copyright", "credits" or "license" for more information. > > >

The prompt allows you to enter commands and get results immediately. In Java programming, each program must define at least one class. Listing 2 demonstrates a complete Java program that writes a message to the screen:

Listing 2. Complete Java program

Class App {public static void main (String args []) {System.out.println ("I don't like spam!");}}

JPython reduces these lines to:

Listing 3. Jython reduces Java code overhead

> print "I don't like spam!" I don't like spam! >

The print keyword is one of the most important tools, especially in interactive shell, which prints its parameters immediately and then returns a shell prompt to you. Not only do you enter and debug less code, but you don't have to go through a compilation / run cycle to get results right away. You can print more than one value at a time and use simple expressions as easily as shown below:

Listing 4. Print is an important Jython tool

> print "one plus one is", 1x 1 one plus one is 2 >

Jython expressions are similar to Java expressions. The result of 1x1 is an integer that is cast to a string through print and is juxtaposed to the initial string specified by the comma delimiter.

By using Jython, you don't even need any tools to access the standard Java library. The following example accesses java.util.Random:

Listing 5. Access the standard Java library through Jython

> from java.util import Random > rng = Random () > I = rng.nextBoolean () > print i1 >

Jython's import keyword is similar to the Java language version in that it makes the content in one module available to other modules, but the syntax and behavior are different.

The example in listing 5 above uses the relevant from keyword to limit which symbols are imported from java.util. The next line shows the creation of an instance of the Random class. As you can see, the new keyword is not required.

There is also no need for any type description of the variable that holds the new instance. This highlights an important simplification of Jython, and it's an advantage of its dynamic nature-you don't have to worry about data type definitions anymore.

The next line in listing 5 demonstrates the method call, which is exactly the same as the Java language, except that there is no type declaration for the result. NextBoolean () in the Java code is a Boolean value. Jython 2.1 does not have a Boolean type (although this may change quickly; Python 2.3 adds a Boolean type), so it is replaced with an integer such as 0 or 1. Similarly, to call the Java method that you want to use Boolean values, you pass an integer value that satisfies these constraints.

You can also use the import keyword to fully qualify all imported symbols, as shown in listing 6:

Listing 6. Import fully qualifies all imported symbol names

> import java.util.Random > rng = java.util.Random () > print rng.nextFloat () 0.9567907452583313 > >

The floating-point values of Jython are exactly the same as those in the Java language.

These are the language features of Jython, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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