Language: Beginners To Advance
Language: Beginners To Advance
Language: Beginners To Advance
LANGUAGE
BEGINNERS TO ADVANCE
www.genial-code.com
Java language Syntax
“Hello World” program example
Compiling, Running and Debugging Java code
Inheritance
Threading
Synchronization
2
Some buzzwords for Java
“Write Once, Run Anywhere”
Simple
Object oriented
Distributed
Multithreaded
Dynamic
Architecture neutral
Portable
High performance
Robust
Secure
3
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: 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
5
++,-- Auto increment/decrement
+,- Unary plus/minus
*,/ Multiplication/division
% Modulus
+,- Addition/subtraction
6
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
Java has no:
pointers
typedef
preprocessor
struct
unions
multiple inheritance
goto
operator overloading
malloc
…
8
package
Class name
Constructor
Fields
methods
9
10
.java => .class => JVM execution
11
Downloading Java Development Kit (JDK) from Oracle
Java Runtime Environment (JRE) is usually included in the JDK
installation file.
12
Setting JAVA_HOME (Windows):
E.g., C:\Program Files\Java\jdk1.7.0_45
13
14
Eclipse Download from here.
15
16
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
Image courtesy: 18
http://www.vogella.com/tutorials/EclipseDebugging/images/xdebugstart20.gif.pagespeed.ic.SqCELlNeCm.png
Table courtesy: http://www.vogella.com/tutorials/EclipseDebugging/article.html 19
20
Java classes can be derived from other
classes, thereby inheriting fields and
methods from those classes.
21
22
23
24
25
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
26
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
http://harmony.apache.org/subcomponents/drlvm/TM.html
1. Extends Thread
class
2. Implements
Runnable
interface
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html 28
29
Using Thread.interrupt() method:
30
http://stackoverflow.com/questions/7786305/stopping-a-specific-java-thread
31
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.
33
Threads are visiting one field (resource) at the same time.
Multiple steps of an operation
No enforced “happen-before” relationship
34
35
Example: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
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.
37
38
http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
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;
More examples:
https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+
39
Another example of the wrong way of using locks:
https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+
40
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+
41
1. Thinking in Java 4th Ed, Bruce Eckel
2. Oracle Java tutorial
(http://docs.oracle.com/javase/tutorial/index.html)
3. www.cs.drexel.edu/~spiros/teaching/CS575/slides/java.pp
t
4. http://eclipsetutorial.sourceforge.net/Total_Beginner_Comp
anion_Document.pdf
5. http://www.vogella.com/tutorials/EclipseDebugging/article
.html
42