Join Peggy Fisher for an in-depth discussion in this video Exploring the IDE, part of Learning Java.
- The term IDE refers to the software used to write programs in an environment that supports a text editor that we'll use for writing our code, a compiler, which converts the code to machine language, or when you're talking about Java, to byte code. It usually includes a debugger and a runtime environment that's used to test your program execution. Some IDEs also offer intelligent code assistance referred to as IntelliSense. Kind of reminds me of Spell Check in Microsoft Word.
An IDE is not required to program in Java. Some programmers prefer to write code in a separate text editor, compile the code using the Java C compiler, and then testing the program from the DOS prompt. For this series we will be using the NetBeans IDE, although I will give you one example of how to write a program and compile it using the DOS prompt. When we start NetBeans, it will automatically open to a start page. From this page we have the option of taking our tour, trying a sample project, reading the latest NetBeans news, or visiting the community corner.
To prevent this page from always opening at startup, let's deselect the checkbox Show on Startup in the upper right corner. Now we can go ahead and close this window. To get started we want to create a new project. Go to File, New Project. Make sure the Java application is highlighted on the righthand side. Click Next. Let's name our program Demo with a capital D. We will leave the Project Location and the Project Folder to its default locations.
Click Finish. On the left there will be a window containing a list of our projects. Currently, we only have the one Demo project. When we initially create a project with these default settings, NetBeans creates a project folder with several subfolders. Inside the Package folder which is, in our case, labeled demo with a little d, there's only one file called demo.java. For future reference, let's review some of the key parts of the NetBeans IDE before we take a closer look at our program.
At the top is the standard menu toolbar which contains File, Edit, View, Navigate, Source, Refactor, Run, Debug, Profile, Team, Tools, Window, and Help. Take a moment to click on each item to see the contextual menus available for each option, such as Help. Right below that toolbar are some of the often-used icons. Starting from the left we have New File, New Project, Open Project, Save, Build, Clean and Build, Run, Debug, and more.
Let's return our attention to the Demo program we created. Notice the tab for the Source code contains the name of the program with the extension .java. This is done automatically by the IDE. At the top of the program, you'll notice there's several gray lines. This is a comment in Java and it's indicated by using the /* and closing */. This is where we'll describe the intent of our program. The IDE also automatically adds our author name in the next set of comments.
Let's change this comment to say Demo. Now in the main portion of the program, let's write a simple test program where we display a greeting to the console. You see where it says TODO code application logic here? Let's replace that line, which is also a different type of comment, a single line comment, with the command System. Notice the capital S. Remember, Java is case sensitive. Dot out. You can see how IntelliSense is prompting me each time.
Dot println. As we move forward I'll usually refer to this as print line. Open parenthesis double quotes. We're going to put a message that says Hello, and welcome to up and running with Java. Anytime we use double quotes around a series of letters or numbers, it's considered a literal in Java. We'll talk more about that later. Now we can use the hammer icon, which is our Build icon. It'll check for any errors that we have in our code.
Let's go ahead and click on the hammer. It appears that the hammer is done, but we want to see the output, so let's go to Window, Output. We can see, if I scroll down in the window in the bottom, the Build was successful. The hammer icon is used to build the byte code file. This is where the IDE converts the program to an object file that can be understood by the Java Virtual Machine. Next, let's click the green Run arrow. Now we can see the message is written to our output window.
Hello, and welcome to up and running with Java. Again, the Build was successful. We have just written, compiled, and successfully run our very first Java program. Congratulations.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs