0% found this document useful (0 votes)
11 views

S1

Uploaded by

Dany Merhej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

S1

Uploaded by

Dany Merhej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

INTRODUCTION TO

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.

• Originally called Oak, Java was designed in


1991 for use in embedded chips in consumer
electronic appliances.

• In 1995, renamed Java, it was redesigned for


developing Web applications.

• Sun Microsystems was purchased by Oracle in


2010.
• For the history of Java, see
https://www.oracle.com/java/moved-by-java/timeline/ 7
Why Learn Java?
• Large code base of already written applications - More than 12
million professional developers worldwide use Java by the end of
2017. 1 billion= 1milliard
• There were 38 billion active Java Virtual Machines (JVMs) in 2017.
• The same program can run on different platforms using JVMs.
• Mobile Android development is done in Java
• Lots of enterprise applications were developed and are being
developed in Java.
• Java is very popular for developing applications on web servers.
Many commercial Websites are developed using Java on the
backend.
• In 2018, Java is ranked the #1 language used by developers for the
cloud. 8
Examples of Java’s Versatility (Applets)

Java, however, is now very


popular for developing
applications on web
servers.

• Java initially became attractive because Java programs can


run from a web browser. Such programs are called applets.

• Today applets are no longer allowed to run from a Web


browser in the latest version of Java due to security issues.
9
PDA and Cell Phone

10
PDA and Cell Phone

11
Some characteristics of Java

• Java Is Simple • Java is partially modeled on


C++, but greatly simplified and
• Java Is Object-Oriented improved.

• Java Is Distributed

• Java Is Architecture-Neutral

• Java Is Portable

• Java Is Multithreaded

• Java Is Dynamic
12
Some characteristics of Java

• Java Is Simple • Java is inherently object-oriented.


Although many object-oriented
• Java Is Object-Oriented languages began strictly as
procedural languages, Java was
• Java Is Distributed designed from the start to be
object-oriented.
• Java Is Architecture-Neutral

• Java Is Portable

• Java Is Multithreaded

• Java Is Dynamic
13
Some characteristics of Java

• Java Is Simple • Distributed computing involves


several computers working
• Java Is Object-Oriented together on a network.
Since networking capability is
• Java Is Distributed inherently integrated into Java,
writing network programs is
• Java Is Architecture-Neutral like sending and receiving data
to and from a file.
• Java Is Portable

• Java Is Multithreaded

• Java Is Dynamic
14
Some characteristics of Java

• Java Is Simple • Write once, run anywhere.


• Java programs are compiled
• Java Is Object-Oriented into the Java Virtual Machine
code called bytecode.
• Java Is Distributed
• With a Java Virtual Machine
(JVM), you can write one
• Java Is Architecture-Neutral
program that will run on any
• Java Is Portable platform (Linux, macOS,
Windows, Solaris SPARC)
• Java Is Multithreaded

• Java Is Dynamic
15
Some characteristics of Java

• Java Is Simple • Because Java is architecture


neutral, Java programs are
• Java Is Object-Oriented portable. They can be run on
any platform without being
• Java Is Distributed recompiled.

• Java Is Architecture-Neutral

• Java Is Portable

• Java Is Multithreaded

• Java Is Dynamic
16
Some characteristics of Java

• Java Is Simple • Multithreading is the ability of a


central processing unit (CPU) or
• Java Is Object-Oriented a single core in a multi-core
processor to execute multiple
• Java Is Distributed processes or threads
concurrently
• Java Is Architecture-Neutral

• Java Is Portable

• Java Is Multithreaded

• Java Is Dynamic
17
Some characteristics of Java

• Java Is Simple • Java was designed to adapt to an


evolving environment. New code
• Java Is Object-Oriented can be loaded on the fly without
recompilation.
• Java Is Distributed • There is no need for developers
to create, and for users to install,
• Java Is Architecture-Neutral major new software versions.
New features can be
• Java Is Portable
incorporated transparently as
needed
• Java Is Multithreaded

• Java Is Dynamic
18
JDK and JRE
• Java Development Kit (JDK) is required to develop and run
programs

• Java Runtime Environment (JRE) is required to run


programs.

• Users must have JRE installed, developers – JDK

• JDK includes JRE.

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.

• Java EE: Java Enterprise Edition (a.k.a. J2EE)


– J2EE can be used to develop server-side applications such as Java
servlets and Java ServerPages.

• Java Micro Edition (J2ME).


– J2ME can be used to develop applications for mobile devices such
as cell phones.

• 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

• Enter javac –version

If does not work you have to


add java to the path
23
How to add java PATH to system variable?
• Windows 8
1. Go to Control Panel → System
→Advanced

2. In the advanced tab, click on


Environment Variables,

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

Congratulations! Your JDK and JRE are installed

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");
}
}

2. Compile the program using javac compiler, e.g.


javac HelloWorld.java
This will create a file HelloWorld.class

3. Run your program


Java HelloWorld 29
Writing “HelloWorld.java”
• In Java, you start with creating a class. Our class is called HelloWorld.

• A class can have methods. In this case it’s called main()


• You can run the class as a program if it has a method with the
following signature
public static void main(String[] args)
30
Creating, Compiling, and Running
Programs: “HelloWorld.java”

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.

• Java Virtual Machine is a software that interprets Java bytecode 32


Trace a Program Execution
LISTING 1.1 HelloWorld.java Enter main method after
c:\> Java HelloWorld

//This program prints Hello World!


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

33
Trace a Program Execution
LISTING 1.1 HelloWorld.java Execute statement

//This program prints Hello World!


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

34
Trace a Program Execution
LISTING 1.1 HelloWorld.java

//This program prints Hello World!


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

print a message to the


console

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");
}
}

3. Open the command window, change the directory to Chap1 using


the cd command.

4. Compile (javac) and run (java) HelloWorld program – it has to print


HelloWorld on the screen. (take care: javac is case sensitive)
36
Anatomy of a Java Program
• Comments

• 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

• The main method


37
Comments
• Three types of comments in Java
1. Line comment : A line comment is preceded by 2 slashes // in a line.
2. Paragraph comment : A paragraph comment is enclosed between /* and */ in
one or multiple lines.
// This is my first java program
public class HelloWorld {
/**
* @param args : args takes command line argument
*/
public static void main(String[] args) {
System.out.println("Hello World");
/* This is
a multiple line comment
*/
}
}

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.

• The statement System.out.println("Welcome to Java!") in the


program in the listing below is a statement to display the greeting
"Welcome to Java!"

• Every statement in Java ends with a semicolon (;).

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
41
Blocks
• A pair of braces in a program forms a block that groups
components of a program.

public class Test{


public static void main (String[] args){
Class Method
System.out.println("Welcome to Java");
block block
}
}

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!");
}
}

• The mystery of the class will continue to be unveiled throughout this


course.
• For now, though, understand that a program is defined by using one
or more classes.
43
Methods
• What is System.out.println?
The System.out.println is a method.

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

• A method is a collection of statements that performs a sequence of


operations to display a message on the console. It can be used even
without fully understanding the details of how it works.
• Methods are called functions in other programming languages, e.g.
44 C.
The main Method
• The main method provides the control of program flow.
• The Java interpreter executes the application by invoking the main
method.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
// other statements.
}
}

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.

• The other is to use a statement as shown below:


JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE);
x : is a string for the text to be displayed,
y : is a string for the title of the message dialog box.
For example

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

New York, NY 10001

USA

• Study all materials in chapter 1 from the textbook

• Do exercises 1.11 and 1.12 from chapter 1

48

You might also like