0% found this document useful (0 votes)
62 views8 pages

Java

Java is a high-level programming language that is compiled into bytecode that runs on a Java virtual machine (JVM). Java code is written in .java files and compiled into .class files containing bytecode. The JVM interprets the bytecode and executes the program on the underlying hardware platform. This allows Java code to run on any system with a JVM, fulfilling its "write once, run anywhere" principle. Java uses object-oriented programming and features like garbage collection. It can be used to create both applets for web browsers and standalone applications.

Uploaded by

Colen LUMAMBA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
62 views8 pages

Java

Java is a high-level programming language that is compiled into bytecode that runs on a Java virtual machine (JVM). Java code is written in .java files and compiled into .class files containing bytecode. The JVM interprets the bytecode and executes the program on the underlying hardware platform. This allows Java code to run on any system with a JVM, fulfilling its "write once, run anywhere" principle. Java uses object-oriented programming and features like garbage collection. It can be used to create both applets for web browsers and standalone applications.

Uploaded by

Colen LUMAMBA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

JAVA

What is Java?

 Java is a high-level programming language developed by James Gosling (Sun Microsystems)


 The Java language is a C-language derivative, so its syntax rules look much like C's

The Java compiler


 When you program for the Java platform, you write source code in .java files and then compile them. The compiler
checks your code against the language's syntax rules, then writes out bytecodes in .class files. Bytecodes are
standard instructions targeted to run on a Java virtual machine (JVM).
The JVM
 At run time, the JVM reads and interprets .class files and executes the program's instructions on the native
hardware platform for which the JVM was written. The JVM interprets the byte codes just as a CPU would interpret
assembly-language instructions. The difference is that the JVM is a piece of software written specifically for a
particular platform.
 The JVM is the heart of the Java language's "write-once, run-anywhere" principle.
 Your code can run on any chipset for which a suitable JVM implementation is available. JVMs are available for
major platforms like
 Linux and Windows, and subsets of the Java language have been implemented in JVMs for mobile phones and
hobbyist chips.
Why Java?
 “Most popular” language
 Runs on a “virtual machine” (JVM)
Features of Java
 Object-Oriented – uses classes to organize code
 Portable and Platform Independent
 Multithreaded
 Garbage Collected
Applets and Applications
Java can be used to create two types of program:
 Applet
 special applications designed to run within the context of a web browser
 Application
 Stand-alone program that does not need a browser to run
 A minimum Java program has the following format:
public class <classname>{
public static void main(String[] args){
<program statement>
}
}
1. The items in angle brackets are place holders. These must be replaced with whatever is appropriate for the
program (without the angle brackets).
a. The <classname> is an identifier for the program which should be the same as the filename.
b. The <program statement> is a series of Java statements.
2. There is a minimum of 2 pairs of curly braces – one after <classname> surrounding everything else, and another
surrounding the <program statements>.
3. args is a variable which may be renamed according to java rules on variable names. At this point, just use it as is.
Java Coding Guidelines
Below are guidelines to be followed when coding in java.
 java program files must
o Have same name as public class.
o End with extension .java.
 Use comments for documentation and for readability.
 white spaces are ignored
 indent for readability.
 Java Statements
A statement is one or more lines of code terminated by a semicolon. Example:
System.out.print(“Hello, World”);
Note: This is the default output statement.
 Java Sample Program
public class Hello{
public static void main(String[] args){
System.out.println(“Hello World!!”);
}
}
Java Comments
- A comment is an optional statements used to describe what a program or a line of program is doing.
Ex:
//this is a comment - single-line comment
/*this is a comment*/ - multi-line
/** documentation */ - Javadoc
Java Variable
- A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a
data type.
- a variable could be declared to use one of the eight primitive data types.
- every variable must be given an initial value before it can be used.
Ex:
<data type> <variable name> [=initial value];
int a= 9;
int x;
Note: values enclosed in <> are required values, while those values in [] are optional.
Rules
- variables can use alphabetic characters of either case (a-z and A-Z), numbers (0-9), underscores(_), and dollar
signs($).
- Variables cannot start with a number.
- Keywords cannot be used as variable names(keywords is sometimes called reserved words).
Guidelines
- Name your variable close to its functionality.
- For multi-word variable, either use underscores to separate the words, or capitalize the start to each word.
- Avoid starting the variables using the underscore.
- Variables are distinct or unique.
Data Type
 A data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean,
stating the possible values for that type, the operations that can be done on that type, and the way the values of
that type are stored.
 A variable's data type determines the values it may contain, plus the operations that may be performed on it.
Primitive Data Types
 Building blocks and are an essential part of a program. Without these data types it would be impossible for a
programmer to store pertinent data necessary in the completion of the program.
Java Primitive Data Type

 boolean(true or false)-a boolean is not a number and cannot be converted to a number or any other type.
 char(characters)-represents text characters or unicode characters.
 byte-represents short numbers from -128 – 127 only.
 short-ranges from -32768 - 32767
 int(integer)-numbers without fractional parts. Negative values are allowed ranges from -2,147,483,648 -
2,147,483,647.
 long-enhance version of integer. Ranges from 9,223,372,036,854,775,808 - 9,223,372,036,854,775,807.
 float(floating point or fraction)- numbers with decimal points.
 double-mostly used than the float data type, since these numbers have twice the precision of the float type.
Normal Form Compressed Form
sum = sum + 1 sum+=2
p=p–1 p -=1
r = r /4 r /=4
j=j*b j*=b
n = n% 2 n%=2
n=n+1 n++
a=a–1 a—

Relational Operator

&& Conditional-AND

|| Conditional-OR

! Not equal (<>)


Logical Operator

Conditional Statements

 If statement
 If else
 If else if
 switch
If statement

 If the boolean expression evaluates to true then the block of code inside the if statement will be executed. If not
the first set of code after the end of the if statement(after the closing curly brace) will be executed.

37
Programming Logic

What is Computer?

• Is an electronic device that accepts input, processes data, store data and produces output.

Program

• A computer program is a collection of instructions that performs a specific task when executed by a computer.

• A computer requires programs to function, and typically executes the program's instructions in a central
processing unit.

Programming

• Programming is the process of translating a problem’s solution into instructions that a computer can process.

• Software Life Cycle:

– Planning

– Analysis

– Design

– Implementation

– Maintenance

Software Life Cycle

Planning

• Defining the problem

• Identifying the input and expected output

Analysis

• Identifying solutions

• Formulate algorithm -A step by step instruction

Software Life Cycle

Design

• Design the solution base on the requirements defined in analysis

Implementation

• the system is built, tested and installed


Maintenance

• Keep the system running

Algorithm

• Example

 Recipe for baking a cake

 Instruction in a shampoo

• Pseudocode: The steps of the algorithm are written in English in a shorthand form which represents the flow of
control through the algorithm.

• Flowchart: A graphical representation of the instructions

Flowchart Symbols

Terminator Block
Flow lines

Input/Output Block
Programming Structure

1. Sequential Structure

2. Conditional Structure

3. Looping Structure

Conditional

• If

– Executes the statement when the condition is true

• If else

– Executes the statement if the condition is True/False

• If else if

You might also like