JAVA-ADF Track
Part I
JAVA JSE|JEE
Introduction to Programming
Java Course Prerequisites
Date / Time
26-April-2017 / 7~11 PM-CST Presenter: Sayed Taha
1-1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Session Objectives
After completing this course, you should be able to do the
following:
• Realize the basic concepts of programming.
• Understand meaning of binary system.
• Able to differentiate between stack and heap memory
components.
• Understand the meaning of compilers and IDEs.
• Understand Java basic features.
• Able to write simple java program.
• Compile and run Java applications
• Create Java classes
1-2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
What is the computer language?
• As an electric device computers understands DC voltages.
• High Voltage = +15V, +10v ,….
• Low Voltage = 0V, -5V,….
• As a standard High voltage is referred as ONE (1) and Low
voltage is referred as ZERO (0).
• 01001001010 may represent a command to computer.
1-3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
What is a program?
• Series of instructions to a computer to accomplish a task.
• Instructions must be written in a way the computer can
understand.
• Programming languages are used to write programs.
• Once the code (language) of a program has been written,
it must be executed (run, started).
1-4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Programming languages
• It is a formal language used to give instructions to
computers
• Instructions must be written in a way the computer can
understand.
• Once the code (language) of a program has been written,
it must be executed (run, started).
Machine
Language
Program
Compiler (Binary)
(English Commands)
0101010101
1101010010
1-5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Programming languages(Cont’d)
• Types of programming languages
• Machine Language
Different for each computer processor
• Procedure-Oriented Languages
i.e. FORTRAN, COBOL, C,
• Object-oriented languages
i.e. Smalltalk, C++, Java
• Event-driven languages
most Visual languages i.e. Visual Basic
1-6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
What is a variable?
• A variable is a piece of memory that can contain a
data value.
• A variable thus has a data type.
• Data types are covered in more detail in the text
on data types.
• Variables are typically used to store information
which your program needs to do its job
name age BD
Stack
Heap Ahmed 27
1990
1-7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
What is a method?
• A method is a set of code which is referred to by name
and can be called (invoked) at any point in a program
simply by utilizing the method's name.
• Think of a method as a subprogram that acts on data and
often returns a value.
• Each method has its own name.
• Method Signature is as follow:
Access modifier Return type Name (parameters list)
public int add(int x , int y){
return x+y;
}
1-8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Use the right tool for the right job
• IDE (Integrated Development Environment)
• IDE includes helper tools for writing, compiling and running
the program.
• Regular IDEs for java
- Eclips - Netbeans - JDeveloper
• For PL-SQL we recommend SQL Developer
1-9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java
• History of Java
– Sun Microsystems 1995.
– Aimed first to create hardware controller software,
– Team supported by Netscape leaded by
James Golsing.
• Oracle acquired Sun Microsystems on January, 2010.
• Java Can be used to create: desktop, web, enterprise
and mobile application
1 - 10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java SE Platform Versions
Developer Version
Year Platform
(JDK)
1996 1.0 1
1997 1.1 1
1998 1.2 2
2000 1.3 2
2002 1.4 2
2004 1.5 5
2006 1.6 6
2011 1.7 7
2014 1.8 8
Expected on July 2017 1.9 9
1 - 11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Features
• Free License.
• Easy to learn.
• Syntax of C++.
• Fully Object Oriented Programming Language.
• Dynamic memory allocation due to the usage of Garbage
Collector.
• Machine and platform(OS) independent.
Application
Java Virtual Machine (JVM)
OS
H/W
1 - 12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Features Cont’d
• Compiled and interpreted.
• Java interpreter java translate a .class file into code that
can be executed natively on the underlying machine.
Interpret
Compile JVM
*.java *.class
1 - 13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Features Cont’d
• Java depends on late binding at interpreting time
(libraries dynamic linking)
Compiler
JVM Libs +
utilities
JRE (Java Runtime Environment)
JDK (Java Development Kit)
1 - 14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Features Cont’d
• Java is multithreaded.
– Run more than thread of execution in parallel.
• Java is networked:
allows creating network aware apps using different networking
techniques:
– Sockets programming
– RMI: remote method invocation
– CORBA: Common Object Request Broker Architecture
1 - 15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Session Environment
Core Apps
• JDK 7.x / 8.x
• NetBeans 7.x / 8.x
1 - 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Getting Started with Java
• Hello World Program
• When learning a new language, the first program people
usually write is one that salutes the world :)
• Here is the Hello world program in C++.
1 - 17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Manually Compile and Run Java App
• Create Hello.java file and save it into a path
• Set environment variables:
– Create environment variable named JAVA_HOME and set
the value to jdk root folder in program files.
– Edit path environment variable and concatenate with it jdk
folder/bin using {;}
– for example:
JAVA_HOME= C:\Program Files (x86)\Java\jdk1.8.0_121
PATH = %PATH%;%JAVA_HOME%\bin.
• From command prompt : Compile the file using javac
command: i.e. javac Hello.java
• Run the file using java command:
i.e. java Hello.
1 - 18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Compile and Run Java App using an IDE
• Lets try Hello World program using Netbeans.
1 - 19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Structure
• Classes (Apps / Libs) are organized in packages.
• Packaging is to create a folder hierarchy.
• Java Runtime libraries exist in rt.jar.
• Below some package names that contains the
commonly used classes of the java library.
1 - 20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Hello World App Declaration Cont’d
• Main() method
– returns void.
– must be static because it is the first method that is called by
the interpreter even before creating any object.
– Must be public to be directly accessible.
– It accepts an array of Strings as parameters.
• out reference
– Is a static reference that has been created in System class.
– Refers to an object of a class PrintStream.
– PrintStream is an already made stream that is attached to
the standard O/P i.e. the screen.
1 - 21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Archive (JAR)
• Packages are to be compressed in an archive file
name JAR (Java Archive).
• Some information about the archived application can
be written in a MAINFEST.MF file. i.e. vendors , size
version,…etc.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.7
Created-By: 1.8.0_121-b13 (Oracle Corporation)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.java.HelloWorldApp
1 - 22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Archive (JAR)
• Create Jar file using command line.
jar cfm Hello.jar MANIFEST.MF Hello.class
• Run jar file
java -jar Hello.jar
• Netbeans creates jar by default inside dist folder.
1 - 23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Memory
• RAM: Random Access Memory is didived into two
main parts
– Stack: contains variables names points to addresses into
– Heap: hodls the actual data values
Stack name age BD
Heap
Ahmed 27
1990
1 - 24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Memory Cont’d
• Default java heap is fourth (1/4) the physical memory.
• Setting java memory (heap) is needed to avoid java
heap space size runtime errors.
– From control panel Programs Java java tab select
java version.
– Edit Runtime Parameters: set memory size as
— -Xms512m
— -Xms1024m
— -Xms2048m
— -Xms3072m for 3GB
1 - 25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets
• Applet is a client side java application that runs inside
the web browser.
1 - 26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets
• Application byte code (.class) file is to be downloaded
from the webserver to the user’s machine.
• JVM interprets and runs applet in the browser.
1 - 27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets
• For security purpose:
– Applet code is checked before running it.
– Input and Output operations on H.D.D are not allowed.
– No communications are allowed be any server rather than
the server from which applet is downloaded.
1 - 28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets life cycle
• Once the Applet object is created Constructor is invoked.
• Applet is then goes through the following life cycle inside
the web bowser:
– init(): called when the applet is initialized for the first time.
– start(): called whenever the browser window is activated.
– paint(Graphics graph): called after start() to paint the
applet or whenever the applet is repainted.
– stop(): called when browser window is deactivated.
– destroy(): called whenever the browser window is closed.
• Refreshing the applet is achieved via calling repaint()
method
– Repaint just clears the applet and invokes paint(Graphics
graph) again.
1 - 29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets Example
• Custom applet extends Applet class.
• Override the appropriate methods.
• Applet must be public. Otherwise the browser wouldn’t be
able to access it.
• For Example:
public class HelloApplet extends Applet {
public void init() {
}
public void paint(Graphics g){
g.drawString("Hello Java", 100, 150);
}
}
1 - 30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Java Applets Example
• Create an HTML page that includes the Applet:
<html>
<body>
<h1>Java Course<h1>
<applet code="HelloApplet" width=400
height=350></applet>
</body>
</html>
1 - 31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Compiling and Running Java Applets
• From the command prompt run :
appletviewer HelloApplet.html
1 - 32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Summary
In this lesson, you should have learned about:
• Fundamentals of programming.
• Java history and main features.
• The open nature of Java and its community.
• Java program structure.
• Java achieve handling.
• Dealing with java Applets
1 - 33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.