Adding your first line of Java code!
If you've followed this guide to creating a Java project in NetBeans, then you should now be staring at a window that is essentially fairly blank except for a line of icons at the top and a panel with a "tree" in the top-left, as pictured opposite. This tree allows you to browse the various files that make up your project. For now, we won't get too bogged down in the details of these files. The one that interests us is that labelled Main.java, as circled. For now, this is where our program will live. Later on, we'll see that a typical Java application will actually consist of various different little pieces of program. But to get us started, the single "main" file will do just fine.
If you double-click on Main.java, the right-hand side of the screen will change to show the actual program. Instead of providing us with a file that is literally blank, NetBeans actually helps us by providing a "skeleton" program that we can add to. Initially, it should look something like this:
Locate the line starting with // TODO (indicated by the red arrow in the screenshot above). We're going to insert a line of Java code at this point. You can either add the line instead of the // TODO line, or else add the line below. Remember that the line is just a comment, so if you leave it in, Java will ignore it. But equally, Java won't care if you remove it. In the illustration below, we'll actually remove the comment.
In traditional fashion, we're going to add a line to the program so that when run, it prints the message Hello, world!. The line we're going to place instead of the comment is the following:
System.out.println("Hello, world!");
Running the program
If you've typed everything correctly, then you should see something like the following appear at the bottom of the screen:
0 Response to "Adding your first line of Java code!"
Post a Comment