INTRODUCTION TO JAVA
WEEK 1
Introduction to Java
Java is originally designed for programming consumer devices , but it was first successfully used to write applets.
Compilers are programs that translate a program written in a
high-level language into an equivalent machine language.
In case of Java, this machine language is byte code.
To run a Java program on a computer, the program must first be
translated into an intermediate language called
bytecode and then interpreted into a particular machine language.
Unlike machine-language instructions, or machine code, Java
bytecode is not tied to any particular operating systems or
CPU. All we need to run the same Java programs on the different operating
systems is the Java interpreters for the desired
operating systems. Currently, there are Java interpreters for Windows, Mac,
Unix and other operating systems. A Java
interpreter is also called a Java Virtual Machine (JVM) because it is
like a virtual machine that executes bytecode, whereas a
CPU is a real machine that executes machine code.
Programming Process with Problem analysis-Algorithm- Coding and Execution Cycle
Analyze the problem and outline the problem and its solution requirements
Design an algorithm to solve the problem
Implement the algorithm in a programming language, such as Java (create a program.)
Verify that the algorithm works. (compile -run- test)
Maintain the program by using and improving it , and modifying it if the problem domain changes.
Processing of a Java Program
Use a text editor create a program (source code.)
Compile the program - translate the program into the equivalent bytecode and check for syntax errors.
If no error is found, the compiler translates the program into
bytecode. otherwise, go to step 1 edit the program.
to fix the error.
The loader connects the bytecode for classes used in the
program with the program's bytecode. The loader also loaded
the complete program's bytecode into main memory.
The interpreter translates each bytecode instruction into your computer's machine language, and then executes it.
The basic structure of a Java console application
public class test
{
public static void main (string[] args)
{
Write coding
}
}
The file name must be identical to the class name.
In this case the file name you type is test.
The actual file name is test.java. All java files has an extension of java.
A simple Java program
public class Test
{ //This program displays a message
public static void main(String[] args)
{
System.out.println("I love Java!!!");
}//end main
}//end class
Program analysis
public class Test is the class definition. Every Java program must contain at least one programmed-defined class definition.
The term class is a Java keyword and must be followed by the name of the class.
The Java keyword public will be explained later in the class. For now, we always create our class definitions with the keyword public.
public static void main (String [] args) is a method definition line for the Test class's main method.
A method is a block of code that performs certain tasks.
System.out.println is a Java command that that instructs the computer to write a string of text enclosed in quotation marks to the command window.
Comment statement is used to explain in more details in the
program. The comment statement is not compiled by the compiler. It usually
precede by //
/* comment */
// one comment per line
/* ................*/ comments on multiple lines
From the source code to running the program
EditoràSource
code àCompileràClass
files (byte code) machine languageàInterpreter
à Output (Running Program)
Compiler: translates a programs written in a high-level language into
machine code and identify the syntax errors.
Java compiler used in the class is called Dr. Java
Edit , Compile and Test Process
Create or edit the program
Compile the program. If the program has syntax error(s), go to step 2 to edit and fix the error(s).
Run, execute or test the program . If the program has logical error(s) or run-time errors, go to step1.