I will start teaching you how to write code in Java by showing you how to instruct Java to output a message to the console.
Table of Contents
Display a Message to Console
Goal: Instruct Java to display a message, Welcome to Java!
on the console.
Code
public class Welcome { public static void main(String[] args) { //Display the message Welcome to Java! on console System.out.println("Welcome to Java"); } }
Deconstruction
Class
public class Welcome {
In Java, the first line of a Java program defines the class. Welcome
is the class name for this piece of code. Note that the convention is to have the class name start with an uppercase letter; it’s not mandatory though.
Method
public static void main(String[] args) {
The second line defines the main method. The Java program executes from the main method. Note that a class can contain several methods. And a method can contain several statements.
public means that main() can be called from anywhere.
static means that main() doesn’t belong to a specific object
void means that main() returns no value
main is the name of a function. main() is special because it is the start of the program.
String[] means an array of String.
String means a line or bunch of characters. In this case, the String
is the message that is instructed to be printed to the console, Welcome to Java
.
args is the name of the String[] (within the body of main()). args is not special; you could name it anything else and the program would work the same. [Source]
Classes vs. Methods
Classes and methods exist in Java for the same facilitation variables provide. Storing instructions to call them back later. Variables store 1 piece of information that you can call back. Statements make use of variables when telling Java what to do. Methods contain several statements, and Classes store several methods.
So in other words Java uses a “container” hierarchy to organize all the instructions that you command the Java program to do- and classes & methods are involved in that hierarchy as parent & child, respectively.
The benefit of this “container” hierarchy is that the programmer can call one class or several methods, instead of re-writing vast numbers of statements that make up methods & classes.
Comments
On the 3rd line is a line comment:
//Display the message Welcome to Java! on console
Comments are complete ignored by the compiler and do not effect program execution at all. A line comment uses two slashes //
to comment out what comes after it, limited to that particular line.
Another type of comment is called a block or paragraph comment, which uses /*
and */
to comment out what is in between them, not limited to a single line.
Example: All of the code below is commented out.
// This application program displays Welcome to Java! /* This application program displays Welcome to Java! */ /* This application program displays Welcome to Java! */
Statements: Println
Note that the main method contain the statement:
System.out.println("Welcome to Java");
This statement tells Java to display the the string Welcome to Java
on the console.
Note that the term string refers to any sequence of characters. In Java, a string must be enclosed in double quotation marks.
Finally, every statement must end with a semicolon ;
to terminate or end the statement.
Curly Braces {}
Note that curly braces are used in Java to form blocks that group the different parts of a Java program. Each block begins & ends with an opening {
and closing }
brace. Classes and methods each have their own blocks indicated by the curly braces. Note that blocks can be nested, or one block can be inside another block. i.e.:
Other Relevant Information
- In Java, there exist Reserved Keywords that have a unique meaning to the compiler, and so are reserved for specific purposes only; Reserved Keywords cannot be used in other circumstances. An example of a Reserved Keywords is
class
; if the compiler sees the wordclass
, it understands that what comes after the wordclass
is the name of the class. Other examples of Reserved Keywords includepublic
,static
, andvoid
. - Opening brace
{
must be matched by a closing brace}
- Java source programs are case sensitive. For example,
main
≠Main
. So usingMain
method would be wrong; the originalmain
method is correct.
Examples
3 Messages to Console
If you want to display 3 messages to the console, you can repeatedly use the System.out.println
statement 3 times.
public class WelcomeWithThreeMessages { public static void main(String[] args) { System.out.println("Programming is fun!"); System.out.println("Fundamentals first!"); System.out.println("Problem Driven insanity!") } }
Remember that each statement ends with a semi-colon ;
.
Computing an Expression
Know that with Java and almost any other programming language, you can perform mathematical computations and display the results to the console. For example, let’s say that you want Java to solve:
This is how the code ends up looking when you instruct Java to evaluate the mathematics:
public class ComputeExpression { public static void main(String[] args) { System.out.println((10.5 + 2 * 3)/(45 - 3.5); } }
Notice that the quotations are removed. When you want the statement System.out.println
to spit out verbatim what goes inside, you use Quotations ""
. If you want the statement System.out.println
to do whats inside, you don’t use Quotations ""
.