My First Java Program

Page content


In computer terminology, we use the term character to refer to any one of the following:

• a digit from 0 to 9;

• an uppercase letter from A to Z;

• a lowercase letter from a to z;

• a special symbol like (, ), $, =, <, >, +, -, /, *, etc.

The following are commonly used terms:

letter – one of a to z or A to Z

lowercase letter – one of a to z

uppercase letter – one of A to Z

digit – one of 0,1,2,3,4,5,6,7,8,9

special character – any symbol except a letter or a digit

e.g. +, <, >, $, &, *, /, =

alphabetic – used to refer to a letter

numeric – used to refer to a digit

alphanumeric – used to refer to a letter or a digit

Characters are the basic building blocks used in writing programs;

we put characters together to form variables and constants;

we put variables, constants and special characters to form expressions such as (a + 2.5) * c;

we add special words such as if, else and while to form statements such as

if (a > 0) b = a + 2;

and we put statements together to form programs.

Welcome to Java Programming

We take a quick peek at the Java programming language by writing a program to print, on the screen, the message

Welcome to BrightHub

One solution is the following

public class Welcome { public static void main(String[] args) { System.out.printf(“Welcome to BrightHub”); } }

A Java program consists of one or more classes. Our sample program consists of one class called Welcome. A class normally contains two kinds of members - variables and methods. A variable is used to hold data used by the class. Method is the Java term for a group of statements which perform a subtask, perhaps one of many, which can be executed to help accomplish the task for which the class is written. Other languages use the term function instead of method. Our program contains no variables and one method called main.

For now, the programs we will write must have a method named main which we will write with the following “header”:

public static void main(String[ ] args)

The Java interpreter starts executing a program by executing the statements in main. The word public makes this possible; it indicates that main is ‘known’ (and can be called from) outside the class in which it is defined. Thus the Java interpreter ‘knows’ and can ‘call’ main. The other terms in the header of main will be explained in due course. For now, just think of them as required.

The body of main (the part between the left and right braces) contains one statement:

System.out.printf(“Welcome to BrightHub”);

When executed, this statement will print

Welcome to BrightHub

on the ‘standard output’. For now, take this to mean the monitor (screen).

System.out.printf is a standard method which can be used for printing data on the ‘standard output’. To be more precise, printf is a method which belongs to the object System.out; for now, the distinction is not important. For the most part, we will refer to the method simply as printf. In the example, the data to be printed consists of a string (a set of characters enclosed in double quotation marks).

Note: printf was introduced in JDK 5.0. It is much more versatile and flexible than the older print and println methods. In this series, we will use printf.


Reference text: Java Programming – A Beginner’s Course

Related programming references

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions