SlideShare a Scribd company logo
1 of 42
Introduction to Java
Programming Language
Junji Zhi
University of Toronto

1
Content
•
•
•
•
•
•

Java language Syntax
“Hello World” program example
Compiling, Running and Debugging Java code
Inheritance
Threading
Synchronization

2
Java programming Language
• Some buzzwords for Java
–
–
–
–
–
–
–
–
–
–
–

“Write Once, Run Anywhere”
Simple
Object oriented
Distributed
Multithreaded
Dynamic
Architecture neutral
Portable
High performance
Robust
Secure
3
Example: Hello World Program

• Everything is in a class
• One file, one public class
• In the runnable public class:
– public static void main(String [] args)

4
Primitive Data Types
• Primitive Data Types: byte, short, int, long, float,
double, boolean, char
• Arrays are also a class
long [] a = new

long[5];

– You can get the length by visiting the length field of
array object a, like this: a.length

• String class is very commonly used to represents
character strings, for example
String s1 = “Hello ”, s2 = “Wolrd!”;
String s3 = s1 + s2;

5
Operators (same as C/C++) [3]
•
•
•
•
•

++,-- Auto increment/decrement
+,- Unary plus/minus
*,/ Multiplication/division
% Modulus
+,- Addition/subtraction

6
Declaring Variables [3]
int n = 1;
char ch = „A‟;
String s = “Hello”;
Long L = new Long(100000);
boolean done = false;
final double pi = 3.14159265358979323846;
Employee joe = new Employee();
char [] a = new char[3];
Vector v = new Vector();

7
Compared with C/C++ [3]
• Java has no:
–
–
–
–
–
–
–
–
–
–

pointers
typedef
preprocessor
struct
unions
multiple inheritance
goto
operator overloading
malloc
…
8
Declaring a class
•
•
•
•
•

package
Class name
Constructor
Fields
methods

9
Compiling, Running and
Debugging Java Programs

10
Java Development Process
.java => .class => JVM execution

11
Installing Java in your machine (1)
• Downloading Java Development Kit (JDK) from
Oracle
• Java Runtime Environment (JRE) is usually
included in the JDK installation file.

12
Installing Java in your machine (2)
• Setting JAVA_HOME (Windows):
– E.g., C:Program FilesJavajdk1.7.0_45

• Setting path and classpath

13
Compile .java File into a .class File
(Command Line)

14
Running HelloWorld in Eclipse IDE
Eclipse Download from here.

15
Java platform

16
Debugging Java in Eclipse (1)
• Debugging means “run a program
interactively while watching the source code
and the variables during the execution.” [5]
• Set breakpoints to stop the program at the
middle of execution
• Eclipse has a Debug Mode

17
Debugging Java in Eclipse(2)

Image courtesy: http://www.vogella.com/tutorials/EclipseDebugging/images/xdebugstart20.gif.pagespeed.ic.SqCELlNeCm.png
18
Debugging Java in Eclipse(3)

Table courtesy: http://www.vogella.com/tutorials/EclipseDebugging/article.html
19
Java Inheritance

20
Inheritance in Java
• Java classes can be derived from other classes,
thereby inheriting fields and methods from those
classes.

21
Common Root: Object

22
Interface

23
“Multiple Inheritance”

24
A Real World Example: ArrayList

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

25
Java Threading

26
Java Threading
• A thread is a thread of execution in a program [6]
• JVM allows an application to have multiple threads running
concurrently.
• Apache Harmony example:

http://harmony.apache.org/subcomponents/drlvm/TM.html

27
Two ways to do threading
1. Extends Thread class

2. Implements
Runnable interface

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

28
Thread lifecycle

29
How to stop a Thread
• Using Thread.interrupt() method:

http://stackoverflow.com/questions/7786305/stopping-a-specific-java-thread

30
Java Synchronization

31
Thread Interference (1)
• Increment operation is translated to
multiple steps by the virtual
machine :
1. Retrieve the current value of c.
2. Increment the retrieved value
by 1.
3. Store the incremented value
back in c.

Example from: http://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html

32
Thread Interference (2)
•
•
•
•

Assume we have 2 threads, A and B.
A increments c, and B decrements c.
Thread A and B runs together.
One possible order of the low-level steps:
1.
2.
3.
4.
5.
6.

Thread A: Retrieve c.
Thread B: Retrieve c.
Thread A: Increment retrieved value; result is 1.
Thread B: Decrement retrieved value; result is -1.
Thread A: Store result in c; c is now 1.
Thread B: Store result in c; c is now -1.

