Pin Me

Java Programming For Beginners - Characters and printf

written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 11/17/2008

In this article, we explain the notion of characters, the basic building blocks from which all programs are created, and we begin our programming journey by writing our first Java program.

  • slide 1 of 3

    Characters

    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.

  • slide 2 of 3

    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.