S1
S1
JAVA PROGRAMMING
1
Reference
2
Popular High-Level Languages
• COBOL (COmmon Business Oriented Language)
• FORTRAN (FORmula TRANslation)
• BASIC (Beginner All-purpose Symbolic Instructional Code)
• Pascal (named for Blaise Pascal)
• Ada (named for Ada Lovelace : English mathematician/writer)
• C (whose developer designed B first)
• Visual Basic (Basic-like visual language developed by Microsoft)
• Delphi (Pascal-like visual language developed by Borland)
• C++ (an object-oriented language, based on C)
• C# (a Java-like language developed by Microsoft)
• Java
• Python 3
Popular High-Level Languages
https://www.tiobe.com/tiobe-index/
4
Popular High-Level Languages
https://www.tiobe.com/tiobe-index/
The programming language SQL was added to the TIOBE index in 2018 after somebody pointed out that SQL
is Turing Complete. So although this language is very old, it has only a short history in the index. (Turing
complete describes a programmable system that can solve any computational problem) 5
Popular High-Level Languages
https://www.tiobe.com/tiobe-index/
6
Java history
• Java was developed by a team led by James
Gosling at Sun Microsystems.
10
PDA and Cell Phone
11
Some characteristics of Java
• Java Is Distributed
• Java Is Architecture-Neutral
• Java Is Portable
• Java Is Multithreaded
• Java Is Dynamic
12
Some characteristics of Java
• Java Is Portable
• Java Is Multithreaded
• Java Is Dynamic
13
Some characteristics of Java
• Java Is Multithreaded
• Java Is Dynamic
14
Some characteristics of Java
• Java Is Dynamic
15
Some characteristics of Java
• Java Is Architecture-Neutral
• Java Is Portable
• Java Is Multithreaded
• Java Is Dynamic
16
Some characteristics of Java
• Java Is Portable
• Java Is Multithreaded
• Java Is Dynamic
17
Some characteristics of Java
• Java Is Dynamic
18
JDK and JRE
• Java Development Kit (JDK) is required to develop and run
programs
19
JDK Editions: Java SE and Java EE
• Java SE: Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone applications
or applets.
• All Java programs run inside the Java Virtual Machine (JVM)
20
JDK Versions
JDK 1.02 (1995-1996) Java SE 10, March 20th, 2018
JDK 1.1 (19 February 1997) Java SE 11, September 25th, 2018
JDK 1.2 (8 December 1998) Java SE 12, March 19th, 2019
JDK 1.3 (8 May 2000) Java SE 13, September 17th, 2019
JDK 1.4 (6 February 2002) Java SE 14, March 17th, 2020
JDK 1.5 (30 September 2004) a. k. a. JDK 5 or Java 5 Java SE 15, September 15th, 2020
JDK 1.6 (11 December 2006) a. k. a. JDK 6 or Java 6 Java SE 16, Expected on March 2021
JDK 1.7 (28 July 2011) a. k. a. JDK 7 or Java 7 Since Java SE 10, new versions will be
released every 6 months.
JDK 1.8 (18 March 2014) a. k. a. JDK 8 or Java 8
JDK 1.9 (21 Septembre 2017) a. k. a. JDK 9 or Java 9
21
Walkthrough 1
A. Download Java SE (Currently JDK 15) - Windows x64 Installer -
159.69 MB - jdk-15.0.1_windows-x64_bin.exe
https://www.oracle.com/java/technologies/javase-downloads.html
B. Install Java SE
After the file is saved, start this executable and the installation wizard
will lead you through the process
C. If required, set path to JDK bin directory (depends on your
installation folder and the JDK version, and operating system) e.g. :
path=C:\Program Files\Java\jdk1.8.0_31\bin
22
Installing JDK on Windows
• To check your installation, enter java –version at the command
prompt to see the confirmation for installation
24
How to add java PATH to system variable?
• Windows 8
3. Under System Variables, find PATH,
and click on it.
4. In the User variables section, select
PATH and click Edit if PATH is already a
user variable.
5. Otherwise, click New to display the
New User Variable window.
6. Add your java path and close the
window.
C:\Program Files\CommonFiles\Oracle\Java\javapath
25
How to add java PATH to system variable?
• Windows 10
1. Go to “Control Panel” → “System and
Security” → “System”
2. Choose “Advanced system settings”
3. In the “Advanced” tab, Click on
“Environment Variables”,
26
How to add java PATH to system variable?
• Windows 10
3. In the “System variables” section,
select PATH and click Edit if PATH is
already a user variable.
4. Otherwise, click New and edit to
display the New User Variable
window.
5. Add your java path and close the
window.
27
How to add java PATH to system variable?
• Windows 8/10
Reopen Command prompt window: to verify whether JDK is
configured correctly, type javac –version from the command prompt,
as shown in figure below
28
Your first Java program
• Three steps to run the Java program
1. Write a source program (using notepad or notepad++) and save it in
a file with a name that ends with .java, for example
HelloWorld.java
File: HelloWorld.java public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World");
}
}
31
Creating, Compiling, and Running
Programs: “HelloWorld.java”
• Java was designed to run object programs on any platform. With Java,
you write the program once, and compile the source program into a
special type of object code, known as bytecode.
• The bytecode can then run on any computer with a Java Virtual
Machine.
33
Trace a Program Execution
LISTING 1.1 HelloWorld.java Execute statement
34
Trace a Program Execution
LISTING 1.1 HelloWorld.java
35
Walkthrough 2
1. Create a directory called algo_b2 and a subdirectory Chap1
2. Open a plain text editor, e.g. Notepad, Notepad++ etc.., enter the
text of the HelloWorld source program and save it as
HelloWorld.java in the directory Chap1.
// This is my first java program
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World");
}
}
• Reserved words
• Modifiers
// This is my first java program
• Statements public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World");
• Blocks }
}
• Classes
• Methods
3. javadoc comment: javadoc comments begin with /** and end with */. They
are used for documenting classes, data, and methods. They can be extracted
into an HTML file using JDK's javadoc command (c:\>javadoc HelloWorld.java).
38
Reserved Words
• Reserved words or keywords are words that have a specific meaning
to the compiler and cannot be used for other purposes in the
program.
• For example, when the compiler sees the word class, it understands
that the word after class is the name for the class.
• Other reserved words in Listing 1.1 are public, static, and void. Their
use will be introduced later.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
39
Modifiers
• Java uses certain reserved words called modifiers that specify the
properties of the data, methods, and classes and how they can be
used.
• Examples of modifiers are public and static. Other modifiers are
private, final, abstract, and protected.
– A public datum (données), method, or class can be accessed by other
programs.
– A private datum (données) or method cannot be accessed by other programs.
• Modifiers are discussed in Chapter 8, “Objects and Classes.”
40
Statements
• A statement represents an action or a sequence of actions.
42
Classes
• The class is the essential Java construct.
• A class is a template or blueprint for objects. To program in Java, you
must understand classes and be able to write and use them.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
45
Displaying Text in a Message Dialog Box
• To display a text in a Message Dialog Box you can use the
showMessageDialog method in the JOptionPane class.
• JOptionPane is one of the many predefined classes in the Java
system, which can be reused rather than “reinventing the wheel.”
Run 46
Two Ways to Invoke the Method
“showMessageDialog”
• There are several ways to use the showMessageDialog method. For
now, you need to know two ways to invoke it.
• One is to use a statement like this:
JOptionPane.showMessageDialog(null, x);
x: is a string for the text to be displayed.
47
Homework and additional reading
• Write a program that will print your name and address on the
console: for example
Alex Johnson
23 Main Street
USA
48