• Is the result correct?
• What if the thread A and B are bank transactions?
33
Problem Root Cause
• Threads are visiting one field (resource) at the
same time.
• Multiple steps of an operation
• No enforced “happen-before” relationship

34
Solution: synchronized method

Example: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

35
synchronized method
• Enforce the ‘happen-before’ relationship in
the method level.
• Either one of the below instance will happen.
But result is always 0, which is correct.
1.
2.
3.
4.
5.
6.

Thread A: Retrieve c.
Thread A: Increment retrieved value;
result is 1.
Thread A: Store result in c; c is now 1.
Thread B: Retrieve c.
Thread B: Decrement retrieved value;
result is 0.
Thread B: Store result in c; c is now 0.

1.
2.

OR

3.
4.
5.
6.

Thread B: Retrieve c.
Thread B: Decrement retrieved value;
result is -1.
Thread B: Store result in c; c is now -1.
Thread A: Retrieve c.
Thread A: Increment retrieved value;
result is 0.
Thread A: Store result in c; c is now 0.
36
synchronized statements (1)
• Every object has an intrinsic lock associated
with it
• Primitive types (e.g., int, char) do not have
intrinsic locks.
• We can combine object intrinsic locks and
synchronized keyword to create fine-grained
synchronization control.

37
synchronized statements (2)

http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

38
synchronized statement hazards (1)

• Boolean has only two instances of Boolean
• If another thread also synchronizes on the same Boolean
instance, like this:
– private final Boolean someLock = Boolean.FALSE;

• The lock will be reused.
• The system might be deadlock or unresponsive.
• It is hard to detect this type of bugs!
More examples:
https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused

39
synchronized statement hazards (2)
• Another example of the wrong way of using
locks:

What will happen another thread also synchronizes on an integer instance
with the 0 integer value?
https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused

40
synchronized statement hazards (3)
• Correct way of using locks: using new to
instantiate an object

https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused

41
References
1. Thinking in Java 4th Ed, Bruce Eckel
2. Oracle Java tutorial
(http://docs.oracle.com/javase/tutorial/index.ht
ml)
3. www.cs.drexel.edu/~spiros/teaching/CS575/slid
es/java.ppt
4. http://eclipsetutorial.sourceforge.net/Total_Begi
nner_Companion_Document.pdf
5. http://www.vogella.com/tutorials/EclipseDebug
ging/article.html
42

More Related Content

What's hot

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
SoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with SpringSoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with SpringNayden Gochev
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Ryan Cuprak
 
Java 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx FranceJava 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx FranceTrisha Gee
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3RatnaJava
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Javahendersk
 
Smalltalk on the JVM
Smalltalk on the JVMSmalltalk on the JVM
Smalltalk on the JVMESUG
 
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5aminmesbahi
 
Java 101 Intro to Java Programming
Java 101 Intro to Java ProgrammingJava 101 Intro to Java Programming
Java 101 Intro to Java Programmingagorolabs
 
DSR Testing (Part 2)
DSR Testing (Part 2)DSR Testing (Part 2)
DSR Testing (Part 2)Steve Upton
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10Minal Maniar
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Andrei KUCHARAVY
 
Migration strategies 4
Migration strategies 4Migration strategies 4
Migration strategies 4Wenhua Wang
 

What's hot (20)

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
SoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with SpringSoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with Spring
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
 
Java 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx FranceJava 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx France
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
 
Exploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systemsExploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systems
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
Core Java
Core JavaCore Java
Core Java
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Java
 
Smalltalk on the JVM
Smalltalk on the JVMSmalltalk on the JVM
Smalltalk on the JVM
 
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5
 
Java 101 Intro to Java Programming
Java 101 Intro to Java ProgrammingJava 101 Intro to Java Programming
Java 101 Intro to Java Programming
 
DSR Testing (Part 2)
DSR Testing (Part 2)DSR Testing (Part 2)
DSR Testing (Part 2)
 
Introduction to refactoring
Introduction to refactoringIntroduction to refactoring
Introduction to refactoring
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
Presentation on java
Presentation  on  javaPresentation  on  java
Presentation on java
 
Migration strategies 4
Migration strategies 4Migration strategies 4
Migration strategies 4
 

Viewers also liked

Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2ASU Online
 
Adjacency And Incidence Matrix
Adjacency And Incidence MatrixAdjacency And Incidence Matrix
Adjacency And Incidence MatrixAbir Junayed
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummaryAmal Khailtash
 
4. python functions
4. python   functions4. python   functions
4. python functionsin4400
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagramsbarney92
 

Viewers also liked (11)

Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
Interface Vs Abstact
Interface Vs AbstactInterface Vs Abstact
Interface Vs Abstact
 
Interface
InterfaceInterface
Interface
 
Adjacency And Incidence Matrix
Adjacency And Incidence MatrixAdjacency And Incidence Matrix
Adjacency And Incidence Matrix
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagrams
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to Introduction to Java Programming Language

basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introductionjyoti_lakhani
 
ITFT - Java Coding
ITFT - Java CodingITFT - Java Coding
ITFT - Java CodingBlossom Sood
 
OOP with Java
OOP with JavaOOP with Java
OOP with JavaOmegaHub
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java BasicsFayis-QA
 
introduction to c #
introduction to c #introduction to c #
introduction to c #Sireesh K
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introductionSireesh K
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Anton Arhipov
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsRaja Sekhar
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele RialdiCodeFest
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for seleniumapoorvams
 
j-chap1-Basics.ppt
j-chap1-Basics.pptj-chap1-Basics.ppt
j-chap1-Basics.pptSmitaBorkar9
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updatesVinay H G
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Sagar Verma
 
Java Course — Mastering the Fundamentals
Java Course — Mastering the FundamentalsJava Course — Mastering the Fundamentals
Java Course — Mastering the Fundamentalsnehash4637
 

Similar to Introduction to Java Programming Language (20)

109842496 jni
109842496 jni109842496 jni
109842496 jni
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
ITFT - Java Coding
ITFT - Java CodingITFT - Java Coding
ITFT - Java Coding
 
OOP with Java
OOP with JavaOOP with Java
OOP with Java
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
 
Java introduction
Java introductionJava introduction
Java introduction
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele Rialdi
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
 
j-chap1-Basics.ppt
j-chap1-Basics.pptj-chap1-Basics.ppt
j-chap1-Basics.ppt
 
Java 8 selected updates
Java 8 selected updatesJava 8 selected updates
Java 8 selected updates
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
 
Java Course — Mastering the Fundamentals
Java Course — Mastering the FundamentalsJava Course — Mastering the Fundamentals
Java Course — Mastering the Fundamentals
 

Recently uploaded

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 

