Java Manual
Java Manual
1. Write
2. Compile
3. Run
1. First, type the word cmd at the Run menu item of the Start menu.
2. Then, at the C:\> prompt, create a subdirectory named javaprog by typing the following syntax:
C:\> md javaprog (then press the Enter key)
The md here means make/create directory.
3. Now type the command edit. This command will open the text-mode editor of MS-DOS (or use Notepad).
Next, type the following batch file code below:
rem This is a java path configuration.
1
Chapter 1 Introduction To Java
rem Activate this batch file before you create java programs.
@echo Java path is now activated!
@echo off
cls
path=c:\winnt;d:\winnt32;c:\Program
Files\Java\jdk1.6.0_01\bin;c:\Program
Files\Java\jdk1.6.0_01\lib;c:\Program Files\Java\jdk1.6.0_01\jre;
@echo off
4. Make sure that the entire path=c:\ should be in one line only to prevent a syntax error in batch file processing.
Because of the limitation of the space here in this book, our path=c:\ is displayed in 4 lines of code, which will
probably cause a syntax error in jpath file batch processing.
5. Then save it with the filename called jpath.bat. The filename extension .bat means it is a batch file. The rem is
a batch file command which means “ a code remark”, while the command echo means to display the message on
the screen. The cls stands for clear screen. And finally the path command in our batch file is to simply instruct the
computer to look for the Java compiler and its associated libraries (which are needed in the compilation of our
programs) in the listed directories. To check if the jpath batch file is working properly, you can issue the MS-DOS
command such as the following syntax:
C:\JAVAPROG>path (then press the Enter key)
You need to see that the directory path looks like the following line:
C:\JAVAPROG>path
path=c:\winnt;d:\winnt32;c:\Program
Files\Java\jdk1.6.0_01\bin;c:\Program
Files\Java\jdk1.6.0_01\lib;c:\Program Files\Java\jdk1.6.0_01\jre;
In case the jpath batch file will not work as it is intended, you have to manually type the path
command at the MS-DOS prompt like the following:
C:\JAVAPROG> path=c:\winnt;d:\winnt32;c:\Program
Files\Java\jdk1.6.0_01\bin;c:\Program
Files\Java\jdk1.6.0_01\lib;c:\Program Files\Java\jdk1.6.0_01\jre;
(then press the Enter key)
Then you can check again if the manual way works; by issuing the path command again with the
following syntax:
C:\JAVAPROG>path
The Three Files Are Needed
To successfully run our Java applet program, we need the three files. The first one is the source code
which filename extension is .java then the second one is the compiled code which filename extension is .class.
And the last one is the HTML (Hyper-Text Markup Language) file which filename extension is .htm (where our
applet runs).
Object – is a self-contained element of a computer program that represents a related group of features and is
designed to accomplish specific task.
2
Chapter 1 Introduction To Java
Java Identifiers
● Identifiers
– are tokens that represent names of variables, methods, classes, etc.
– Examples of identifiers are: Hello, main, System, out.
● Java identifiers are case-sensitive.
– This means that the identifier Hello is not the same as hello.
Java Keywords
● Keywords are predefined identifiers reserved by Java for a specific purpose.
● You cannot use keywords as names for your variables, classes, methods ... etc.
● The next line contains the list of the Java Keywords.
3
Chapter 1 Introduction To Java
Sample Program:
public class Welcome {
// Program start here.
public static void main(String args[]) {
System.out.println(" Welcome to Java-Programming! ");
}
}
Basic procedure to run the java program at DOS prompt:
1. Save the program as “Welcome.java”
2. Compile: “javac Welcome.java”
3. File created without error: Welcome.class
4. Running the program, command: “java Welcome” then press Enter key.
OUTPUT:
Welcome to Java-Programming!
Variables
● A variable is an item of data used to store the state of objects.
● A variable has a: – data type
● The data type indicates the type of value that the variable can hold. – name
● The variable name must follow rules for identifiers.
Sample Program
1 public class VariableSamples {
2 public static void main( String[] args ){
3 //declare a data type with variable name
4 // result and boolean data type
5 boolean result;
6
4
Chapter 1 Introduction To Java
Sample Program
1 public class OutputVariable {
2 public static void main( String[] args ){
3 int value = 10;
4 char x;
5 x = ‘A’;
6
7 System.out.println( value );
8 System.out.println( “The value of x=“ + x );
9 }
10 }
The program will output the following text on screen:
10
The value of x=A
Operators
● Different types of operators:
– arithmetic operators
– relational operators
– logical operators
– conditional operators
● These operators follow a certain kind of precedence so that the compiler will know which operator to
evaluate
first in case multiple operators are used in one statement.
5
Chapter 1 Introduction To Java
Arithmetic Operators
Sample Program
1 public class ArithmeticDemo {
2 public static void main(String[] args){
3 //a few numbers
4 int i = 37;
5 int j = 42;
6 double x = 27.475;
7 double y = 7.22;
8 System.out.println("Variable values...");
9 System.out.println(" i = " + i);
10 System.out.println(" j = " + j);
11 System.out.println(" x = " + x);
12 System.out.println(" y = " + y);
System.out.println("Adding...");
13 System.out.println(" i + j = " + (i + j));
14 System.out.println(" x + y = " + (x + y));
15 //subtracting numbers
16 System.out.println("Subtracting...");
17 System.out.println(" i - j = " + (i – j));
18 System.out.println(" x - y = " + (x – y));
19 //multiplying numbers
20 System.out.println("Multiplying...");
21 System.out.println(" i * j = " + (i * j));
22 System.out.println(" x * y = " + (x * y));
23
24 //dividing numbers
25 System.out.println("Dividing...");
26 System.out.println(" i / j = " + (i / j));
27 System.out.println(" x / y = " + (x / y));
28 //computing the remainder resulting from dividing
29 // numbers
30 System.out.println("Computing the remainder...");
31 System.out.println(" i % j = " + (i % j));
6
Chapter 1 Introduction To Java
Program Output
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i/j=0
x / y = 3.8054 Computing
the remainder...
i % j = 37
x % y = 5.815
Mixing types...
j + y = 49.22
i * x = 1016.58
7
Chapter 1 Introduction To Java
Relational Operators
● Relational operators compare two values and determines the relationship between those values.
● The output of evaluation are the boolean values true or false.
Logical Operators
● Logical operators have one or two boolean operands that yield a boolean result.
● There are six logical operators:
– && (logical AND)
– & (boolean logical AND)
– || (logical OR)
– | (boolean logical inclusive OR)
– ^ (boolean logical exclusive OR)
– ! (logical NOT)
Java’s logical operators enable you to form more complex conditions by combining simple conditions.
The logical operators are && (conditional AND), || (conditional OR), & (boolean logical AND), | (boolean logical
inclusive OR), ^ (boolean logical exclusive OR) and ! (logical NOT).
Sample Program:
8
Chapter 1 Introduction To Java
1 // ContinueTest.java
2 // continue statement terminating an iteration of a for statement.
3 public class ContinueTest
4{
5 public static void main( String[] args )
6{
7 for ( int count = 1;count<= 10;count++) // loop 10 times
8{
9 if ( count == 5 ) // if count is 5,
10
11
12 System.out.printf( "%d ", count );
13 } // end for
14
15 System.out.println( "\nUsed continue to skip printing 5" );
16 } // end main
17 } // end class ContinueTest
Output:
1 2 3 4 6 7 8 9 10
Used continue to skip printing 5
9
Chapter 1 Introduction To Java
10