Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Java lecture 1: Your first cup of Java. Teach/learn online

JavaFAQ Home » Java Lectures by Anatoliy Malyarenko Go to all tips in Java Lectures by Anatoliy Malyarenko


Bookmark and Share
All Java Lectures contents page | Java lecture 1: Your first cup of Java | Java lecture 2: A quick introduction to Java programming | Java Lecture 3: Object-oriented programming concepts: theory. | Java Lecture 4: Object-oriented programming concepts: example. | Java Lecture 5: Variables | Java Lecture 6: Operators | Java Lecture 7: Expressions, statements, and blocks | Java Lecture 8: Control flow statements | Java Lecture 9: The life cycle of an object | Java Lecture 10: Characters and strings | Java Lecture 11: Numbers and arrays | Java Lecture 12: Classes | Java Lecture 12a: Introduction to Painting Concepts | Java Lecture 13: Inheritance in Java. Part I | Java Lecture 13: Inheritance in Java. Part II | Java Lecture 14: Interfaces and packages. Part I. | Java Lecture 14: Interfaces and packages. Part II. | Java Lecture 15: Swing I. Part I |

Your first cup of Java

by Anatoliy Malyarenko

Today we publish first Java Lecture by Anatoliy Malyarenko.  We are very thankful to him that he gave his permission to publish all of them (25 Java Lectures) on our site! If you starting with Java, the Java Lectures by Anatoly is best way to go.

Very easy to understand, excellent style of presentation - you will read them in one breath Smile. Very logical presentation, step by step - the lectures are prepared by professional  mathematician! . Try it, compile your first Java program right now!

Abstract

  • Contents of the lecture.

  • A checklist.

  • Creating your first application.

  • Creating your first applet.

  • About the Java technology.

  • What can Java technology do?

  • How will Java technology change your life?

A checklist

To write your first program, you need:

  1. The JavaTM 2 Platform, Standard Edition. You can download it here: http://java.sun.com/javase/downloads/index.jsp  Make sure you download the SDK, not the JRE.
  2. A text editor. We use RealJ. You can find it here.

Creating your first application

Your first program, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will:

Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand.
Compile the source file into a bytecode file. The compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler converts these instructions into a bytecode file.
 


Run the program contained in the bytecode file. The Java interpreter installed on your computer implements the Java VM. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

So, you've heard that with the Java programming language, you can "write once, run anywhere." This means that when you compile your program, you don't generate instructions for one specific platform. Instead, you generate Java bytecodes, which are instructions for the Java Virtual Machine (Java VM). If your platform -- whether it's Windows, UNIX, MacOS, or an Internet browser -- has the Java VM, it can understand those bytecodes.

Creating a source file

Start RealJ, click "New Project..." on the Project menu. In the dialog that follows, type in a name HelloWorldApp and choose the "Application Project" radio button. Then click "OK".

RealJ will then automatically generate a Java source file for your application class. You are prompted to save the project, and when you do you should create a new folder for it, since the Java source files will be saved in the same folder as the project.

You must then edit the Java file to create your own application. Type the following code:
The source code:   

Code:
   
/**
* File: HelloWorld.java
* Description: First application
* Author: your name
* Mail: your e-mail address
*/
class HelloWorldApp {
   public static void main(String[] args) {
      // Display "Hello World!"
      System.out.println("Hello World!");
   }
}

Be careful when you type. Type all code, commands, and file names exactly as shown. The Java compiler and interpreter are case-sensitive, so you must capitalise consistently.

HelloWorldApp is not helloworldapp
Save this code to a file. From the menu bar, select File --> Save.

Compiling a source file


From the Start menu, select Programs --> Accessories -->Command Prompt. Change your current directory to the directory where your file is located.

If you enter dir at the prompt, you should see your file:



You can compile the application by choosing "Build" from the Build menu.

The compiler has generated a Java bytecode file, HelloWorldApp.class.

At the prompt, type dir to see the new file that was generated:


Now that you have a .class file, you can run your program.

Run the program

Run your program by choosing "Run Application" from the Build menu. Now you should see:



Congratulations! Your program works.

Creating your first applet

HelloWorldApp is an example of a Java application, a stand-alone program. Now you will create a Java applet called HelloWorld, which also displays the greeting "Hello world!".

Unlike HelloWorldApp, however, the applet runs in a Java-enabled Web browser such as Netscape Navigator or Microsoft Internet Explorer.
To create this applet, you'll perform the basic steps as before: create a Java source file; compile the source file; and run the program.

Creating a Java source file

Start RealJ, click "New Project..." on the Project menu. In the dialog that follows, type in a name HelloWorld for your main applet class, and choose the "Applet Project" radio button.

Then click "OK". RealJ will then automatically generate a Java source file for your applet class, and an HTML file that can be used to launch the applet. You are prompted to save the project, and when you do you should create a new folder for it, since the Java and html source files will be saved in the same folder as the project.

You must then edit the Java file to create your own applet. Type the following code:

The source code

Code:

/**
* File: HelloWorld.java
* Description: First applet
* Author: your name
* Mail: your e-mail address
*/
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
   public void paint(Graphics g) {
      g.drawString("Hello world!", 50, 25);
   }
}

Save this code.

You also need an HTML file to accompany your applet. Fortunately, RealJ generates this file automatically under the name Example1.html.

Compiling the source file

Choose "Build" from the Build menu to compile the applet. The compiler should generate a Java bytecode file, HelloWorld.class.

