JAVA TUTORIAL
I would recommend downloading, installing and using eclipse.
Click to download eclipse now!
The following is the Hello World Application as written in Java. Type it into a text file or
copy it out of your web browser, and save it as a file named HelloWorld.java.
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World!");
}
}
|
Congratulations! You've just written your first Java program!
Comments
Comments can appear anywhere in a source file. Comments are identical to those in C and C++.
Everything between /* and */ is ignored by the compiler and everything on a line after two
consecutive slashes is also thrown away. Therefore the following program is, as far as the
compiler is concerned, identical to the first one:
// This is the Hello World program in Java
class HelloWorld {
public static void main (String args[]) {
/* Now let's print the line Hello World */
System.out.println("Hello World");
}
}
|
Data and Variables
Methods are only half of a Java class. The other half is data. Consider the following
generalization of the HelloWorld program:
// This is the Hello Rusty program in Java
class HelloRusty {
public static void main (String args[]) {
// You may feel free to replace "Rusty" with your own name
String name = "Rusty";
/* Now let's say hello */
System.out.print("Hello ");
System.out.println(name);
}
}
|
The If statement
The if (in the glossary) statement enables your program to selectively execute
other statements, based on some criteria. For instance you stick your hand out the window
to test if it's raining. If it is raining then you take an umbrella with you.
If it isn't raining then you don't.
// testing if
class TestIf{
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
}
}
|
The Else statement
What if you want to perform a different set of statements if the expression is false?
You can use the else (in the glossary) statement for that. Consider another example:
Suppose your program needs to perform different actions depending on whether the user
clicks the OK button or another button in an alert window. The program could do this
by using an if statement along with an else statement.
public class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
|
Variables
An object stores its state in variables.Definition: A variable (in the glossary) is
an item of data named by an identifier. You must explicitly provide a name and a type
for each variable you want to use in your program.
public class MaxVariablesDemo {
public static void main(String args[]) {
//integers
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;
//real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble = Double.MAX_VALUE;
//other primitive types
char aChar = 'S';
boolean aBoolean = true;
//Display them all.
System.out.println("The largest byte value is "
+ largestByte + ".");
System.out.println("The largest short value is "
+ largestShort + ".");
System.out.println("The largest integer value is "
+ largestInteger + ".");
System.out.println("The largest long value is "
+ largestLong + ".");
System.out.println("The largest float value is "
+ largestFloat + ".");
System.out.println("The largest double value is "
+ largestDouble + ".");
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar
+ " is uppercase.");
} else {
System.out.println("The character " + aChar
+ " is lowercase.");
}
System.out.println("The value of aBoolean is "
+ aBoolean + ".");
}
}
|