- Commanding a Turtle
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- File Input/Output
- A Java Program
- What's a Class?
- Extending Existing Classes
- Turtle Graphics
- Control Flow
- User Interface Events
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- Installation Notes
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
A Java Program
In this lesson, you will learn
- what makes Java such a hit with programmers,
- what the components in the world of Java are,
- how you build a Java application by going through the "Edit, Compile, Execute" process.
Java is a programming language built for the age of the Internet. It was built for a world in which everything that has some sort of electronic component: stereo systems, wireless phones, cars, even your refrigerator, are all on the Internet. This world is right around the corner.
What is so unique about Java that has propelled its rapid, wide acceptance?
- It's available on more devices world-wide than any other language. Notice that I say devices - not just computers. Java is currently being used not only on mainframe systems in the enterprise and personal computers in the office and at home - it's also running in cellphones.
- It was carefully designed to eliminate many of the most common causes of programming errors - bugs. Java programs that compile error-free tend to work! Strong data typing and complete memory management are two features that make this possible.
- It provides for secure programs that can be executed on the Internet without worry of them infecting your system with some virus or planting a trojan horse.
What is a Java Program and How do I Create One?
Let's look at what makes up a Java program. A Java program is built by writing (and referencing already available) things called classes. In the simplest sense, a Java program is a bunch of classes. You will construct at least one, typing its source code into a file.
The stuff you will enter (text) has a very specific structure (its syntax) that the Java compiler expects. You create your Java programming language files with an editor that is available on your computer. On a PC running Windows, Wordpad or Notepad will work just fine. On a Sun workstation, textedit is a nice editor.
Once you have some complete Java source code in a file, you compile it. The Java compiler turns your file full of characters into another file which contains instructions that a JVM (Java Virtual Machine) can interpret, a ".class" file.
The Java Virtual Machine takes over from here. JVMs exist for any computer and operating system, for example PCs running Windows, Sun Microsystems' computers running Solaris or Linux, cellphones, etc... The JVM takes your ".class" file, loads it into its virtual memory, links lots of stuff together, and then starts interpreting/executing the program. During linking, your class file will be combined with other classes that are part of the Java environment. Standard classes exist for helping you do things like displaying text on the display, get characters typed on the keyboard, display graphics stuff, communicate over the Internet, ... And, then, off it goes; your program comes to life.
A Java Application, Our First!
There are two kinds of Java programs: applets and applications. You will start with a very simple application.
To get going you need to start by working your way through what's known as the "Edit, Compile, Execute" process or cycle. This is something you will tend to do over and over as you work on your Java programs. This exercise will give you a feel for how you will be working in the Java environment. To make this quick and simple, you'll start with what is the shortest Java program possible.
Type the following text into your computer, putting it in a file called "Hello.java"
Once you have it in a file in the computer, check it to see that it matches the example - character for character, upper-case only where the example shows upper-case characters, the same punctuation, etc... When you think you have it, make sure to save the text to the file: Hello.java.
Now it's time to run the Java compiler with the file "Hello.java" as input. This will get you your "Hello.class" file. Type in:
Did it complain? Did it find a typo that you missed? If so, go back into the editor and compare your text with what I've provided. The compiler tells you the line number that it detected an error on. This should help. But, the error can be on an earlier line too. Repeat this "Edit, Compile" cycle until you get the compiler to quit complaining.
Now you can use the JVM to execute your program. Type in:
Notice that you don't type "java Hello.class" even though you had to provide the full filename to the Java compiler. The Java Virtual Machine (JVM) assumes and looks for the file Hello.class even though you only provided the class name Hello, not the filename. If you have everything right, the system should respond with:
Cool... You've just entered and executed your first Java application.
You've used the println command in your jLogo programs too. I added this command in preparation for your move to Java. In standard Logo, the print command does what jLogo's println does. But, in Java, print does not add the newline character to the output - just like the way print works in jLogo. In Berkeley Logo, the type command leaves off the newline.
|type||Display the text provided as its input/argument.|
|println||println||Display the text provided as its input/argument, followed by a newline character.|
Play around, try both methods.
One of the most common initial mistakes is mixing character case. In Java, case matters. The word someThing is not the same as the word something.
A class' name, must match the name of the file that it is in. The Java source code will compile without any indication of problems. But, when you try to execute it, the JVM will complain that it can't find your class, the name of which is the file name.
Finally, notice that the java compiler command, javac, expects a full filename including the ".java" part; but, the java command will not accept the full filename - it expects to find a file with the name you specify, ending in ".class"
Go to the Table of Contents
On to What's a Class?