Tutorial:BeanShell/2 Arithmetic

From HandWiki


In BeanShell you also have the option of working with "loosely typed" variables. That is, you can simply omit the types of variables that you use (both primitives and objects). Now, let's try to use BeanShell as a calculator. We will calculate 3*8/6:

<jc lang="bsh"> print(3*8/6); </jc>

Numbers and Math

You can also do some calculations using elementary functions. Math functions are imported by BeanShell automatically and you will need only to call the package name Math. Let's try to use BeanShell as a calculator. We will calculate $\sqrt(100))$:

<jc lang="bsh"> print(Math.sqrt(100)); </jc>

We have added the "Math" package name which is imported automatically by BeanShell. Let us consider a logarithmic function:

<jc lang="bsh"> print(Math.log(100)); </jc>

Default Imports

In the previous example, we called the "Math" package, which belongs to Java java.lang package. This package is imported automatically. By default, common Java core and extension packages are imported for you. They are, in the order in which they are imported:

  • javax.swing.event
  • javax.swing
  • java.awt.event
  • java.awt
  • java.net
  • java.util
  • java.io
  • java.lang

Exercises:

  1. Calculate logarithm of 10