Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 2: Your First Java Program

Java is a popular programming language that is especially suited to develop applications for business and the Internet of Things (IoT).

This guide will use the Eclipse IDE (Neon version) to develop the java code.

Hello World!

Most beginning programmers start with the standard "Hello World" code:

package buffalostate.edu.sp18.assignments.put-your-lastname-here;

/**
 *
 * File: helloworld.java
 *
 * This is a Hello World Application
 * It prints a short statement to the standard out
 */

public class HelloWorld {
/**
 * The main methid is the entry point for a Java program when executed
 *
 * @param args String[] - command line arguments passed to program
 * @return none
 */

  public static void main(String[] args) {
  // print to the console
    System.out.println("Hello World!");
  }
}

Figure 2-1: Code for helloworld.java

The Eclipse Console would look like this:

Console
<terminated>
Hello World!

Figure 2-2: HelloWorld.java Console Output

Java Comments

Java provides inline, single line, multi=line, and JavaDoc comments. You can even use HTML tags in JavaDoc comments.

JavaDoc comments:

/**
 * The <b>main</b> method is the entry point for a Java program when executed
 *
 * @param args String[] - command line arguments passed to program
 * @return none
 */

Multi-line comments:

/*
  This
  is
  a
  multi-line
  comment
 */

Single line comments:

// print to the console

Inline comments:

System.out.println("Hello World!");// print this out

Figure 2-3: Code for helloworld.java
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland