Anda di halaman 1dari 17

Introduction to Java

Simon Kroly
simon.karoly@codespring.ro
The Java programming language

High level, bytecode-based, object-oriented


Virtual Machine (VM) a software implementation of a machine
(computer) that executes programs.
Java VM executes bytecode instructions
JIT (Just in Time) compilers, optimization.
Obs.: there is also possibility to use AOT (Ahead of Time) compilers.

7/11/2016 2
Java history in a nutshell

Simula 67 objects, Smalltalk OOP


1983 C++
1991 Sun Microsystems, Stealth (Green) Project: the aim was to
create a new language for different devices (not only personal
computers), for creating heterogeneous networks of smart devices
with centralized control possibility.
Bill Joy, Mike Sheridan, Patrick Naughton, James Gosling
Oak Java
1994 Patrick Naughton: Hot Java Browser (WebRunner); Netscape
support
1995 Sun JavaSoft
1996 JDK (Java Development Kit) 1.0

7/11/2016 3
Java history in a nutshell

1998 J2SE 1.2 (Java 2 Platform Standard Edition) (JDK 1.2), J2EE
(Java 2 Platform Enterprise Edition), J2ME (Java 2 Platform Micro
Edition)
J2EE: application servers, component-based development, distributed
systems, web applications (EJB Enterprise Java Beans, JPA Java
Persistence API, JTA Java Transaction API, JMS Java Message
Service, Servlet API, JSP JavaServer Pages, JSF JavaServer Faces
etc.)
2005: there are 2.5 billions of electronic devices running Java,
approximately 4.5 millions of developers using Java.
2009-2010: the Sun Microsystems was bought by Oracle
There are more than 1 billion JRE downloads in a year, more than 10
millions of developers using Java, billions of devices running Java
(more than 3 billions of mobile devices).

7/11/2016 4
Java history in a nutshell

Development environments: Borland JBuilder, Oracle JDeveloper,


IntelliJ IDEA, Xinox JCreator etc.
Obs.: Microsoft J++ (MSJVM, supported by Visual Studio 6.0), followed
by J# (it was a part of the .NET framework).
NetBeans (netbeans.org):
Started in 1997 as a university project.
In 1999 it was bought by Sun, published as an open source IDE.
Eclipse (eclipse.org):
Started in 1998 as an IBM project. In 2001 the Eclipse consortium
was founded and Eclipse was published as an open source IDE.
Since 2004 it is supported by the Eclipse Foundation.
It is based on the Equinox implementation of the OSGi (Open
Service Gateway Initiative).
Relatives: Groovy, JRuby, Jython etc.
7/11/2016 5
Java SE version history

1996: JDK 1.0


1997: JDK 1.1 nested classes, JDBC, RMI, reflection, etc.
1998: J2SE 1.2 (Playground) SWING, JCF, JIT compiler, etc.
2000: J2SE 1.3 (Kestrel) HotSpot, RMI-IIOP, JNDI, etc.
2002: J2SE 1.4 (Merlin) chained exceptions, java.nio, logging, JAXP,
security and cryptography extensions etc.
2004: J2SE 5.0 (Tiger) generics, enumerations, auto-boxing,
annotations, for each loops, concurrency package, etc.
2006: Java SE 6 (Mustang) performance tuning, other extensions.
2011: Java SE 7 (Dolphin) small language modifications (exception
handling, switch etc.), other extensions.
2014: Java SE 8 (Spider) lambda expressions, default methods,
date and time API, repeatable annotations, etc.

7/11/2016 6
Main principles

Platform independency
Reliability:
E.g.: desktop application failure vs. mobile phone/ATM software
failures the aim is to reduce the possibility of programmers
mistakes, programming errors.
Only objects are used as data structures.
Proper exception handling.
No multiple inheritance.
Garbage collector
Security
No pointer arithmetic, no uncontrolled access to the memory.
Sandbox mechanism.

7/11/2016 7
Beyond the stage

Runtime environment: JRE (Java Runtime Environment) contains


the JVM, a launcher for running applications and core classes.
The JVM specification is an open standard, there are several
implementations. Suns reference implementation is HotSpot
(runnable in client and server mode, provides JIT compiler).
Obs.: in some special situations AOT compilers can also be used
(e.g. Excelsior Jet).
A Java application is delivered to the user as a collection of .class
files or a collection of packages (class files archived in .jar (or .zip)
format).
To launch the application the name of a .class file has to be
specified for the JRE. After some verifications on this file the main
method is invoked by the JRE (if there is a main method in the
class).
7/11/2016 8
Beyond the stage

