Java Programming
By
Mr. Silas Majyambere
University of Rwanda
College of Science and Technology
School of ICT
Computer Science
Year 2, Academic Year 2019-2020, Trimester 2
Chap1: Introduction
• Java is a high level programming language developed
by James Gosling from Sun Microsystem in 1995
• Java has a compiler (Javac) and Interpreter named Java
Virtual Machine(JVM)
• Java is a pure Object Oriented Programming (OOP)
language
• Java Runtime Environment(JRE) has required software
tools to run Java Programs
• JRE and JVM are components of Java Development
Kit(JDK) which has built-in classes and libraries called
packages
Year 2, Academic Year 2019-2020,
Trimester 2
Java Installation
• First we install JDK (1.8 will be installed)
• Java Integrated Development Environment
(IDE) for this Module we will use NetBeans 8
as Java IDE
• We then set environment variables correctly
• MySQL Server will installed in order to
connect Java Programs to Database
Year 2, Academic Year 2019-2020,
Trimester 2
Java Application
• Java2SE (Java 2 Standard Edition) is a computing
platform for development and deployment of small
scale software for desktop and server environment
• Java2EE (Java 2 Enterprise Edition) used for
developing, building and deploying Web-based
enterprise applications online. Java2EE is used to
develop large scale enterprise applications
(Distributed, Concurrent, Web)
• Java2ME(Java 2 Micro Edition) is Java programming
for hand held devices such as smart phones and
PDAs
Year 2, Academic Year 2019-2020,
Trimester 2
Main Components of Java
• Application Programming Interface (API), An API
includes classes, interfaces, packages and also
their methods, variables, and constructors.
• User defined classes
• Methods and Variables
• Interfaces
• Packages
• Frameworks
Year 2, Academic Year 2019-2020,
Trimester 2
Basics in Java Programming
Simple Java Program:
public class MyClass{
public static void main(String args[]){
System.out.println(“Hello World");
}
}
▪ Every Java program must have at least one class
▪ Execution of Java Program is done in main method
▪ Java is case sensitive: main=Main
▪ Our simple program displays Hello World on Screen
Year 2, Academic Year 2019-2020,
Trimester 2
Variables and Methods
• Variables are used to store values in memory
• There are 2 types of variables: Global and Local variables
• Global variables are declared before any method (even
main method) and are accessed throughout the whole
program
• Local variables are declared inside the method and are
accessed only the method in which they are declared
• A method defines a group of statements that can be
executed together
• There are 3 types of methods: Method with arguments,
Method without arguments and Constructor
Year 2, Academic Year 2019-2020,
Trimester 2
Example of Variables and Method without arguments
public class Mymethod{
public static int a,b;//glogal variables a and b
Public static void int sum() {
//Method without argument Variables and Methods that
are not defined in a User
a=5; defined class must be static
b=10; as they are accessed in main
int c=a+b;//local variable c method which is static
System.out.println("Sum="+c)
}
Public static void main(String args[]){
sum();
}
}
Year 2, Academic Year 2019-2020,
Trimester 2
Example of Method with arguments
public class MethodArg{
public static double c;
public static double quotient(doouble a, double b)
{
c=a/b;
System.out.println(“Quotient"+c); ▪ When data type of a method is not
return 0; void we must use return keyword,
Variable b should not be zero, we use
}
+ for addition and concatenation
public static void main(String args[]){
quotient(20.5,2.5)
}
}
Year 2, Academic Year 2019-2020,
Trimester 2
Compiling a Java Program
• A Java program is called Java Source Code
with a .java extension
• After compiling a Java Program we create a
.class file called Java Bytecode
• Java is a platform independent which means
Java bytecode can run on any patform
• The Java Bytecode is a binary file, to run it we
need JVM which can interpret it
Year 2, Academic Year 2019-2020,
Trimester 2