Hello, Java
Resources
A quick guide |
Java interview questions |
Essential classes |
Deployment |
Create GUI with Swing |
online java compiler
Why Java
Java, developed by Sun Microsystems Corporation, enjoys its popularity because it is a
platform-independent,
object-oriented programming (OOP) language, and because certain kinds of Java programs, called
applets, can be embeded in Web pages.
- Platform-independent (& how Java programs are executed)
Java Virtual Machine (JVM) is a software "computer" that runs inside an actual computer. So before you can execute a Java program, the classes in the Java program must first be translated into an executable form by a compiler: Java compiler generates a file of platform-independent Java byte code instructions.
Java compilers are platform dependent, even though they produce the same byte code file for a given Java source program.
- Object-oriented programming (OOP) language (C++ and Python are also OOP languages)
- Java applets
Classes and Objects
- In Java, and OOP in general, the class is the fundamental programming unit. Every Java program is written as a collection of classes (C++ and Python programs don't have to have classes).
- Class definitions are stored in separate files with the extension .java; the file name must be the same as the class name defined within (again, C++ and Python don't follow this rule).
- Classes vs objects: classes are the descriptions/definitions/blueprints; objects are the instances (individual entities).
Java SDK
we will use
Eclipse. Try the Eclipse Installer (
download), which provides the easiest way to install & update Eclipse.
The Java API
The Java API (the Java Application Programming Interface) contains a collection of packages (libraries); each package contains a collection of related Java classes. The Swing package, the AWT package, and the util package are among the commonly used packages.
Java Syntax Basics
"Hello World" in Java
public class HelloWorldTxt {
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
"Hello World" with GUI
import javax.swing.*;
public class HelloWorldGUI {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("What's your name?");
JOptionPane.showMessageDialog(null, "Hello " + name);
}
}
- The import statement (line 1) tells the Java compiler to make the names defined in the Swing package accessible to this class. The semicolon at the end of the line is used to terminate a Java statement.
- Line 2 identifies HelloWorld as a public class and makes it visible (public) to other classes (or JVM).
- Line 3: main() function is where the JVM begins the execution of an application program. The public static void keywords mean the JVM can call the program's main method to start the program (public) without creating an instance of the class (static; a static method belongs to the class itself and a non-static, or instance, method belongs to each object that is generated from that class), and the program does not return data to the Java VM interpreter (void) when it ends.
Compiling and Running a Simple Program