0% found this document useful (0 votes)
51 views

Java Manual

1. Java was created in 1991 by James Gosling at Sun Microsystems to make web pages interactive. It is an object-oriented programming language that is portable across platforms. 2. A Java program goes through three phases: writing the source code, compiling the code, and running the compiled code. The Java Development Kit (JDK) is required to compile and run Java programs. 3. Key aspects of Java include primitive data types, variables, operators, and outputting data. Variables are used to store data and must be declared with a data type and name. The System.out.println() method prints output to the console.

Uploaded by

klint
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Java Manual

1. Java was created in 1991 by James Gosling at Sun Microsystems to make web pages interactive. It is an object-oriented programming language that is portable across platforms. 2. A Java program goes through three phases: writing the source code, compiling the code, and running the compiled code. The Java Development Kit (JDK) is required to compile and run Java programs. 3. Key aspects of Java include primitive data types, variables, operators, and outputting data. Variables are used to store data and must be declared with a data type and name. The System.out.println() method prints output to the console.

Uploaded by

klint
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 1 Introduction To Java

Introduction to Java Programming


History
- An object oriented programming(OOP) language.
- Was created in 1991.
- By James Gosling of Sun Microsystems.
- Initially called Oak, in honor of the tree outside Gosling's window, its name was changed to Java
because there was already a language called Oak.
- Make Web pages interactive and alive.
- Java is a machine independent. Java programs are portable across different platforms.

Phases of Java Program:

Phases of a java Program:

1. Write
2. Compile
3. Run

Run the Java Program


A word processor such as Notepad or Notepad++ to type the Java program and save the file to compile
and run. Next, install the Java Development Kit (JDK) 1.6 (or later version) in your computer. Get a free copy of
this JDK from this website: http://www.sun.com. Download it and follow the instructions for proper installation.

Making a Batch File in MS-DOS


Create a batch file in the MS-DOS (Disk Operating System) mode to automate the compilation of Java
programs. Do this by typing the word cmd right at the Run menu item of the Start menu of the Windows
operating system to create this batch file. The complete steps:

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 Oriented Programming


Is a set of objects that work together in predefined ways to accomplish task.

Object – is a self-contained element of a computer program that represents a related group of features and is
designed to accomplish specific task.

Primitive Data Types


● The Java programming language defines eight primitive data types.

2
Chapter 1 Introduction To Java

– boolean (for logical)


– char (for textual)
– byte
– short
– int
– long (integral)
– double
– float (floating point).

● A character data type (char), represents a single Unicode character.


● It must have its literal enclosed in single quotes(’ ’).
For example, ‘a’ //The letter a‘\t’ //A tab
● To represent special characters like ' (single quotes) or" (double quotes), use the escape character \.
For example, '\'' //for single quotes '\"' //for double quotes

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.

Java Literals: Character


● To use a character literal, enclose the character in single quote delimiters.
● For example

3
Chapter 1 Introduction To Java

– the letter a, is represented as ‘a’.


– special characters such as a newline character, a backslash is used followed by the character
code.
For example, ‘\n’ for the newline character, ‘\r’ for the carriage return, ‘\b’ for backspace.
Java Literals: String
● String literals represent multiple characters and are enclosed by double quotes.
● An example of a string literal is, “Hello World”.

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.

Declaring and Initializing Variables


● Declare a variable as follows:
<data type> <name> [=initial value];
● Note: Values enclosed in <> are required values, while those values in [ ] are optional.

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

7 //declare a data type with variable name


8 // option and char data type
9 char option;
10 option = 'C'; //assign 'C' to option
11
12 //declare a data type with variable name
13 //grade, double data type and initialized
14 //to 0.0
15 double grade = 0.0;
16 }
17 }

Outputting Variable Data


● In order to output the value of a certain variable, we can use the following commands:
System.out.println()
System.out.print()

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

32 System.out.println(" x % y = " + (x % y));


33
34 //mixing types
35 System.out.println("Mixing types...");
36 System.out.println(" j + y = " + (j + y));
37 System.out.println(" i * x = " + (i * x));
38 }
39 }

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

Increment and Decrement Operators

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

You might also like