Sei sulla pagina 1di 38

INTRODUCTION TO

JAVA
PROGRAMMING

CC102- Programming Fundamentals


LESSON I – INTRODUCTION TO JAVA PROGRAMMING

LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
1. Can narrate the key highlights behind JAVA
programming
2. Can distinguish the features of java.
3. Familiarize on the JAVA programming environment

CC102- PROGRAMMING FUNDAMENTALS


2
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java?

• was created in 1991


• by James Gosling ,
Mike Sheridan
Patrick Naughton
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
CC102- PROGRAMMING FUNDAMENTALS
3
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

the first projects developed using Java


• a personal hand-held
remote control named:
• Star 7
• was part of the:
• Green Project

CC102- PROGRAMMING FUNDAMENTALS


4
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

JAVA Versions
• JDK Alpha and Beta (1995) • Java SE 7
• JDK 1.0 (23rd Jan, 1996) (28th July, 2011)
• JDK 1.1 (19th Feb, 1997) • Java SE 8
(18th March, 2014)
• J2SE 1.2 (8th Dec, 1998)
• Java SE 9
• J2SE 1.3 (8th May, 2000) (21st Sep, 2017)
• J2SE 1.4 (6th Feb, 2002) • Java SE 10
• J2SE 5.0 (30th Sep, 2004) (20th March, 2018)
• Java SE 6 (11th Dec, 2006)

CC102- PROGRAMMING FUNDAMENTALS


5
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– A programming language
– A development environment
– An application environment
– A deployment environment

CC102- PROGRAMMING FUNDAMENTALS


6
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– A programming language
As a programming language, Java can create all
kinds of applications that you could create using
any conventional programming language.

CC102- PROGRAMMING FUNDAMENTALS


7
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– A Development Environment
As a development environment, Java
technology provides you with a large suite of
tools:
A compiler
An interpreter
A documentation generator
A class file packaging tool and so on...

CC102- PROGRAMMING FUNDAMENTALS


8
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– An Application and Runtime Environment
Java technology applications are typically
general-purpose programs that run on any
machine where the Java Runtime
Environment (JRE) is installed

CC102- PROGRAMMING FUNDAMENTALS


9
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– An Application and Runtime Environment
There are two main deployment environments:
1. The JRE supplied by the Java 2 Software
Development Kit (SDK) contains the complete
set of class files for all the Java technology
packages, which includes basic language
classes, GUI component classes, and so on.

CC102- PROGRAMMING FUNDAMENTALS


10
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– An Application and Runtime Environment
There are two main deployment environments:
2. The other main deployment environment is
on your web browser. Most commercial
browsers supply a Java technology interpreter
and runtime environment.

CC102- PROGRAMMING FUNDAMENTALS


11
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– The Java Virtual Machine
– Garbage Collection
– Code Security
– Platform Independence
– Object Oriented

CC102- PROGRAMMING FUNDAMENTALS


12
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– The Java Virtual Machine
Java Virtual Machine (JVM)
– an imaginary machine that is implemented by
emulating software on a real machine
– provides the hardware platform specifications to
which you compile all Java technology code

CC102- PROGRAMMING FUNDAMENTALS


13
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– The Java Virtual Machine
Bytecode
– a special machine language that can be understood
by the Java Virtual Machine (JVM)
– independent of any particular computer hardware,
so any computer with a Java interpreter can execute
the compiled Java program, no matter what type of
computer the program was compiled on

CC102- PROGRAMMING FUNDAMENTALS


14
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– The Java Virtual Machine

(byte code)

CC102- PROGRAMMING FUNDAMENTALS


15
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
The execution of java code internally will be in the
following steps:
Execution Command -->java Test
1.Start JVM
2.Create and Start Main Thread
3.Look at Test.class File
4.Load Test.class file
5.Execute Main method
6.Unload Test.class
7.Terminate Main Thread
8.Shut down JVM

CC102- PROGRAMMING FUNDAMENTALS


16
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Phases of a Java Program

CC102- PROGRAMMING FUNDAMENTALS


17
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Phases of a Java Program

CC102- PROGRAMMING FUNDAMENTALS


18
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Garbage Collection
– responsible for
freeing any memory
that can be freed.
This happens automatically during the lifetime of the
Java program.
– programmer is freed from the burden of having to
deallocate that memory themselves

CC102- PROGRAMMING FUNDAMENTALS


19
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Code Security
- Code security is
attained in Java
through the
implementation of its
Java Runtime Environment
● JRE
– runs code compiled for a
JVM and performs class loading (through the
class loader), code verification (through the
bytecode verifier) and finally code execution
CC102- PROGRAMMING FUNDAMENTALS
20
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Platform Independence
- “write once, run anywhere.” (WORA)
-The Java compiler compiles the source code
into bytecode, which can be interpreted by a
suitable JVM on any platform.

CC102- PROGRAMMING FUNDAMENTALS


21
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Platform Independence

CC102- PROGRAMMING FUNDAMENTALS


22
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Object Oriented

CC102- PROGRAMMING FUNDAMENTALS


23
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Some features of Java:
– Object Oriented
– Encapsulation: combining an object’s
data with its methods
– Inheritance:
• Code reusability
• Code maintenance
• Implementing OOP
– Polymorphism: allows an object of a
superclass to refer to an object of any
subclass

CC102- PROGRAMMING FUNDAMENTALS


24
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Java Features

• Some features of Java:


– Polymorphism:
• allows an object of a
superclass to refer to
an object of any
subclass

CC102- PROGRAMMING FUNDAMENTALS


25
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Organizing Your Java Application


• A Java application is composed of a set of files generally
distributed in a directory structure
1. Source code: filename.java
2. Class file: classname.class (the same w/ filename)
• The compiler searches for a class file when it
encounters a reference to a class in a .java file
• The interpreter, during runtime, searches the .class files
• Both the compiler and the interpreter search for the
.class files in the list of directories listed in the
classpath variable
CC102- PROGRAMMING FUNDAMENTALS
26
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

A sample source code in java

CC102- PROGRAMMING FUNDAMENTALS


27
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

The main() method


• When executing a Java application, the JVM loads
the class, and invokes the main() method of this
class
• The method main() must be declared public, static,
and void.
• A source file may have one or more classes. Only
one class (matching the file name) at most may be
declared public

CC102- PROGRAMMING FUNDAMENTALS


28
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Coding reminders:
• A .java file name should match the name of a
class in the file. If one of those classes is declared
public, then the name of the .java file must
match the name of the public class
• There can be only one main class at maximum
in a source file
• The compiler generates a file with extension
.class corresponding to each class in the source
file that is compiled
• The name of the .class file matches the name of
the corresponding class
CC102- PROGRAMMING FUNDAMENTALS
29
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Tips: Names

• You can bundle related classes and interfaces into


a group called a package in a directory whose
name matches the name of the package
CC102- PROGRAMMING FUNDAMENTALS
30
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Tips: Names

• The qualified name for the class is course.Student,


and the path name to it is course/Student.java
• to import the package:
import course.Student;
• You can import all the classes in the package:
import course.*;

CC102- PROGRAMMING FUNDAMENTALS


31
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Tips: Names Advantages


• It makes it easier to find and use classes.
• It avoids naming conflicts. Two classes with the
same name existing in two different packages
do not have a name conflict, as long as they
are referenced by their fully qualified name.
• It provides access control.

CC102- PROGRAMMING FUNDAMENTALS


32
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Package/ directory structure

CC102- PROGRAMMING FUNDAMENTALS


33
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Classpath
• When the compiler or the interpreter encounters a
class name in your code, they look for classes in
each directory or a JAR (Java Archive) file listed in
the classpath
• Let’s assume that we put the class files in the
c:\app\class\com\netalong\course directory, the
classpath must include this:
path name:
C:\app\class

CC102- PROGRAMMING FUNDAMENTALS


34
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

How the compiler and interpreter


construct a full path name

CC102- PROGRAMMING FUNDAMENTALS


35
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

The JAR files


• All the directories of an application can be
compressed into what is called a JAR file.
• A JAR file as a tree of directories.
• can be created with the jar command:
jar -cf myApp.jar topDir
• list directories and files in this JAR file:
jar -tf myApp.jar
• execute your application contained in the JAR file:
java -jar myApp.jar

CC102- PROGRAMMING FUNDAMENTALS


36
LESSON I – INTRODUCTION TO JAVA PROGRAMMING

Example programs/activity:
/*
CallingMethodsInSameClass.java
* illustrates how to call public static void printOne(){
* static methods a class System.out.println("Hello World");
* from a method in the same }
* class*/ public static void printTwo() {
public class printOne();
CallingMethodsInSameClass printOne();
{ }
public static void }
main(String[] args) {
printOne();
printOne();
printTwo();
}

CC102- PROGRAMMING FUNDAMENTALS


37
THE END

CC102- Programming Fundamentals

Potrebbero piacerti anche