Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
245 views

Phase 1: Creating A Program: Editor Program Editor Source Code

Java was originally created for intelligent consumer devices but is now used widely for enterprise applications, web development, and mobile apps. It is platform independent, meaning code can run on any system with a Java Virtual Machine (JVM). The typical Java development process involves writing source code, compiling it to bytecode, loading the bytecode into memory, verifying it, and executing it on a JVM. Source code is written in text files with a .java extension using an editor or IDE. The javac compiler translates source code into bytecode files with a .class extension. The JVM loads class files and executes bytecode through interpretation and just-in-time compilation.

Uploaded by

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

Phase 1: Creating A Program: Editor Program Editor Source Code

Java was originally created for intelligent consumer devices but is now used widely for enterprise applications, web development, and mobile apps. It is platform independent, meaning code can run on any system with a Java Virtual Machine (JVM). The typical Java development process involves writing source code, compiling it to bytecode, loading the bytecode into memory, verifying it, and executing it on a JVM. Source code is written in text files with a .java extension using an editor or IDE. The javac compiler translates source code into bytecode files with a .class extension. The JVM loads class files and executes bytecode through interpretation and just-in-time compilation.

Uploaded by

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

Java

– Originally for intelligent consumer-electronic devices


– Then used for creating web pages with dynamic content
– Now also used to:
• Develop large-scale enterprise applications
• Enhance web server functionality
• Provide applications for consumer devices (cell phones, etc.)
Why Java?
 Java is a platform Independent: – Platform independent means java can run on any
computer irrespective to the hardware and software dependency. Means Java does not depend
on hardware means what type of processor, RAM etc. Java will run on a machine which will
satisfy its basic needs.

 Object Oriented: - In Java, everything is an Object. Java can be easily extended since it is
based on the Object model.

 Java Is a Secure Language: - With Java's secure feature, it enables to develop virus-free
systems.

 Portable: - Due to its Byte code is in Bytes, java program takes very less memory on hard
disk and therefore java technology is Portable also.

Typical Java Development Environment

Java programs go through five phases

Phase 1: Creating a Program

 Phase 1 consists of editing a file with an editor program (normally known simply as an
editor).
 You type a Java program (typically referred to as source code) using the editors (vi and
emacs for Linux and notepad for windows) make any necessary corrections and save the
program on a secondary storage device, such as your hard drive.
 A file name ending with the .java extension indicates that the file contains Java source
code.
 Integrated Development Environments(IDEs) provide tools that support the software
development process, including editors for writing and editing programs and debuggers for
locating logic errors.
 Popular IDEs include Eclipse (www.eclipse.org), NetBeans (www.netbeans.org), JBuilder
(www.borland.com), JCreator (www.jcreator.com), BlueJ (www.blueJ.org), jGRASP
(www.jgrasp.org) and jEdit (www.jedit.org).
Phase 2: Compiling a Java Program into Bytecodes

 In Phase 2, the programmer uses the command javac (the Java compiler) to compile a
Program.
 If the program compiles, the compiler produces a .class file called className.class that
contains the compiled version of the program.
 The Java compiler translates Java source code into bytecodes that represent the tasks to
execute in the execution phase (Phase 5).
 Bytecodes are executed by the Java Virtual Machine (JVM)—a part of the JDK and
the foundation of the Java platform.
 A Virtual Machine (VM) is a software application that simulates a computer, but
hides the under-lying operating system and hardware from the programs that interact
with the VM.
 If the same VM is implemented on many computer platforms, applications that it
executes can be used on all those platforms. The JVM is one of the most widely used
virtual machines.
 Unlike machine language, which is dependent on specific computer hardware,
bytecodes are platform-independent instructions—they are not dependent on a
particular hardware platform.
 So Java’s bytecodes are portable—that is, the same bytecodes can execute on any
platform containing a JVM that understands the version of Java in which the bytecodes
were compiled.
 The JVM is invoked by the java command. For example, to execute a Java application
called Welcome, you would type the command java Welcome

Phase 3: Loading a Program into Memory

 In Phase 3, the program must be placed in memory before it can execute—known as


loading.
 The class loader takes the .class files containing the program’s bytecodes and transfers
them to primary memory.
 The class loader also loads any of the .class files provided by Java that your program
uses.
 The .class files can be loaded from a disk on your system or over a network.

Phase 4: Bytecode Verification


 In Phase 4, as the classes are loaded, the bytecode verifier examines their bytecodes to
ensure that they are valid and do not violate Java’s security restrictions.
 Java enforces strong security, to make sure that Java programs arriving over the