Recently uploaded (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 

Introduction to Java Programming Language

  • 1. Introduction to Java Programming Language Junji Zhi University of Toronto 1
  • 2. Content • • • • • • Java language Syntax “Hello World” program example Compiling, Running and Debugging Java code Inheritance Threading Synchronization 2
  • 3. Java programming Language • Some buzzwords for Java – – – – – – – – – – – “Write Once, Run Anywhere” Simple Object oriented Distributed Multithreaded Dynamic Architecture neutral Portable High performance Robust Secure 3
  • 4. Example: Hello World Program • Everything is in a class • One file, one public class • In the runnable public class: – public static void main(String [] args) 4
  • 5. Primitive Data Types • Primitive Data Types: byte, short, int, long, float, double, boolean, char • Arrays are also a class long [] a = new long[5]; – You can get the length by visiting the length field of array object a, like this: a.length • String class is very commonly used to represents character strings, for example String s1 = “Hello ”, s2 = “Wolrd!”; String s3 = s1 + s2; 5
  • 6. Operators (same as C/C++) [3] • • • • • ++,-- Auto increment/decrement +,- Unary plus/minus *,/ Multiplication/division % Modulus +,- Addition/subtraction 6
  • 7. Declaring Variables [3] int n = 1; char ch = „A‟; String s = “Hello”; Long L = new Long(100000); boolean done = false; final double pi = 3.14159265358979323846; Employee joe = new Employee(); char [] a = new char[3]; Vector v = new Vector(); 7
  • 8. Compared with C/C++ [3] • Java has no: – – – – – – – – – – pointers typedef preprocessor struct unions multiple inheritance goto operator overloading malloc … 8
  • 9. Declaring a class • • • • • package Class name Constructor Fields methods 9
  • 11. Java Development Process .java => .class => JVM execution 11
  • 12. Installing Java in your machine (1) • Downloading Java Development Kit (JDK) from Oracle • Java Runtime Environment (JRE) is usually included in the JDK installation file. 12
  • 13. Installing Java in your machine (2) • Setting JAVA_HOME (Windows): – E.g., C:Program FilesJavajdk1.7.0_45 • Setting path and classpath 13
  • 14. Compile .java File into a .class File (Command Line) 14
  • 15. Running HelloWorld in Eclipse IDE Eclipse Download from here. 15
  • 17. Debugging Java in Eclipse (1) • Debugging means “run a program interactively while watching the source code and the variables during the execution.” [5] • Set breakpoints to stop the program at the middle of execution • Eclipse has a Debug Mode 17
  • 18. Debugging Java in Eclipse(2) Image courtesy: http://www.vogella.com/tutorials/EclipseDebugging/images/xdebugstart20.gif.pagespeed.ic.SqCELlNeCm.png 18
  • 19. Debugging Java in Eclipse(3) Table courtesy: http://www.vogella.com/tutorials/EclipseDebugging/article.html 19
  • 21. Inheritance in Java • Java classes can be derived from other classes, thereby inheriting fields and methods from those classes. 21
  • 25. A Real World Example: ArrayList http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 25
  • 27. Java Threading • A thread is a thread of execution in a program [6] • JVM allows an application to have multiple threads running concurrently. • Apache Harmony example: http://harmony.apache.org/subcomponents/drlvm/TM.html 27
  • 28. Two ways to do threading 1. Extends Thread class 2. Implements Runnable interface http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html 28
  • 30. How to stop a Thread • Using Thread.interrupt() method: http://stackoverflow.com/questions/7786305/stopping-a-specific-java-thread 30
  • 32. Thread Interference (1) • Increment operation is translated to multiple steps by the virtual machine : 1. Retrieve the current value of c. 2. Increment the retrieved value by 1. 3. Store the incremented value back in c. Example from: http://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html 32
  • 33. Thread Interference (2) • • • • Assume we have 2 threads, A and B. A increments c, and B decrements c. Thread A and B runs together. One possible order of the low-level steps: 1. 2. 3. 4. 5. 6. Thread A: Retrieve c. Thread B: Retrieve c. Thread A: Increment retrieved value; result is 1. Thread B: Decrement retrieved value; result is -1. Thread A: Store result in c; c is now 1. Thread B: Store result in c; c is now -1. • Is the result correct? • What if the thread A and B are bank transactions? 33
  • 34. Problem Root Cause • Threads are visiting one field (resource) at the same time. • Multiple steps of an operation • No enforced “happen-before” relationship 34
  • 35. Solution: synchronized method Example: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html 35
  • 36. synchronized method • Enforce the ‘happen-before’ relationship in the method level. • Either one of the below instance will happen. But result is always 0, which is correct. 1. 2. 3. 4. 5. 6. Thread A: Retrieve c. Thread A: Increment retrieved value; result is 1. Thread A: Store result in c; c is now 1. Thread B: Retrieve c. Thread B: Decrement retrieved value; result is 0. Thread B: Store result in c; c is now 0. 1. 2. OR 3. 4. 5. 6. Thread B: Retrieve c. Thread B: Decrement retrieved value; result is -1. Thread B: Store result in c; c is now -1. Thread A: Retrieve c. Thread A: Increment retrieved value; result is 0. Thread A: Store result in c; c is now 0. 36
  • 37. synchronized statements (1) • Every object has an intrinsic lock associated with it • Primitive types (e.g., int, char) do not have intrinsic locks. • We can combine object intrinsic locks and synchronized keyword to create fine-grained synchronization control. 37
  • 39. synchronized statement hazards (1) • Boolean has only two instances of Boolean • If another thread also synchronizes on the same Boolean instance, like this: – private final Boolean someLock = Boolean.FALSE; • The lock will be reused. • The system might be deadlock or unresponsive. • It is hard to detect this type of bugs! More examples: https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused 39
  • 40. synchronized statement hazards (2) • Another example of the wrong way of using locks: What will happen another thread also synchronizes on an integer instance with the 0 integer value? https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused 40
  • 41. synchronized statement hazards (3) • Correct way of using locks: using new to instantiate an object https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused 41
  • 42. References 1. Thinking in Java 4th Ed, Bruce Eckel 2. Oracle Java tutorial (http://docs.oracle.com/javase/tutorial/index.ht ml) 3. www.cs.drexel.edu/~spiros/teaching/CS575/slid es/java.ppt 4. http://eclipsetutorial.sourceforge.net/Total_Begi nner_Companion_Document.pdf 5. http://www.vogella.com/tutorials/EclipseDebug ging/article.html 42

Editor's Notes

  1. Package
  2. Eclipse is automatically compiling your code on the fly.
  3. JVM implementations are also permitted to reuse wrapper objects for larger ranges of values. While use of the intrinsic lock associated with the boxed Integer wrapper object is insecure; instances of theInteger object constructed using the new operator (new Integer(value)) are unique and not reused. In general, locks on any data type that contains a boxed value are insecure.