Setup:› I will continue to add to this article over the coming weeks.
You'll need to download and install two things to get started coding:
- Java SE 6 JDK (Java Platform, Standard Edition 6 Development Kit)
- Choose your operating system and platform and download the JDK version, not the JRE (only the runtime environment) - Eclipse IDE (Integrated Development Environment)
This is the interface through which you will be programming, compiling, and running and testing code
This simple program will output the text, "Hello, World!" (without quotes).
The program is composed of a class definition named HelloWorld. The HelloWorld class runs from the first opening brace ({) to the last closing brace (}). All Java programs use class definitions.
The next portion of code sets up the main method. Methods define the operations that an object or program will perform. The code in a method lives inside another set of braces ({}) In this case, the program is simply printing a line that says "Hello, World!" using the println (print line) method of the out object, which is stored in Java's predefined System class.
That's it! This Java program contains just three simple steps:public class HelloWorld{public static void main (String[] args)}System.out.println ("Hello, World!");
}}
- Define a class named HelloWorld. Its code is included between two braces
- Define a method named main that has a few settings that aren't really important to know about yet, but can be explained a bit further in this Java tutorial. The code between the method's braces will be instructions to be performed by the program
- Call the println method from the System class's out object to write "Hello, World!" as output. This instruction is ended with a semicolon (;)
Comments:
Comments are an efficient and important way to provide documentation and insight as to what your program is doing at any given point in the code. Be sure to include detailed descriptions to help you or another programmer when returning to review or revise the code at a later time.
// Single-line comment
// This can be attached after some code on a line.
// Anything after these slashes on the same line
// will be omitted
/* A comment spanning several lines:
EVERYTHING between these comment marks will
be omitted by the Java interpreter */
// This can be attached after some code on a line.
// Anything after these slashes on the same line
// will be omitted
/* A comment spanning several lines:
EVERYTHING between these comment marks will
be omitted by the Java interpreter */
Variables & Constants:
Variables and constants are places in code to store information for the program to use and reference. Variables are dynamic; in that once they are created they can remain blank or equal to zero, or they can hold text or numeric information or be set to true or false. Examples include any kind user provided data, an e-mail address, age, phone number, number of stars given on a restaurant or movie review, a program iteration number, amount of records in a database, etc. Further, they can be changed at any time, or erased and set back to their original "blank" or "null" state.
Constants, on the other hand, can also hold data, though these will be set once when the program begins and will remain the same throughout the program's life. Examples include the number pi, thresholds (1 millionth customer at a store, the upper limit of $1.00 on the Price is Right Showcase Showdown), freezing/boiling points of water in °F/°C/°K, etc.
Identifiers: are variables, constants, reserve words, etc.
- Alphanumeric, underscores (_), and dollar signs ($)
- Case sensitive
- Can be any length
- Must not begin with a number
int_Value, String3, $LakeSize, _StreamSize, $100, a_very_long_stupid_string_that_should_be_shortened
Unacceptable:
int-Value, 3rdString, Lake Size, _Stream#Size...
Escape Sequences:
This is a list of special characters that can be used in a program which would otherwise cause a compile-time, run-time, or logic error if entered directly. For instance, entering """ (a string containing a double quote character) will cause a compile-time error because the the middle quotation mark will be interpreted as the end of a blank string. Instead the code would need to be entered as "\"".
Sequence | Output |
\b | backspace |
\t | tab |
\n | newline |
\r | carriage return |
\" | double quote |
\' | single quote |
\\ | backslash |
Conditional Operators:
Equality / Relational | |
Operator | Value |
== | equal to |
!= | not equal to |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
Logical | ||
Operator | Value | Use |
! | NOT | ! a |
&& | AND | x && y |
|| | OR | x || y |
Resources:
Java Foundations, 2nd Edition John Lewis, et. al |