Introduction to Java Programming Language Junji Zhi University of Toronto 1.

Slides:



Advertisements
Similar presentations
wwwcsif.cs.ucdavis.edu/~jacksoni
Advertisements

Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Introduction to Java Programming
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Netprog 2002 Java Intro1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network.
A Short Introduction to JAVA
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java. Features of Java Architecture Neutral and Portable o Write once, run anywhere o Java Virtual Machine (JVM) Object Oriented o Classes encapsulate.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Applied Computing Technology Laboratory QuickStart Java Learning to Program in Java Dr. Tom Way October 21, 2005.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
introductory lecture on java programming
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
AP Refresher Module Introduction to Java Advanced Programming. All slides copyright: Chetan Arora.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
Object Oriented Programming in
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Chapter No. : 1 Introduction to Java.
Internet and Java Foundations, Programming and Practice
Website: SSD1 Unit 2 Intro to Java Presentation 2.3 Website:
Getting Started ARCS Lab..
Introduction to Programming in Java
Unit 41 – Programing in Java
An Introduction to Java – Part I, language basics
Units with – James tedder
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Applying OO Concepts Using Java
(Computer fundamental Lab)
Presentation transcript:

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 = ; 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 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 Files\Java\jdk1.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.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: 18

Debugging Java in Eclipse(3) Table courtesy: 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 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: 27

Two ways to do threading 1.Extends Thread class 2.Implements Runnable interface 28

Thread lifecycle 29

How to stop a Thread Using Thread.interrupt() method: 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: 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.Thread A: Retrieve c. 2.Thread B: Retrieve c. 3.Thread A: Increment retrieved value; result is 1. 4.Thread B: Decrement retrieved value; result is Thread A: Store result in c; c is now 1. 6.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: 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.Thread A: Retrieve c. 2.Thread A: Increment retrieved value; result is 1. 3.Thread A: Store result in c; c is now 1. 4.Thread B: Retrieve c. 5.Thread B: Decrement retrieved value; result is 0. 6.Thread B: Store result in c; c is now 0. 1.Thread B: Retrieve c. 2.Thread B: Decrement retrieved value; result is Thread B: Store result in c; c is now Thread A: Retrieve c. 5.Thread A: Increment retrieved value; result is 0. 6.Thread A: Store result in c; c is now 0. OR 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) 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! 39 More examples:

synchronized statement hazards (2) 40 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?

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

References 1.Thinking in Java 4 th Ed, Bruce Eckel 2.Oracle Java tutorial ( ml) ml 3. es/java.ppt‎ 4. nner_Companion_Document.pdfhttp://eclipsetutorial.sourceforge.net/Total_Begi nner_Companion_Document.pdf 5. ging/article.htmlhttp:// ging/article.html 42