network do not damage your files or your system (as computer viruses and worms
might).
Phase 5: Execution
 In Phase 5, the JVM executes the program’s bytecodes, thus performing the actions
specified by the program.
 In early Java versions, the JVM was simply an interpreter for Java bytecodes.
 This caused most Java programs to execute slowly because the JVM would interpret
and execute one bytecode at a time.
 Today’s JVMs typically execute bytecodes using a combination of interpretation and so
called just-in-time (JIT) compilation.
 In this process, The JVM analyzes the bytecodes as they are interpreted, searching for
hotspots-parts of the bytecodes that execute frequently.
 For these parts, a just-in-time (JIT) compiler- known as the Java Hotspot compiler-
translates the bytecodes into the underlying computer’s machine language.

Fundamental Programming Structures in Java


1 // Demonstrating basics java programming structure
2 // Text-printing program.
3 public class Welcome1
5{
6 // main method begins execution of Java application
7 public static void main (String args [])
8 {
9 System.out.println(“Welcome to Java Programming!”);
10 } // end method main
11
12 } // end class Welcome1
Line 1://
 begins with //, indicating that the remainder of the line is a comment.
 Programmers insert comments to document programs and improve their readability.
 This helps other people read and understand your programs. The Java compiler ignores
comments, so they do not cause the computer to perform any action when the program is
run.
 Two form of comments:
 End-of-line/ single line: comment terminates at the end of the line on which it appears.
 Traditional comments/multiple-line comments: This type of comment begins with the
delimiter /* and ends with */. All text between the delimiters is ignored by the compiler.

Line 3: public class Welcome1

 begins a class declaration for class Welcome1.


 Every program in Java consists of at least one class declaration that is defined by you—
the programmer.
 These are known as programmer-defined classes or user-defined classes.
 The class keyword introduces a class declaration
 By convention, all class names in Java begin with a capital letter and capitalize the first
letter of each word they include (e.g., SampleClassName).
 A Java class name is an identifier—a series of characters consisting of letters, digits,
underscores (_ ) and dollar signs ($) that does not begin with a digit and does not
contain spaces.
 Java is case sensitive—that is, uppercase and lowercase letters are distinct, so a1 and
A1 are different (but both valid) identifiers.

Line 7: public static void main( String args[] )


 It is the starting point of every Java application.
 The parentheses after the identifier main indicate that it is a program building block
called a method.
 Java class declarations normally contain one or more methods.
 For a Java application, exactly one of the methods must be called main and must be
defined; otherwise, the JVM will not execute the application.
 Methods are able to perform tasks and return information when they complete their
tasks.
 Keyword void indicates that this method will perform a task but will not return any
information when it completes its task.
 The String args [] in parentheses is a required part of the method main’s declaration.

