Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

01 IntroJava

The document provides an introduction to Java programming including its history, how it impacted the internet, and key concepts. Java was created at Sun Microsystems in 1991 to be platform independent and embeddable. It became successful due to enabling portable and secure applets and addressing issues like security and portability through bytecode executed by the Java Virtual Machine.

Uploaded by

Vân Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

01 IntroJava

The document provides an introduction to Java programming including its history, how it impacted the internet, and key concepts. Java was created at Sun Microsystems in 1991 to be platform independent and embeddable. It became successful due to enabling portable and secure applets and addressing issues like security and portability through bytecode executed by the Java Virtual Machine.

Uploaded by

Vân Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

12/10/2022

JAVA PROGRAMMING

Week 1: Introduction to Java


Lecturer:
NGUYỄN Thị Minh Tuyền

Plan 2

1. History
2. How Java impacted the Internet
3. Compiling and running a Java program

Java Programming

1
12/10/2022

Plan 3

1. History
2. How Java impacted the Internet
3. Compiling and running a Java program

Java Programming

Origins of Java 4

• Conceived by James Gosling, Patrick Naughton, Chris


Warth, Ed Frank, and Mike Sheridan at Sun
Microsystems in 1991.
• Initially called “Oak” but was renamed “Java” in 1995.
• Primary motivation:
• A platform independent language
• To create software to be embedded in various consumer
electronic devices, such as toasters, microwave ovens, and
remote controls.
• Slogan: Write once, run anywhere

Java Programming

2
12/10/2022

Problems (at that time) 5

• Most computer languages were designed to be compiled


into machine code that was targeted for a specific type of
CPU.
• For example, consider the C++ language.
• Although it was possible to compile a C++ program for
just about any type of CPU, to do so required a full C++
compiler targeted for that CPU à compilers are
expensive and time consuming to create.
à In an attempt to find a better solution, Gosling and the
others worked on a portable, cross-platform language that
could produce code that would run on a variety of CPUs
under differing environments à Java

Java Programming

Java’s Lineage: C and C++ [1] 6

• C and C++ are among the most important computer


languages ever invented and are still in widespread use
today.
• Java inherits its syntax from C. Java’s object model is
adapted from C++.
• Java’s relationship to C and C++ is important for a
number of reasons.
• Many programmers were familiar with the C/C++ syntax à Java
uses a similar syntax, it was relatively easy for a C/C++
programmer to learn Java.
• Java provides a powerful, logically consistent programming
environment by inheriting and building on C/C++.

Java Programming

3
12/10/2022

Java’s Lineage: C and C++ [2] 7

• Although C++ and Java are related, especially in their


support for object-oriented programming, Java is not
simply the “Internet version of C++”.
• Java has significant practical and philosophical
differences from C++.
• Java is not an enhanced version of C++.

Java Programming

Plan 8

1. History
2. How Java impacted the Internet
3. Compiling and running a Java program
4. Variables
5. Expression

Java Programming

4
12/10/2022

How Java Impacted the Internet 9

• Java had a profound effect on the Internet


• The creation of Java acting as a catalyst that drew legions of
programmers to the Web.
• Java innovated a new type of networked program called the
applet that changed the way the online world thought about
content.
• Java addressed some of the thorniest issues associated with the
Internet: portability and security.

Java Programming

Java Simplified Web-Based


Programming 10

• Ability to create portable, cross-platform programs.


• Java supports for networking
• Provide library that enabled programmers to easily write
programs that accessed or made use of the Internet.
• Provided mechanisms that enabled programs to be readily
delivered over the Internet.

Java Programming

10

5
12/10/2022

Java Applets 11

• An applet is a special kind of Java program designed to


be transmitted over the Internet and automatically
executed inside a Java-compatible web browser.
• If the user clicks a link that contains an applet, the applet will
download and run in the browser automatically.
• Applets: small programs, typically used to display data provided
by the server, handle user input, or provide simple functions.
• Key feature: execute locally, rather than on the server.
• The creation of the applet was important because it is a
dynamic, self-executing program.
• In the early days of Java: applets were a crucial part of
Java programming.
• Applet support was removed by JDK 11.

Java Programming

11

Security 12

• Java enables you to confine an application to the Java


execution environment and prevent it from accessing
other parts of the computer.
• The ability to download an application with a high level of
confidence that no harm will be done contributed
significantly to Java’s early success.

Java Programming

12

6
12/10/2022

Portability 13

• Java provides mechanism that allows the same


application to be downloaded and executed by a wide
variety of CPUs, operating systems, and browsers is
required.

Java Programming

13

Java’s Magic: The Bytecode 14

• The key that allowed Java to address both the security


and the portability problems is that the output of a Java
compiler is not executable code.
• Rather, it is bytecode.
• Bytecode is a highly optimized set of instructions designed to be
executed by what is called the Java Virtual Machine (JVM), which
is part of the Java Runtime Environment (JRE).
• The original JVM was designed as an interpreter for bytecode.
• Once a JRE exists for a given system, any Java program
can run on it.
• Although the details of the JRE will differ from platform to
platform, all JREs understand the same Java bytecode.

Java Programming

14

7
12/10/2022

15

• A Java program is executed by the JVM also helps to


make it secure.
• Because the JVM is in control, it manages program execution.
• It is possible for the JVM to create a restricted execution
environment, called the sandbox, that contains the
program, preventing unrestricted access to the machine.
Safety is also enhanced by certain restrictions that exist
in the Java language.
• Because bytecode has been highly optimized, the use of
bytecode enables the JVM to execute programs faster.

Java Programming

15

Servlet 16

• A Java servlet is a small program that executes on a


server.
• Servlets dynamically extend the functionality of a web
server.
• With the advent of the servlet, Java spanned both sides
of the client/server connection.

Java Programming

16

8
12/10/2022

Java Buzzwords 17

Java Programming

17

Object-Oriented Programming 18

• To support the principles of OOP, Java have three traits in


common
• encapsulation,
• polymorphism, and
• inheritance.

Java Programming

18

9
12/10/2022

Encapsulation 19

• A programming mechanism that binds together code and


the data it manipulates, and that keeps both safe from
outside interference and misuse.
• Within an object, code, data, or both may be private to
that object or public.
• Java’s basic unit of encapsulation is the class.
• A class defines the form of an object.
• It specifies both the data and the code that will operate on that
data. Java uses a class specification to construct objects.
• Objects are instances of a class.
• The code and data that constitute a class are called
members of the class.

Java Programming

19

Polymorphism 20

• Polymorphism = “many forms”


• The quality that allows one interface to access a general
class of actions.
• The specific action is determined by the exact nature of the
situation.
• Polymorphism helps reduce complexity by allowing the
same interface to be used to specify a general class of
action.
• It is the compiler’s job to select the specific action (i.e., method)
as it applies to each situation.
• The programmer don’t need to do this selection manually.

Java Programming

20

10
12/10/2022

Inheritance 21

• The process by which one object can acquire the


properties of another object.
• This is important because it supports the concept of
hierarchical classification.
• Using inheritance, an object need only define those
qualities that make it unique within its class.
• It can inherit its general attributes from its parent.
àIt is the inheritance mechanism that makes it possible for one
object to be a specific instance of a more general case.

Java Programming

21

Plan 22

1. History
2. How Java impacted the Internet
3. Compiling and running a Java program
4. Variables
5. Expression

Java Programming

22

11
12/10/2022

Java Development Kit [1] 23

• Before you can compile and run those programs, you


must have a Java Development Kit (JDK).
• The current release of the JDK is JDK 15.
• This is the version for Java SE 15. (SE stands for Standard
Edition)
• If you need to install the JDK on your computer:
• Be aware that for modern versions of Java, both Oracle JDKs
and open source OpenJDKs are available for download.

Java Programming

23

24

Source: https://www.oracle.com/java/technologies/javase-jdk14-downloads.html

Java Programming

24

12
12/10/2022

Java Development Kit [2] 25

• The JDK supplies two primary programs


• javac: the Java compiler.
• java : the standard Java interpreter and is also referred to as the
application launcher.
• The JDK runs in the command-prompt environment and
uses command-line tools.
• It is not a windowed application.
• It is also not an integrated development environment (IDE).
• There are several high-quality IDEs available for Java,
such as NetBeans and Eclipse.

Java Programming

25

My first example 26
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello, world!");
} Hello, world!
}

Three steps:
1. Enter the program.
2. Compile the program.
3. Run the program.

Java Programming

26

13
12/10/2022

My first example: Command line 27


Compile: javac HelloWorld.java
Run : java HelloWorld
Output: Hello, world

Java Programming

27

Comment 28

// A single-line comment

/*
* This is a simple Java program
* Multiline comment
*/

Java Programming

28

14
12/10/2022

29
public class HelloWorld2 {
public static void main(String[] args) {
if (args.length >0) {
System.out.print("Hello, " + args[0]);
}else {
System.out.println("Hello new user!");
}
}
}

Java Programming

29

30
/* This demonstrates a variable
* Call this file Example2.java
*/
public class Example2{
public static void main(String args[]) {
int myVar1; // this declares a variable
int myVar2; // this declares another variable

myVar1 = 1024; // this assigns 1024 to myVar1


System.out.println("myVar1 contains " + myVar1);

myVar2 = myVar1 / 2;
System.out.print("myVar2 contains myVar1 / 2: ");
System.out.println(myVar2);
}
} Java Programming

30

15
12/10/2022

Arithmetic operators 31

+ Addition
- Subtraction
* Multiplication
/ Division

Java Programming

31

Another data type 32

• To allow numbers with fractional components, Java


defines two floating-point types: float and double
• represent single and double-precision values, respectively.
• double is the most commonly used.
• To declare a variable of type double:
double x;

Java Programming

32

16
12/10/2022

/* This program illustrates the differences between int


* and double; Call this file Example3.java
*/
33
public class Example3 {
public static void main(String args[]) {
int v; // this declares an int variable
double x; // this declares a floating-point variable
v = 10; // assign v the value 10
x = 10.0; // assign x the value 10.0

System.out.println("Original value of v: " + v);


System.out.println("Original value of x: " + x);

// now, divide both by 4


v = v / 4;
x = x / 4;
System.out.println("v after division: " + v);
System.out.println("x after division: " + x);
}
}

Java Programming

33

Exercise: Converting Gallons to


Liters 34

1 gallon = 3.7854 liters


1. Create a new file called GalToLit.java.
2. Implement the conversion of gallons to liters in
GalToLit.java
3. Compile the program:
javac GalToLit.java
4. Run the program:
java GalToLit.java

Java Programming

34

17
12/10/2022

GalToLit.java 35
/* This program converts gallons to liters
* Call this program GalToLit.java
*/
public class GalToLit {

public static void main(String[] args) {


double gallons; // holds the number of gallons
double liters; // holds conversion to liters
gallons = 10; // start with 10 gallons
liters = gallons * 3.7854; // convert to liters
System.out.println(gallons + " gallons is " + liters + "
liters.");
}
}

Java Programming

35

Two control statements 36

• if statement
• for loop

Java Programming

36

18
12/10/2022

if statement 37

if(condition) statement;
• condition is a Boolean expression
• Example:
if (10 < 11) System.out.println("10 is less than 11");
if (10 < 9) System.out.println("this won’t be displayed");

Java Programming

37

Operators 38

Operator Meaning
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal to
!= No equal

Java Programming

38

19
12/10/2022

Example 39
/* Demonstrate the if
* Call this file IfDemo.java
*/
public class IfDemo {
public static void main(String[] args) {
int a, b, c;
a = 2;
b = 3;
if(a < b) System.out.println("a is less than b");
//this won't display anything
if(a == b) System.out.println("you won't see anything");
System.out.println();
c = b - a; // c contains 1
System.out.println("c contains 1");
if(c >= 0) System.out.println("c is non-negative");
if(c < 0) System.out.println("c is negative");
}
} Java Programming

39

for Loop 40
for(initialization; condition; iteration) statement;
• Example:
/* Demonstrate the for loop.
* Call this file ForDemo.java.
*/
public class ForDemo {

public static void main(String[] args) {


int count;
for(count = 0; count < 5; count = count + 1)
System.out.println("This is count: " + count);
System.out.println("Done!");
}
}

Java Programming

40

20
12/10/2022

Create blocks of code 41


/* Demonstrate a block of code
* Call this file BlockDemo.java.
*/
public class BlockDemo {

public static void main(String[] args) {


double i, j, d;
i = 5;
j = 10;
// the target of this if is a block
if(i != 0) {
System.out.println("i does not equal zero");
d = j / i;
System.out.println("j / i is " + d);
}
}
}

Java Programming

41

Semicolons and positioning 42

• In Java, the semicolon is a separator


• It is often used to terminate a statement.
• In essence, the semicolon indicates the end of one logical
entity.
x = y;
y = y + 1;
System.out.println(x + " " + y);
• Java does not recognize the end of the line as a
terminator.
x = y; y = y + 1; System.out.println(x + " " + y);
System.out.println("This is a long line of output"
+ x + y + x + "more output");

Java Programming

42

21
12/10/2022

Indentation practices 43

• Java is a freeform language, meaning that it does not


matter where you place statements relative to each other
on a line.
• However, over the years, a common and accepted
indentation style has developed that allows for very
readable programs.
• Using this style, you indent one level after each opening
brace, and move back out one level after each closing
brace.

Java Programming

43

Exercise: Improving the Gallons-


to-Liters Converter 44
1. Create a new file called GalToLitTable.java.
2. Implement program into the file GalToLitTable.java
3. Compile the program:
javac GalToLitTable.java
5. Run the program:
java GalToLitTable

Java Programming

44

22
12/10/2022

45
class GalToLitTable {
public static void main (String args[]) {
double liters;
int counter = 0;
for (double gallons = 1; gallons <= 100; gallons++) {
liters = gallons * 3.7854;
System.out.println(gallons + " gallons is "
+ liters + " liters.");
counter++;
if (counter == 10) {
System.out.println();
counter = 0;
}
}
}
}
Java Programming

45

Java keywords 46

Java Programming

46

23
12/10/2022

Identifiers in Java 47

• An identifier is a name given to a method, a variable, or


any other user-defined item.
• Identifiers can be from one to several characters long.
• Variable names may start with any letter of the alphabet,
an underscore, or a dollar sign. Next may be either a
letter, a digit, a dollar sign, or an underscore.
• The underscore can be used to enhance the readability of
a variable name, as in line_count.
• Uppercase and lowercase are different; that is, to Java,
myvar and MyVar are separate names.

Java Programming

47

Java class libraries 48

• The Java environment relies on several built-in class


libraries that contain many built-in methods that provide
support for such things as I/O, string handling,
networking, and graphics.
• The standard classes also provide support for a graphical
user interface (GUI).
• Thus, Java as a totality is a combination of the Java
language itself, plus its standard classes.

Java Programming

48

24
12/10/2022

49

Question?

Java Programming

49

Exercise 50

Write a program:
1. Ask user to enter personal information (first name, last name,
date of birth, adress etc.) and display information on screen.
2. Display personal information received from aguments passed
through the function main.
3. Display the square root of floating points numbers (double)
from 1 to 100 and its rounding error.

Java Programming

50

25

You might also like