Running the program

Although you can view your applets using a Web browser, you may find it easier to test
your applets using the simple appletviewer application that comes with the JavaTM Platform.
Choose "Run Applet" from the Build menu. Now you should see:

Congratulations! Your applet works.

An additional feature is that you can open the HTML file in a Web browser by opening the HTML file (double-click its icon in the class browser) and choosing "Open in Web Browser" from the Build menu. Another way to do this is to right-click on the file name in the class browser and choose "Open in Web Browser" from the pop-up menu.

The Java programming language

Java technology is both a programming language and a platform. The Java programming language is a high-level language that can be characterised by
all of the following buzzwords:

  • Simple

  • Object oriented

  • Distributed

  • Interpreted

  • Robust

  • Secure

  • Architecture neutral

  • Portable

  • High performance

  • Multi-threaded

  • Dynamic

With most programming languages, you either compile or interpret a program so that you can run it on your computer. The Java programming language is unusual in that a program is both compiled and interpreted. With the compiler, first you translate a program into an intermediate language called Java bytecodes -- the platform-independent codes interpreted by the interpreter on the Java platform. The interpreter parses and runs each Java bytecode instruction on the computer. Compilation happens just once; interpretation occurs each time the program is executed. The following figure illustrates how this works.


You can think of Java bytecodes as the machine code instructions for the Java Virtual Machine (Java VM). Every Java interpreter, whether it's a development tool or a Web browser that can run applets, is an implementation of the Java VM.

Java bytecodes help make "write once, run anywhere" possible. You can compile your program into bytecodes on any platform that has a Java compiler. The bytecodes can then be run on any implementation of the Java VM. That means that as long as a computer has a Java VM, the same program written in the Java programming language can run on Windows 2000, a Solaris workstation, or on an iMac.

 

The Java platform

A platform is the hardware or software environment in which a program runs. We've already mentioned some of the most popular platforms like Windows 2000, Linux, Solaris, and MacOS. Most platforms can be described as a combination of the operating system and hardware. The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.

The Java platform has two components:

  • The Java Virtual Machine (Java VM)
  • The Java Application Programming Interface (Java API)

You've already been introduced to the Java VM. It's the base for the Java platform and is ported onto various hardware-based platforms. The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages.

The following figure depicts a program that's running on the Java platform. As the figure shows, the Java API and the virtual machine insulate the program from the hardware.

Native code is code that after you compile it, the compiled code runs on a specific hardware platform. As a platform-independent environment, the Java platform can be a bit slower than native code. However, smart compilers, well-tuned interpreters, and just-in-time bytecode compilers can bring performance close to that of native code without threatening portability.

What can Java technology do?

The most common types of programs written in the Java programming language are applets and applications. If you've surfed the Web, you're probably already familiar with applets. An applet is a program that adheres to certain conventions that allow it to run within a Java-enabled browser.

However, the Java programming language is not just for writing cute, entertaining applets for the Web. The general-purpose, high-level Java programming language is also a powerful software platform. Using the generous API, you can write many types of programs.

An application is a standalone program that runs directly on the Java platform. A special kind of application known as a server serves and supports clients on a network. Examples of servers are Web servers, proxy servers, mail servers, and print servers.

How does the API support all these kinds of programs? It does so with packages of software components that provide a wide range of functionality. Every full implementation of the Java platform gives you the following features:

  • The essentials: Objects, strings, threads, numbers, input and output, data structures, system properties, date and time, and so on.
  • Applets: The set of conventions used by applets.
  • Networking: URLs, TCP (Transmission Control Protocol), UDP (User Datagram Protocol), sockets, and IP (Internet Protocol) addresses.
  • Internationalisation: Help for writing programs that can be localised for users worldwide. Programs can automatically adapt to specific locales and be displayed in the appropriate language.
  • Security: Both low level and high level, including electronic signatures, public and private key management, access control, and certificates.

The Java platform also has APIs for 2D and 3D graphics, accessibility, servers, collaboration, telephony, speech, animation, and more. The following figure depicts what is included in the Java 2 SDK.

How will Java technology change your life?

I can't promise you fame, fortune, or even a job if you learn the Java programming language. Still, it is likely to make your programs better and requires less effort than other languages. I believe that Java technology will help you do the following:

  • Get started quickly: Although the Java programming language is a powerful object-oriented language, it's easy to learn, especially for programmers already familiar with C or C++.
  • Write less code: Comparisons of program metrics (class counts, method counts, and so on) suggest that a program written in the Java programming language can be four times smaller than the same program in C++.
  • Write better code: The Java programming language encourages good coding practices, and its garbage collection helps you avoid memory leaks. Its object orientation, its JavaBeans component architecture, and its wide-ranging, easily extendible API let you reuse other people's tested code and introduce fewer bugs.
  • Develop programs more quickly: Your development time may be as much as twice as fast versus writing the same program in C++. Why? You write fewer lines of code and it is a simpler programming language than C++.
  • Avoid platform dependencies with 100% Pure Java: You can keep your program portable by avoiding the use of libraries written in other languages.
  • Write once, run anywhere: Because 100% Pure Java programs are compiled into machine-independent bytecodes, they run consistently on any Java platform.
  • Distribute software more easily: You can upgrade applets easily from a central server. Applets take advantage of the feature of allowing new classes to be loaded "on the fly," without recompiling the entire program.
     

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy