[ Previous Section | Chapter Index | Main Index ]

Section 2.6

Programming Environments


Although the Java language is highly standardized, the procedures for creating, compiling, and editing Java programs vary widely from one programming environment to another. There are two basic approaches: a command line environment, where the user types commands and the computer responds, and an integrated development environment (IDE), where the user uses the keyboard and mouse to interact with a graphical user interface. While there is essentially just one command line environment for Java programming, there are several common IDEs, including Eclipse, IntelliJ IDEA, and BlueJ. I cannot give complete or definitive information on Java programming environments in this section, but I will try to give enough information to let you compile and run the examples from this textbook using the command line, Eclipse, or BlueJ. (Readers are strongly encouraged to read, compile, and run the examples. Source code for sample programs and solutions to end-of-chapter exercises can be downloaded from the book's web page, https://math.hws.edu/javanotes-swing.)

One thing to keep in mind is that you do not have to pay any money to do Java programming (aside from buying a computer, of course). Everything that you need can be downloaded for free on the Internet.

This textbook is meant for use with Java 17 or later, although the large majority of it it is valid for versions as old as Java 8. In this section, I will try to give you enough information to make it possible to install Java and use it with this textbook. Since Java 8, new versions of Java are released much more frequently than in the past, about twice a year, but only some of the releases are "long-term support" (LTS) releases that will continue to receive bug fixes and security updates over an extended period of time. Java 8, Java 11, and Java 17 are long-term support releases. I recommend that you use Java 17, or Java 11 if you already have that installed, and avoid non-long-term-support releases unless you have specific need for some feature that they offer.


2.6.1  Getting a JDK

The basic development system for Java programming is usually referred to as a JDK (Java Development Kit). Note that Java comes in two versions: a Development Kit version (the JDK) and a Runtime Environment version (the JRE). A Runtime Environment can be used to run Java programs, but it does not allow you to compile your own Java programs. A Development Kit includes the Runtime Environment but also lets you compile programs. (It has become harder find a separate JRE download, but you will still see the term used — sometimes to refer, in fact, to a JDK.) A JDK will include the command line environment that you need to work with Java on the command line. If you decide to use an IDE, you might still need to download a JDK first; note, however, that both the Eclipse IDE and BlueJ now include a JDK, so you do not need to download a separate JDK to use them.

Java was developed by Sun Microsystems, Inc., which was acquired by the Oracle corporation. It is possible to download a JDK directly from Oracle's web site, but starting with Java 11, the Oracle JDK is meant mostly for commercial use. For personal and educational use, it is probably preferable to use OpenJDK, which has the same functionality as the version available from Oracle and is distributed under a fully free, open-source license. Although OpenJDK can be downloaded from https://jdk.java.net/, which is also owned by Oracle, I recommend downloading from Adoptium at this address:

https://adoptium.net/

This site is run by the Eclipse Foundation, and it distributes a version of the OpenJDk under the name "Eclipse Temurin." You will want to download Temurin 17 or later. The JDK comes in different versions, depending on the operating system and type of CPU for your computer, but the Adoptium web page should detect your operating system and CPU, so that you just need to click the "Latest release" download button to get the appropriate version. (If you need to select the version yourself, note that the CPU type is referred to as "Architecture." You are most likely to be using the "x64" architecture, which works for Intel and AMD CPUs, but the newer M1 Macs use "aarch64," which refers to ARM CPUs.)

The Adoptium site provides installers for MacOS and Windows that make it easier to set up Java on those platforms. (The installer for MacOS is a .pkg file, and the installer for Windows is a .msi file.) If you use the Linux operating system, you can probably install an OpenJDK using the usual software manager for your distribution; if not, you can download a version from Adoptium, but it will just be a compressed archive rather than an installer.

If you download a JDK installer for Windows or MacOS from Adoptium, you can just double-click the installer file to start the installation, if it does not start automatically. If you use the default installation, the installer will set up your computer so that you can use the javac and java commands on the command line.

(An OpenJDK can also be downloaded as a compressed archive, which you can decompress and place anywhere on your computer. However, to use the javac and java commands, you will either need to put the bin directory from the OpenJDK directory on your PATH environment variable, or use full path names for the javac and java commands. The Adoptium installers for Windows and for MacOS will take care of this detail for you.)


2.6.2  Command Line Environment

Many modern computer users find the command line environment to be pretty alien and unintuitive. It is certainly very different from the graphical user interfaces that most people are used to. However, it takes only a little practice to learn the basics of the command line environment and to become productive using it. It is useful to know how to use the command line, and it is particularly important for computer science students, but you can skip this subsection if you plan to do all of your work with Java in an IDE.

To use a command line programming environment, you will have to open a window where you can type commands. In Windows, you can open such a command window by running a program named cmd. In MacOS, you want to run the Terminal program, which can be found in the Utilities folder inside the Applications folder. In Linux, there are several possibilities, including a very old program called xterm; but try looking for "Terminal" in your Applications menu.

No matter what type of computer you are using, when you open a command window, it will display a prompt of some sort. Type in a command at the prompt and press return. The computer will carry out the command, displaying any output in the command window, and will then redisplay the prompt so that you can type another command. One of the central concepts in the command line environment is the current directory or working directory, which contains files that can be used by the commands that you type. (The words "directory" and "folder" mean the same thing.) Often, the name of the current directory is part of the command prompt. You can get a list of the files in the current directory by typing in the command dir (on Windows) or ls (on Linux and MacOS). When the window first opens, the current directory is your home directory, where your personal files are stored. You can change the current directory using the cd command with the name of the directory that you want to use. For example, if the current directory is your home directory, then you can change into your Desktop directory by typing the command cd Desktop (and then pressing return).

You might want to create a directory (that is, a folder) to hold your Java work. For example, you might create a directory named javawork in your home directory. You can do this using your computer's GUI; another way is to use the command line: Open a command window. If you want to put your work directory in a different folder from your home directory, cd into the directory where you want to put it. Then enter the command mkdir javawork to make the directory. When you want to work on programming, open a command window and use the cd command to change into your Java work directory. Of course, you can have more than one working directory for your Java work; you can organize your files any way you like.


The most basic commands for using Java on the command line are javac and java. The javac command is used to compile Java source code, and java is used to run Java programs. These commands, and other commands for working with Java, can be found in a directory named bin inside the directory that holds the JDK. If you set things up correctly on your computer, it should recognize these commands when you type them on the command line. Try typing the commands java -version and javac -version. The output from these commands should tell you which version of Java is being used. If you get a message such as "Command not found," then Java is not correctly configured.

Java should already be configured correctly on Linux, if you have installed Java from the Linux software repositories. The same is true on MacOS and Windows, if you have used an installer from Adoptium.


To test the javac command, place a copy of HelloWorld.java into your working directory. (If you downloaded the Web site of this book, you can find it in the directory named chapter2 inside the directory named source; you can use your computer's GUI to copy-and-paste this file into your working directory, or you could cd into the chapter2 folder and work there. Alternatively, you can navigate to HelloWorld.java on the book's Web site and use the "Save As" command in your Web browser to save a copy of the file into your working directory.) Type the command (while working in the directory that contains the file HelloWorld.java):

javac  HelloWorld.java

This will compile HelloWorld.java and will create a bytecode file named HelloWorld.class in the same directory. Note that if the command succeeds, you will not get any response from the computer; it will just redisplay the command prompt to tell you it's ready for another command. You will then be able to run the program using the java command:

java  HelloWorld

The computer should respond by outputting the message "Hello World!". Note that although the compiled program is stored in a file named HelloWorld.class, the java command uses the name of the class, HelloWorld, not the name of the file. To run the program, you only need .class file, not the .java file.

Many of the sample programs for this book use TextIO to read input from the user (see Subsection 2.4.3). Since TextIO is not a standard part of Java, you must make it available to any program that uses it. This means that your working directory should contain a folder named textio, and inside that folder should be the file TextIO.java. You can copy TextIO.java from this book's source directory, or your can download it from the web site, but you should be sure to place it inside a folder named textio in the same directory as the program that uses TextIO.

Once you have TextIO.java you can run a sample program such as Interest2.java to test user input. First, compile the program with the command

javac  Interest2.java

If successful, this will create the compiled file named Interest2.class. But you will also notice that it creates the file TextIO.class inside the textio folder, if that file does not already exist. More generally, the javac command will compile not just the file that you specify but also any additional Java files that are needed. Once you have Interest2.class, you can run it using the command

java  Interest2

You will be asked to enter some information, and you will respond by typing your answers into the command window, pressing return at the end of each line. When the program ends, you will see the command prompt, and you can enter another command. (Note, by the way, that "java TextIO" would not make sense, since TextIO does not have a main() routine, and so it is not possible to execute it as a program.)

You can follow a similar procedure to run all of the examples in this book.


To create your own programs, you will need a text editor. A text editor is a computer program that allows you to create and save documents that contain plain text. It is important that the documents be saved as plain text, that is without any special encoding or formatting information. Word processor documents are not appropriate, unless you can get your word processor to save as plain text. A good text editor can make programming a lot more pleasant. Linux comes with several text editors. On Windows, you can use notepad in a pinch, but you will probably want something better. For MacOS, you might download the BBEdit application, which can be used for free. One possibility that will work on any platform is to use jedit, a programmer's text editor that is itself written in Java and that can be downloaded for free from www.jedit.org. Another popular cross-platform programming editor is Atom, available from atom.io.

To work on your programs, you can open a command line window and cd into the working directory where you will store your source code files. Start up your text editor program, such as by double-clicking its icon or selecting it from a Start menu. Type your code into the editor window, or open an existing source code file that you want to modify. Save the file into your working directory. Remember that the name of a Java source code file must end in ".java", and the rest of the file name must match the name of the class that is defined in the file. Once the file is saved in your working directory, go to the command window and use the javac command to compile it, as discussed above. If there are syntax errors in the code, they will be listed in the command window. Each error message contains the line number in the file where the computer found the error. Go back to the editor and try to fix one or more errors, save your changes, and then try the javac command again. (It's usually a good idea to just work on the first few errors; sometimes fixing those will make other errors go away.) Remember that when the javac command finally succeeds, you will get no message at all, or possibly just some "warnings"; warnings do not stop a program from running. Then you can use the java command to run your program, as described above. Once you've compiled the program, you can run it as many times as you like without recompiling it.

That's really all there is to it: Keep both editor and command-line window open. Edit, save, and compile until you have eliminated all the syntax errors. (Always remember to save the file before compiling it—the compiler only sees the saved file, not the version in the editor window.) When you run the program, you might find that it has semantic errors that cause it to run incorrectly. In that case, you have to go back to the edit/save/compile loop to try to find and fix the problem.


2.6.3  Eclipse IDE

In an Integrated Development Environment, everything you need to create, compile, and run programs is integrated into a single package, with a graphical user interface that will be familiar to most computer users. There are a number of different IDEs for Java program development, ranging from fairly simple wrappers around the JDK to highly complex applications with a multitude of features. For a beginning programmer, there is a danger in using an IDE, since the difficulty of learning to use the IDE, on top of the difficulty of learning to program, can be daunting. However, for my own programming, I generally use the Eclipse IDE, and I introduce my students to it after they have had some experience with the command line. I will discuss Eclipse in some detail and a much simpler alternative, BlueJ, more briefly. IDEs have features that are very useful even for a beginning programmer, although a beginner will want to ignore many of their advanced features.

You can download an Eclipse IDE from eclipse.org. When I install Eclipse, I get the "Eclipse IDE for Java Developers" package (not the "installer") from this web page:

https://www.eclipse.org/downloads/packages/

For Windows and Linux, the package download is a compressed archive file. You can simply extract the contents of the archive and place the resulting directory wherever you want it on your computer. You will find the Eclipse application in that directory, and you can start Eclipse by double-clicking the application icon. For MacOS, the download is a .dmg file that contains the Eclipse application. You can open the .dmg file and drag the application to any location that you prefer (probably the Applications folder). Note that the MacOS and Linux packages are available in two versions, for x86_64 and AArch64. This refers to the type of CPU that the computer uses. Newer "M1" Macs can use the AArch64 version; the x86_64 version is for older Macs that use Intel CPUs. For Linux, you are most likely to need the x86_64 version, which will work for Intel and AMD CPUs.

Eclipse is a free program. It is itself written in Java. Recent versions of Eclipse include an OpenJDK (although Eclipse calls it a JRE), so you can use it without downloading a separate JDK. The March 2022 version includes a Java 17 SDK.

The first time you start Eclipse, you will be asked to specify a workspace, which is the directory where your work will be stored. You can accept the default name, or provide one of your own. You can use multiple workspaces and select the one that you want to use at startup. When a new workspace is first opened, the Eclipse window will be filled by a large "Welcome" screen that includes links to extensive documentation and tutorials. You should close this screen, by clicking the "X" next to the word "Welcome"; you can get back to it later by choosing "Welcome" from the "Help" menu.

The Eclipse GUI consists of one large window that is divided into several sections. Each section contains one or more views. For example, a view can be a text editor, it can be a place where a program can do I/O, or it can contain a list of your projects. If there are several views in one section of the window, then there will be tabs at the top of the section to select the view that is displayed in that section. This will happen, for example, if you have several editor views open at the same time.

Each view displays a different type of information. The whole set of views in the window is called a perspective. Eclipse uses different perspectives, that is, different sets of views of different types of information, for different tasks. For compiling and running programs, the only perspective that you will need is the "Java Perspective," which is the default. As you become more experienced, you might want to use the "Debug Perspective," which has features designed to help you find semantic errors in programs. There are small buttons in the Eclipse toolbar that can be used to switch between perspectives.

The Java Perspective includes a large area in the center of the window that contains text editor views. This is where you will create and edit your programs. To the left of this is the Package Explorer view, which will contain a list of your Java projects and source code files. To the right are one or more other views that you might or might not find useful; I usually close them by clicking the small "X" next to the name of each one. Several other views that will certainly be useful appear under different tabs in a section of the window below the editing area. If you accidently close one of the important views, such as the Package Explorer, you can get it back by selecting it from the "Show View" submenu of the "Window" menu. You can also reset the whole window to its default contents by selecting "Reset Perspective" from the "Window" menu.


To do any work in Eclipse, you need a project. To start a Java project, go to the "New" submenu in the "File" menu, and select the "Java Project" command. In the window that pops up, you will need to fill in a "Project Name," which can be anything you like. There are two other sections of the window that you might need to pay attention to:

For the "JRE," you should use an "execution environment," which should be set to JavaSE 17 or later, as shown here. And for any program from this textbook, you need to uncheck the option labeled "Create module-info.java file" in the "Module" section. This textbook does not use modular programs! Note that the workspace will remember these two settings for the next time that you create a new project.

After entering a project name, and changing the options if necessary, click the "Finish" button. The project should appear in the "Package Explorer" view on the left side of the Eclipse window. Click on the small triangle or plus sign next to the project name to see the contents of the project. Assuming that you use the default settings, there should be a directory named "src," which is where your Java source code files will go. The project also contains the "JRE System Library". This is the collection of standard built-in classes that come with Java.

To run any of the sample Java programs from this textbook, you need to copy the source code file into your Eclipse Java project. Assuming that you have downloaded the source code file onto your computer, you can copy-and-paste it into the Eclipse window. (Right-click the file icon (or control-click on MacOS); select "Copy" from the pop-up menu; then right-click the project's src folder in the Eclipse window, and select "Paste". Be sure to paste it into the src folder, not into the project itself; files outside the src folder are not treated as Java source code files.) Alternatively, you can try dragging the file icon from a file browser window onto the src folder in the Eclipse window.

To use the TextIO-based examples from this textbook, you must add the source code file TextIO.java to your project. This file has to be in a "package" named textio. If you already have TextIO.java in a folder named "textio," as described above, then you can simply copy-and-paste the textio folder into the "src" folder of your project. Alternatively, you can create the textio package using the "New/Package" command from the "File" menu. This will make a folder named "textio" in your project, inside the src folder, and you can then copy-and-paste TextIO.java into that folder. In any case, package textio should appear under "src" in your project, with TextIO.java inside it. (You can drag files from one location to another in the Package Explorer view, if you accidently put a file in the wrong location.)

Once a Java program is in the project, you can open it in an editor by double-clicking the file name in the "Package Explorer" view. To run the program, right-click in the editor window, or on the file name in the Package Explorer view (or control-click in MacOS). In the menu that pops up, go to the "Run As" submenu, and select "Java Application". The program will be executed. If the program writes to standard output, the output will appear in the "Console" view, in the section of the Eclipse window below the editor section. If the program uses TextIO or Scanner for input, you will have to type the required input into the "Console" view—click the "Console" view before you start typing so that the characters that you type will be sent to the correct part of the window. (For an easier way to run a program, find and click the small "Run" button in Eclipse's tool bar. This will run either the program in the editor window, the program selected in the Package Explorer view, or the program that was run most recently, depending on context.) Note that when you run a program in Eclipse, it is compiled automatically. There is no separate compilation step.

You can have more than one program in the same Eclipse project, or you can create additional projects to organize your work better. Remember to place a copy of TextIO.java, inside a folder named textio, in any project that requires it.


To create a new Java program in Eclipse, you must create a new Java class. To do that, right-click the Java project name in the "Project Explorer" view. Go to the "New" submenu of the popup menu, and select "Class". (Alternatively, there is a small icon in the toolbar at the top of the Eclipse window that you can click to create a new Java class.) In the window that opens, type in the name of the class that you want to create. The class name must be a legal Java identifier. Note that you want the name of the class, not the name of the source code file, so don't add ".java" at the end of the name. The window also includes an input box labeled "Package" where you can specify the name of a package to contain the class. Most examples in this book use the "default package," but you can create your own programs in any package. To use the default package, the "Package" input box should be empty. Finally, click the "Finish" button to create the class. The class should appear inside the "src" folder, in a folder corresponding to its package. The new file should automatically open in the editing area so that you can start typing your program.

Eclipse has several features that aid you as you type your code. It will underline any syntax error with a jagged red line, and in some cases will place an error marker in the left border of the edit window. If you hover the mouse cursor over the error marker or over the error itself, a description of the error will appear. Note that you do not have to get rid of every error immediately as you type; many errors will go away as you type in more of the program! If an error marker displays a small "light bulb," Eclipse is offering to try to fix the error for you. Click the light bulb—or simply hover your mouse over the actual error—to get a list of possible fixes, then click the fix that you want to apply. For example, if you use an undeclared variable in your program, Eclipse will offer to declare it for you. You can actually use this error-correcting feature to get Eclipse to write certain types of code for you! Unfortunately, you'll find that you won't understand a lot of the proposed fixes until you learn more about the Java language, and it is not a good idea to apply a fix that you don't understand—often that will just make things worse in the end.

Eclipse will also look for spelling errors in comments and will underline them with jagged red lines. Hover your mouse over the error to get a list of possible correct spellings.

Another essential Eclipse feature is content assist. Content assist can be invoked by typing Control-Space. It will offer possible completions of whatever you are typing at the moment. For example, if you type part of an identifier and hit Control-Space, you will get a list of identifiers that start with the characters that you have typed; use the up and down arrow keys to select one of the items in the list, and press Return or Enter. (You can also click an item with the mouse to select it, or hit Escape to dismiss the list.) If there is only one possible completion when you hit Control-Space, it will be inserted automatically. By default, Content Assist will also pop up automatically, when you type a period or certain other characters. For example, if you type "TextIO.", you will get a list of all the subroutines in the TextIO class. Personally, I find this auto-activation annoying. You can disable it in the Eclipse Preferences. (Look under Java / Editor / Content Assist, and turn off the "Enable auto activation" option.) You can still call up Code Assist manually with Control-Space.

Once you have an error-free program, you can run it as described above. If you find a problem when you run it, it's very easy to go back to the editor, make changes, and run it again.


(As a side note, it is possible to use the JDK that is included with Eclipse on the command line. That JDK is a directory inside the Eclipse installation, with a long, complex name. The best way to find the name might be to open The "Installed JREs" section of the Eclipse preferences, as described above, select the built-in JRE in the list of "Installed JREs", and click "Edit." The name of the JDK directory will be in the "JRE home" section of the dialog, and you can copy-and-paste it from there. You need to add /bin— or \bin on Windows—to that directory name to get the name of the directory that contains the JDK command line programs such as javac. You can add the full name of that bin directory to your PATH environment variable, or you can use full path names for the javac and java commands.)


2.6.4  BlueJ

As a simpler alternative to Eclipse, I will mention BlueJ, a small IDE that is designed specifically for people who are learning to program. It is much less complex than Eclipse, but it does have some features that make it useful for education. BlueJ can be downloaded from bluej.org. There are installers available for Windows, for MacOS, and for Debian-based Linux (such as Ubuntu Linux and Linux Mint). The installers include an OpenJDK as well as JavaFX (which is required to run BlueJ), so you will not need to do any additional downloading or configuration. As of March, 2022, BlueJ supports Java 11 but not later versions.

In BlueJ, you can begin a project with the "New Project" command in the "Project" menu. A BlueJ project is simply a folder. When you create a project, you will have to select a folder name that does not already exist. The folder will be created and a window will be opened to show the contents of the folder. Files are shown as icons in the BlueJ window. You can drag .java files from the file system onto that window to add files to the project; they will be copied into the project folder as well as shown in the window. You can also copy files directly into the project folder, but BlueJ won't see them until the next time you open the project. When you restart BlueJ, it should show the project that you were working on most recently, but you can open any project with a command from the "Project" menu.

There is a button in the project window for creating a new class. An icon for the class is added to the window, and a .java source code file is created in the project folder. The file is not automatically opened for editing. To edit a file, double-click its icon in the project window. An editor will be opened in a separate window. (A newly created class will contain some default code that you probably don't want; you can erase it and add a main() routine instead.) The BlueJ editor does not show errors as you type. Errors will be reported when you compile the program. Also, it does not offer automatic fixes for errors. It has a less capable version of Eclipse's Content Assist, which seems only to work for getting a list of available subroutines in a class or object; call up the list by hitting Control-Space after typing the period following the name of a class or object.

An editor window contains a button for compiling the program in the window. There is also a compile button in the project window, which compiles all the classes in the project.

To run a program, it must already be compiled. Right-click the icon of a compiled program. In the menu that pops up, you will see "void main(String[] args)". Select that option from the menu to run the program.

One of the neatest features of BlueJ is that you can actually use it to run any subroutine, not just main(). If a class contains other subroutines, you will see them in the list that you get by right-clicking its icon. A pop-up dialog allows you to enter any parameters required by the routine, and if the routine is a function, you will get another dialog box after the routine has been executed to tell you its return value. This allows easy testing of individual subroutines. Furthermore, you can also use BlueJ to create new objects from a class. An icon for the object will be added at the bottom of the project window, and you can right-click that icon to get a list of subroutines in the object. This will, of course, not be useful to you until we get to object-oriented programming in Chapter 5.


2.6.5  The Problem of Packages

Every class in Java is contained in something called a package. Classes that are not explicitly put into a package are in the "default" package. All of Java's standard classes are in named packages. This includes even classes like String and System, which are in a package named java.lang. Classes in java.lang are automatically imported into any Java file, but classes in other packages must be imported using an import directive. My TextIO class is in a package named textio, and it must be imported into a program that wants to use it. I will discuss packages in greater detail in Section 4.6. For now, you just need to know some basic facts.

Although most of my examples are in the default package, the use of the default package is in fact discouraged, according to official Java style guidelines. Nevertheless, I have chosen to use it, since it seems easier for beginning programmers to avoid packages as much as possible, at least at first. If Eclipse tries to put a class into a package, you can delete the package name from the class-creation dialog to get it to use the default package instead. But if you do create a class in a package, the source code starts with a line that specifies which package the class is in. For example, if the class is in a package named test.pkg, then the first line of the source code will be

package test.pkg;

For example, the source code for TextIO begins with "package textio;". I put TextIO in a package because a class that is in a non-default package cannot use a class from the default package. That is, if TextIO were in the default package, then it could only be used by programs that are also in the default package. (In fact, in earlier versions of this textbook, TextIO was in the default package. I moved it to package textio as of Version 8 of the book.)

When packages are used in a command-line environment, some complications arise. For example, if a program is in a package named test.pkg, then the source code file must be in a subdirectory named "pkg" inside a directory named "test" that is in turn inside your main Java working directory. Nevertheless, when you compile or execute the program, you should be working in the main directory, not in the subdirectory. When you compile the source code file, you have to include the name of the directory in the command: For example, for a program in package test.pkg use "javac test/pkg/ClassName.java" on Linux or MacOS, or "javac test\pkg\ClassName.java" on Windows. The command for executing the program is then "java test.pkg.ClassName", with a period separating the package name from the class name.


2.6.6  About jshell

I will mention one more command-line tool for working with Java: jshell. The jshell command is a standard part of the JDK (after Java 8). If you can use the javac and java commands on the command line, then you can also use jshell. The purpose of jshell is to let you type in and execute Java code without the bother of creating a .java file and writing a main program. To start jshell, just enter the command on a line by itself. You will get a jshell prompt where you can enter either a Java statement or a Java expression. If you enter a statement, it will be executed. If you enter an expression, its value will be printed. You do not have to place a semicolon at the end of a line. Here is a short example of a jshell session. The text typed by the user is shown in bold.

$ jshell
|  Welcome to JShell -- Version 17.0.2
|  For an introduction type: /help intro

jshell> System.out.println("Hello World")
Hello World

jshell> int x = 42
x ==> 42

jshell> x * x
$3 ==> 1764

jshell> /exit
|  Goodbye

Using jshell can be a great way to learn Java and to experiment with its features. I won't give any more detailed information about it in this book, but you can learn more at

https://docs.oracle.com/en/java/javase/17/jshell/introduction-jshell.html

or you can use the /help command inside jshell to learn more about it.


End of Chapter 2


[ Previous Section | Chapter Index | Main Index ]