Line 9: System.out.println(“Welcome to Java Programming!" );

 instructs the computer to perform an action—namely, to print the string of


characters contained between the double quotation marks (but not the quotation
marks themselves).
 A string is sometimes called a character string, a message or a string literal.
 We refer to characters between double quotation marks simply as strings.
 White-space characters in strings are not ignored by the compiler.
 System.out is known as the standard output object. System.out allows Java
applications to display sets of characters in the command window from which the
Java application executes.
 Method System.out.println displays (or prints) a line of text in the command
window.
 Method System.out.println performs its task by displaying (also called outputting) its
argument in the command window.
 The entire line 9, including System.out.println, the argument "Welcome to Java
Programming!" in the parentheses and the semicolon (;), is called a statement.
 Each statement ends with a semicolon.
Primitive Data Type
 Primitive Types, lists the eight primitive types in Java.
 Like its predecessor languages C and C++, Java requires all variables to have a type.
 For this reason, Java is referred to as a strongly typed language.
 In C and C++, programmers frequently have to write separate versions of programs to support
different computer platforms, because the primitive types are not guaranteed to be identical from
computer to computer.
 For example, an int value on one machine might be represented by 16 bits (2 bytes) of memory,
and on another machine by 32 bits (4 bytes) of memory. In Java, int values are always 32 bits (4
bytes).

Figure 1:primitive Data type

Arithmetic operators
 Most programs perform arithmetic calculations.
 The arithmetic operators are binary operators because they each operate on two operands.
 Arithmetic expressions in Java must be written in straight-line form to facilitate entering
programs into the computer.
 All constants, variables and operators appear in a straight line (e.g. a / b).
Figure 2: Arithmetic operators

Rules of Operator Precedence

 Java applies the operators in arithmetic expressions in a precise sequence determined by the
following rules of operator precedence:
1. Multiplication, division and remainder operations are applied first. If an expression
contains several such operations, the operators are applied from left to right.
Multiplication, division and remainder operators have the same level of precedence.
2. Addition and subtraction operations are applied next. If an expression contains several
such operations, the operators are applied from left to right. Addition and subtraction
operators have the same level of precedence.

Decision Making: Equality and Relational Operators


 A condition is an expression that can be either true or false.
 This section introduces Java’s if statement that allows a program to make a decision based on a
condition’s value.
 If the condition is false, the body does not execute.
 Conditions in if statements can be formed by using the equality operators (== and !=) and
relational operators (>, <, >= and <=).
 Both equality operators have the same level of precedence, which is lower than that of the
relational operators.
 The equality operators associate from left to right.
 The relational operators all have the same level of precedence and also associate from left to
right.

Figure 3: Equality operators part


Figure 4: Relational operators

Array

 Arrays are data structures consisting of related data items of the same type.
 Arrays are fixed-length entities—they remain the same length once they are created, although an
array variable may be reassigned such that it refers to a new array of a different length.
 An array is a group of variables (called elements or components) containing values that all have
the same type.

Declaring and Creating Arrays


 Array objects occupy space in memory.
 Like other objects, arrays are created with keyword new.
 To create an array object, the programmer specifies the type of the array elements and the number
of elements as part of an array-creation expression that uses keyword new.
 Such an expression returns a reference that can be stored in an array variable.
 The following declaration and array-creation expression create an array object containing 12 int
elements and store the array’s reference in variable c:
int c[] = new int[ 12 ];//or
int c[]; // declare the array variable
c = new int [ 12 ]; // create the array; assign to array variable

Using an Array Initializer


 A program can create an array and initialize its elements with an array initializer, which is a
comma-separated list of expressions (called an initializer list) enclosed in braces ({and}); the
array length is determined by the number of elements in the initializer list. For example, the
declaration
int n[] = { 10, 20, 30, 40, 50 };
Control Structure
 Sequential execution: - statements in a program are executed one after the other in the order in
which they’re written.
 Transfer of control: - enable you to specify that the next statement to execute is not necessarily
the next one in sequence.
 Java has only three kinds of control structures, which from this point forward we refer to as
control statements: the sequence statement, selection statements (three types: if statement-
single selection statement, if…else statement—double selection statement and switch statement
—multiple selection statement) and repetition statements/looping statements (three types: for,
do…while and while statements).

Selection Control Statements


1. If single-selection statements
 The if statement is a single-entry/single-exit control statement.
 if statement either performs (selects) an action, if a condition is true, or skips it, if the
condition is false.
if(condition) {
//body
}

2. if…else Double-Selection Statement


 The if…else double-selection statement allows you to specify an action to perform when the
condition is true and a different action when the condition is false.
If(condition) {
//body for first option if the condition is true
} else {
//default body for the first action is false
}

Conditional Operator (?:)


 Java provides the conditional operator (?:) that can be used in place of an if…else
statement.
 This is Java’s only ternary operator (operator that takes three operands).
 Together, the operands and the ?: symbol form a conditional expression.
 The first operand (to the left of the ?) is a boolean expression (i.e., a condition that evaluates
to a Boolean value—true or false), the second operand (between the ? and :) is the value of
the conditional expression if the boolean expression is true and the third operand (to the right
of the :) is the value of the conditional expression if the Boolean expression evaluates to
false.
e.g. System.out.println(studentGrade >= 60 ? "Passed" : "Failed”);

Nested if…else Statements


 A program can test multiple cases by placing if…else statements inside other if…else
statements to create nested if…else statements.
e.g.
if (studentGrade >= 90)
System.out.println("A");
else
if (studentGrade >= 80)
System.out.println("B");
else
if (studentGrade >= 70)
System.out.println("C");
else
if (studentGrade >= 60)
System.out.println("D");
else
System.out.println("F");

3. Switch Multiple-Selection Statement


 The switch statement is called a multiple-selection statement because it selects among
many different actions (or groups of actions).
e.g.
switch(courseCode) {
case "SE 2031":
System.out.println("Object Oriented Programming");
break;
case "xx 3456":
System.out.println("System Admin.");
break;
default:
System.out.println("Unknown");
}

Repetition Statements in Java


 Java provides three repetition statements (also called looping statements) that enable
programs to perform statements repeatedly as long as a condition (called the loop-
continuation condition) remains true.
 The repetition statements are the while, do…while and for statements.
 The while and for statements perform the action (or group of actions) in their bodies zero or
more times—if the loop-continuation condition is initially false, the action (or group of
actions) will not execute.
 The do…while statement performs the action (or group of actions) in its body one or more
times.

You might also like