introduction to java programming language n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Java Programming Language PowerPoint Presentation
Download Presentation
Introduction to Java Programming Language

Loading in 2 Seconds...

play fullscreen
1 / 42

Introduction to Java Programming Language - PowerPoint PPT Presentation


  • 211 Views
  • Uploaded on

Introduction to Java Programming Language. Junji Zhi University of Toronto. Content. Java language Syntax “Hello World” program example Compiling, Running and Debugging Java code Inheritance Threading Synchronization. Java programming Language. Some buzzwords for Java

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Java Programming Language' - london


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to java programming language

Introduction to Java Programming Language

JunjiZhi

University of Toronto

content
Content
  • Java language Syntax
  • “Hello World” program example
  • Compiling, Running and Debugging Java code
  • Inheritance
  • Threading
  • Synchronization
java programming language
Java programming Language
  • Some buzzwords for Java
    • “Write Once, Run Anywhere”
    • Simple
    • Object oriented
    • Distributed
    • Multithreaded
    • Dynamic
    • Architecture neutral
    • Portable
    • High performance
    • Robust
    • Secure
example hello world program
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)
primitive data types
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;

operators same as c c 3
Operators (same as C/C++) [3]
  • ++,-- Auto increment/decrement
  • +,- Unary plus/minus
  • *,/ Multiplication/division
  • % Modulus
  • +,- Addition/subtraction
declaring variables 3
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();

compared with c c 3
Compared with C/C++ [3]
  • Java has no:
    • pointers
    • typedef
    • preprocessor
    • struct
    • unions
    • multiple inheritance
    • goto
    • operator overloading
    • malloc
declaring a class
Declaring a class
  • package
  • Class name
  • Constructor
  • Fields
  • methods
java development process
Java Development Process

.java => .class => JVM execution

installing java in your machine 1
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.
installing java in your machine 2
Installing Java in your machine (2)
  • Setting JAVA_HOME (Windows):
    • E.g., C:\Program Files\Java\jdk1.7.0_45
  • Setting path and classpath
running helloworld in eclipse ide
Running HelloWorld in Eclipse IDE

Eclipse Download from here.

debugging java in eclipse 1
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
debugging java in eclipse 2
Debugging Java in Eclipse(2)

Image courtesy: http://www.vogella.com/tutorials/EclipseDebugging/images/xdebugstart20.gif.pagespeed.ic.SqCELlNeCm.png

debugging java in eclipse 3
Debugging Java in Eclipse(3)

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

inheritance in java
Inheritance in Java
  • Java classes can be derived from other classes, thereby inheriting fields and methods from those classes.
a real world example arraylist
A Real World Example: ArrayList

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

java threading1
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

two ways to do threading
Two ways to do threading
  • Extends Thread class
  • Implements Runnable interface

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

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

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

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

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

thread interference 2
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:
    • 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?
problem root cause
Problem Root Cause
  • Threads are visiting one field (resource) at the same time.
  • Multiple steps of an operation
  • No enforced “happen-before” relationship
solution synchronized method
Solution: synchronized method

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

synchronized method
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.
  • 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.
  • 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.

OR

synchronized statements 1
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 keywordto create fine-grained synchronization control.
synchronized statements 2
synchronized statements (2)

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

synchronized statement hazards 1
synchronized statement hazards (1)
  • Boolean has only two instances of Boolean
  • If another thread also synchronizes on the same Boolean instance, like this:
    • privatefinalBoolean 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

synchronized statement hazards 2
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

synchronized statement hazards 3
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

references
References
  • Thinking in Java 4th Ed, Bruce Eckel
  • Oracle Java tutorial (http://docs.oracle.com/javase/tutorial/index.html)
  • www.cs.drexel.edu/~spiros/teaching/CS575/slides/java.ppt‎
  • http://eclipsetutorial.sourceforge.net/Total_Beginner_Companion_Document.pdf
  • http://www.vogella.com/tutorials/EclipseDebugging/article.html