Generally, external classes are required to launch an application:


standard classes from the Java platform (bootstrap classes e.g.
from the java.lang or java.util packages), other classes from the
application, extensions (classes from optional packages delivered
with the platform), classes from third-party packages.
The JVM needs to find these classes:
The bootstrap classes and extensions are automatically loaded (from the
/lib and /lib/ext folders within the JRE home directory) (obs.: the main
platform classes are included into the /lib/rt.jar archive)
For other classes the path has to be specified using the CLASSPATH
environment variable of the operating system (or passing command-line
arguments to the launcher).

7/11/2016 9
Example

Printing command-line arguments to the console:

public class Example {


public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}

Source code: Example.java


Compiling: javac Example.java ( Example.class)
Launching: java Example
Observations: UNICODE characters, file names, coding and naming
conventions
7/11/2016 10
Primitive data types

Primitive data types: byte (signed integer, 8 bit), short (signed integer, 16
bit), int (signed integer, 32 bit), long (signed integer, 64 bit), char (16 bit
Unicode character, UTF16 encoding), float (floating point, 32 bit), double
(floating point, 64 bit), boolean (the value can be true or false, the size is
not specified, it is JVM-dependent).
Wrapper classes: Byte, Short, Integer, Long, Character, Float, Double,
Boolean

Examples:
int i1 = 5; i2 = new Integer(i1);
Integer i2 = new Integer(5); float f = i2.floatValue();
Integer i3 = new Integer(i1); char c = a;
String s = "10"; if (Character.isLowerCase(c))
int i1 = Integer.parseInt(s); c = Character.toUpperCase(c);

7/11/2016 11
Reference type

The objects are identified by references.


Reference: refers to an object in memory (similarity with C++
pointers, but there are no pointer operators)
Examples:

String s1 = "Hello";
String s2 = s1;
String s3;
s3 = new String("Hello");

The implicit value for a reference is null. Memory allocation for the
referenced object is done using the new operator.

7/11/2016 12
Reference type

Copying and comparing values clone and equals methods


The objects and arrays are always identified by references, but
method parameters are always passed by value (not passed by
reference). Java passes objects as references and those references
are passed by value (a copy is created when passing the reference).
Even so, the objects can be modified within the methods through
these references (these modifications remain valid after the method
call).

7/11/2016 13
Autoboxing and auto-unboxing

Tiger: autoboxing and auto-unboxing mechanisms


Example:
int i; int i;
Integer j; Integer j;
i = 1; i = 1;
j = new Integer(2); j = 2;
i = j.intValue(); i = j;
j = new Integer(i); j = i;
Application example:
int i1 = 10;
Vector v = new Vector();
v.add(i1);
Obs.: more convenient programming, but there are possibilities for mistakes.
E.g. object identity (reference equality) will be verified by the == operator. In
addition, the wrapper class caching mechanism could lead to further
misunderstandings.

7/11/2016 14
Arrays

Objects, subclasses of the Object superclass.


Array elements are of the same type, array dimensions are always known and
the limits are always verified.
If an array is accessed using an illegal index (negative value or a value greater
than or equal with the dimension) an exception is thrown.
Array elements can be other arrays. (In fact, multi-dimensional arrays are
arrays containing other arrays.)
Examples:
int ti[];
ti = new int[5];
ti = {1, 2, 3, 4, 5};
ti = new int[10];
for (int i = 0; i < 10; i++)
ti[i] = i;

int ti[][] = new int[5][];


ti[0] = new int[2];
ti[1] = new int[3];
7/11/2016 15
Exception handling in a nutshell

try {
// Could throw exception
} catch(Exception1 object1) {
// Handling an exception of type Exception1
} catch(Exception2 object2) {
// Handling an exception of type Exception2
} finally {
// Will be always executed
}

Exceptions are subclasses of the Exception superclass


If an exception is thrown, the first catch block corresponding to that
exception type will be executed.
There is an exception hierarchy. The order of the catch blocks could
be important (unreachable code -> compilation error).

7/11/2016 16
Exception handling - example

public class ExceptionExample {


public static void main(String[] args) {
int i;
try {
i = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e1) {
i = 10;
} catch (NumberFormatException e2) {
i = 20;
} finally {
i++;
}
System.out.println(i);
}
}

7/11/2016 17

Anda mungkin juga menyukai