Author Topic: Introduction to Programming A Java Program  (Read 1237 times)


  • Sr. Member
  • ****
  • Posts: 423
  • If You Believe You Can, Than You Should Do It.
Introduction to Programming A Java Program
« on: April 02, 2015, 06:11:06 PM »
This two-part tutorial introduces the structure, syntax, and programming paradigm of the Java™ language and platform. You'll learn the Java syntax you are most likely to encounter professionally and Java programming idioms you can use to build robust, maintainable Java applications. In Part 1, J. Steven Perry guides you through the essentials of object-oriented programming on the Java platform, including fundamental Java syntax and its use. You'll get started with creating Java objects and adding behavior to them, and conclude with an introduction to the Java Collections Framework, with considerable ground covered in between.
Why Java?
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 running in Palm Pilots, wireless phones, and appliances.

    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, Palm Pilots and Handspring Visors running PalmOS, 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 ""
class Hello

        public static void main(String[] args)
            System.out.println("Hello World!");   

    } // end class Hello
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:

Now it's time to run the Java compiler with the file "" 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 Example 16.1 again.  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:

    java Hello

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:

    Hello World!

Cool... You've just entered and executed your first Java application.

You've used the println command in your TurtleTalk programs too.  I added this command in preparation for your move to Java.  In standard Logo, the print command does what TurtleTalk println does.  But, in Java, print does not add the newline character to the output - just like the way print works in TurtleTalk.  In Logo, the type command leaves off the newline.
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"
« Last Edit: April 02, 2015, 06:16:52 PM by jitudas »
Jitu Das
Deploma in CSE
Bangladesh Skill Development Institute
Contact Number:+8801620210620