Our First Java Program
The Hello World Program
The classic first program a programming writes in any new programming language is known as the Hello World Program. Basically, it is a short program where by the words “Hello World!” are printed on the screen. We will use this program to understand the basic anatomy of a Java class.
HelloWorld.java
public class HelloWorld
{
public static void main(String[] void)
{
// Display a greeting in the console window
System.out.println(”Hello World!”);}
}
Output: Hello World!
Classes
The Puzzle As A Whole
In Object Oriented Programming (OOP), a class is a file that encompasses the many attributes and capabilities of a certain entity. For example, each class has variables and methods.
Let’s take a look at our code…
public class HelloWorld
{
…}
- The keyword
classmust be included between the access specifer and the class name. - Access Specifiers
- Classes that are
publiccan be accessed from any other class - Classes that are
protectedcan only be accessed from other classses in the same package - Classes that are
privatecannot be accessed from anywhere. This is usually pointless, unless you are using inner classes. More on this during later sessions.
- Classes that are
- Nomenclature
- Must begin with a letter (A-Z)
- Cannot contain symbols, except underscore “_”
- Java language is case-sensitive, ie.
helloWorldis not the same asHelloWorld - Cannot be the same as special keywords reserved in Java
- Usually, camel notation is used for class names, ie.
HelloWorld
Objects
Different Interpretations of the Same Puzzle
In Object Oriented Programming (OOP), data is stored and carried out by objects. Objects are instances of a specific Class. Objects are created and instantiated by the new keyword new.
Let’s take a look at our code…
HelloWorld myProgram = new HelloWorld();
- Objects contain the same properties as their parent classes
- Multiple objects can be created from a single class, each with different information stored within
- Objects are created using the keyword new
- Object values are stored as variables, and follow the same nomenclature
- Note: Objects are stored in memory; all objects created while a program is running are destroyed when the program halts
Variables/Fields
The Attributes of Every Java Program
A variable is a structure that is assigned to hold data and is accounted for by a unique name. In Java, fields are variables that are created outside of methods, at the beginning of the class.
Let’s take a look at our code…
public class HelloWorld
{
private String myString = “Value”;
}
- Only fields have access specifiers; variables don’t
- Fields that are
publiccan be accessed from any other class - Fields that are
protectedcan only be accessed from other classses in the same package - Fields that are
privatecan only be accessed from within their own class.
- Fields that are
- Nomenclature
- Must begin with a letter (A-Z)
- Cannot contain symbols, except underscore “_”
- Java language is case-sensitive, ie.
myWorldis not the same asMyString - Cannot be the same as special keywords reserved in Java
- Usually, camel notation is used for variable names; however, with the first word in all lower-case, ie.
myString
- Primitive Types
- boolean - true/false
- int - an integer value ranging from -2,147,483,648 to 2,147,483,467
- double - a decimal value ranging from NEG INF to POS INF
- char - an integer representation of characters from the Unicode encoding system
- String - a set of consecutive character values
- Declaration vs Instantiation
- Declare - a variable is declared by typing its type and variable name.
- Instantiate - a variable is instantiated or assigned to a specific value, only when an assignment operator is used.
Methods
Actions Speak Louder Than Fields
In Object Oriented Programming (OOP), methods are used to access and manipulated data stored in variables. Methods are also known functions, routines, and procedures. The signature of a method contains four elements - access specifer, return type, method name, and parameters.
public class HelloWorld
{
public void incrementCounterByValue(int increment)
{
newValue = oldValue + increment;}
public int getNewValue()
{
return newValue;}
}
- Mutators vs Accessors
- Mutator methods are called upon to mutate or update values stored in that class as varibles.
- Accessor methods are called upont to access certain variables and return their data.
- Access Specifiers
- Methods that are
publiccan be accessed from any other class - Methods that are
protectedcan only be accessed from other classses in the same package - Methods that are
privatecan only be accessed from within their own class.
- Methods that are
- Nomenclature
- Must begin with a letter (A-Z)
- Cannot contain symbols, except underscore “_”
- Java language is case-sensitive, ie.
helloWorldis not the same asHelloWorld - Cannot be the same as special keywords reserved in Java
- Usually, camel notation is used for method names; however, with the first word in all lower-case, ie.
myMethod
- Return Value/Statement
- Every method must have a return value.
- The return value of accessor methods is the Class Type of the value they return. ie,
public String myMethod… if a String is returned - The return value of mutator methods that do not return any value is
void
Main Method
The main method is a very important method in Java. Every program can have many classes working together; however, each program can have a maximum of one main method. This is because when a program is run, the Operating System calls the main method.
When trying to execute a Java program, you type:
java MyProgram
This signified that MyProgram is only class in this program with the main method.
public- the main method must always be public because it has to accessed by the OS, which is outside the boundaries of the classstatic- static is a very confusing term for beginners and often skipped by professors in introductory courses. It basically means that this method will only exist in the class and not object of this class. More on this in later sessions.void- void, of course, is a return type and signifies that this is a mutator method that does not return any values when calledmain- main is simply the name of the method. Remember, Java is case-sensitive, soMainwill not work(String[] args)- args is the parameter of this method. String[] signifies the data type of the parameter. This method accepts an array of Strings, separated by a space, as the parameter.
Comments
The Invisible Instructions
Comments are very important in software development. Whether you’re programming in Java, PHP, C#, or BASIC, it is very important to write code that is readible not only by yourself, but by others you may work with. Comments
allow you to leave notations and notes regarding the structure of your program and any clarifications you may want to make.
Let’s take a look at our code…
public class HelloWorld
{
// Display a greeting in the console window
}
- There are two notations for comments:
- Remember, any segment of code contained within a comment is not read by the compiler
// Comment- Any line that begins with
//in considered a comment
- Any line that begins with
/* Comment */- Any segment of code beginning with
/*and ending with*/is considered a comment
- Any segment of code beginning with