J01 Java Environment
J01 Java Environment
3
Java Timeline
1991: SUN develops a programming
language for cable TV set-top boxes
Simple, OO, platform independent
1994: Java-based web browser
(HotJava), the idea of “applet” comes
out
1996: first version of Java (1.0)
Java timeline (cont’d)
1996: Netscape supports Java
Popularity grows
Java 1.02 released, followed by many updated releases in
close rounds
1997: Java 1.1 released, major leap over for the
language
1998: Java 2 platform (v. 1.2) released (libraries)
2005: Java 5 (language enhancements)
New features marked with
2006: Java 6 (Faster Graphics), goes open source
2011: Java 7 (I/O improvements)
5
OO language features
OO language provides constructs to:
Define classes (types) in a hierarchic way
(inheritance)
Create/destroy objects dynamically
Send messages (w/ dynamic binding)
No procedural constructs (pure OO
language)
no functions, class methods only
no global vars, class attributes only
6
Java features
Platform independence (portability)
Write once, run everywhere
Translated to intermediate language
(bytecode)
Interpreted (with optimizations, e.g. JIT)
High dynamicity
Run time loading and linking
Dynamic array sizes
Automatic garbage collection
7
Java features (cont’d)
Robust language, i.e. less error prone
Strong type model and no explicit pointers
– Compile-time checks
Run-time checks
– No array overflow
Garbage collection
– No memory leaks
Exceptions as a pervasive mechanism to
check errors
8
Java features (cont’d)
Shares many syntax elements w/ C++
Learning curve is less steep for C/C++
programmers
Quasi-pure OO language
Only classes and objects (no functions,
pointers, and so on)
Basic types deviates from pure OO...
Easy to use
9
Java features - Classes
There is only one first level concept:
the class
public class First {
}
The source code of a class sits in a
.java file having the same name
Rule: one file per class
Enforced automatically by IDEs
Case-wise name correspondence
10
Java features - Methods
In Java there are no functions, but only
methods within classes
The execution of a Java program starts
from a special method:
public static void main(String[] args)
In
In C
C we
we find
find the
the function:
function:
Note int
int main(int
main(int argc,
argc, char*
char* argv[])
argv[])
return type is void
args[0] is the first argument on the
command line (after the program name)
Build and run
First.java Output
javac First.java
Java
Virtual Machine
Java compiler
First.class
java -cp . First
bytecode
bytecode
Note:
Note: no
no
extension
extension
Building and running
Build environment Run-Time environment
Java Source Bytecode
(.java) Loader
Bytecode
Java Compiler
Verifier
(javac)
Just In Time
Interpreter (JIT) Compiler
Java ByteCode Java
(.class) Virtual
Machine
(JVM)
Run time
OS/HW
13
Dynamic class loading
JVM loading is based on the classpath:
list of locations whence classes can be
loaded
When class X is required:
For each location in the classpath:
– Look for file X.class
– If present load the class
– Otherwise move to next location
Example
File: First.java:
public class First {
public static void main(String[] args){
int a;
a = 3;
System.out.println(a);
}
}
Java features (cont’d)
Supports “programming in the large”
JavaDoc
Class libraries (Packages)
Lots of standard utilities included
Concurrency (thread)
Graphics (GUI) (library)
Network programming (library)
– socket, RMI
– applet (client side programming)
16
Types of Java programs
Application
It’s a common program, similarly to C
executable programs
Runs through the Java interpreter (java)
of the installed Java Virtual Machine
public class HelloWorld {
public static void main(String args[]){
System.out.println(“Hello world!”);
}
}
17
Types of Java programs
Applet (client browser)
Java code dynamically downloaded
Execution is limited by “sandbox”
Servlet (web server)
In J2EE (Java 2 Enterprise Edition)
Midlet (mobile devices, e.g.
smartphone and PDA)
In J2ME (Java 2 Micro Edition)
18
Java development environment
Java SE 7
(http://www.oracle.com/technetwork/java/javase)
javac compiler
jdb debugger
JRE (Java Run Time Environment)
– JVM
– Native packages (awt, swing, system, etc)
Docs
http://docs.oracle.com/javase/
Eclipse:
Integrated development environment (IDE)
http://www.eclipse.org/
19
Coding conventions
Use camelBackCapitalization for
compound names, not underscore
Class name must be capitalized
Method name, object instance name,
attributes, method variables must all
start in lowercase
Constants must be all uppercases (w/
underscore)
Indent properly
20
Coding conventions (example)
class ClassName {
21
Deployment - Jar
Java programs are packaged and
deployed in jar files.
Jar files are compressed archives
Like zip files
Contain some meta-information
It is possible to directly execute the
contents of a jar file from a JVM
JVM can load classes from within a JAR
Jar command
A jar file can be created using:
jar cvf my.jar *.class
The contents can be seen with:
jar tf my.jar
To run a class included in a jar:
java -cp my.jar First
The “-cp my.jar” option adds the jar to the
JVM classpath
Jar Main class
When a main class for a jar is defined,
it can executed simply by:
java -jar my.jar
To define a main class, a manifest file
must be added to the jar with:
jar cvfm my.jar manifest.txt
Main-Class: First
FAQ
Which is more “poweful”: Java or C?
Performance: C is better though non that
much better (JIT)
Ease of use: Java
Error containment: Java
How can I generate an “.exe” file?
You cannot. Use an installed JVM to
execute the program
GCJ: http://gcc.gnu.org/java/
FAQ
I downloaded Java on my PC but I
cannot compile Java programs:
Check you downloaded Java SDK
(including the compiler) not Java RTE or
JRE (just the JVM)
Check the path includes pathToJava/bin
Note: Eclipse uses a different compiler
than javac
FAQ
Java cannot find a class
(ClassNotFoundException)
The name of the class must not include
the extension .class:
– Es. java First
Check you are in the right place in your
file system
– java looks for classes starting from the
current working directory
Wrap-up session
Java is a quasi-pure OO language
Java is interpreted
Java is robust (no explicit pointers,
static/dynamic checks, garbage collection)
Java provides many utilities (data types,
threads, networking, graphics)
Java can used for different types of
programs
Coding conventions are not “just aesthetic”
28
License (1)
THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL"
OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK
OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF
THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF
SUCH TERMS AND CONDITIONS.
1. Definitions
– "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in
unmodified form, along with a number of other contributions, constituting separate and independent works in themselves,
are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work
(as defined below) for the purposes of this License.
– "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a
translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art
reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted,
except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this
License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of
the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this
License.
– "Licensor" means the individual or entity that offers the Work under the terms of this License.
– "Original Author" means the individual or entity who created the Work.
– "Work" means the copyrightable work of authorship offered under the terms of this License.
– "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this
License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this
License despite a previous violation.
2. Fair Use Rights. Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or
other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.
3. License Grant. Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free,
non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated
below:
a. to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work
as incorporated in the Collective Works;
b. to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a
digital audio transmission the Work including as incorporated in Collective Works;
The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights
include the right to make such modifications as are technically necessary to exercise the rights in other media and
formats, but otherwise you have no rights to make Derivative Works. All rights not expressly granted by Licensor are
hereby reserved, including but not limited to the rights set forth in Sections 4(d) and 4(e).
29
License (2)
4. Restrictions.The license granted in Section 3 above is expressly made subject to and limited by the following
restrictions:
a. You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this
License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of
the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms
on the Work that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder. You may
not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may
not distribute, publicly display, publicly perform, or publicly digitally perform the Work with any technological measures that
control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the
Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made
subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent
practicable, remove from the Collective Work any credit as required by clause 4(c), as requested.
b. You may not exercise any of the rights granted to You in Section 3 above in any manner that is primarily intended for or
directed toward commercial advantage or private monetary compensation. The exchange of the Work for other copyrighted
works by means of digital file-sharing or otherwise shall not be considered to be intended for or directed toward commercial
advantage or private monetary compensation, provided there is no payment of any monetary compensation in connection with
the exchange of copyrighted works.
c. If you distribute, publicly display, publicly perform, or publicly digitally perform the Work, You must keep intact all copyright
notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or
pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g.
a sponsor institute, publishing entity, journal) for attribution in Licensor's copyright notice, terms of service or by other
reasonable means, the name of such party or parties; the title of the Work if supplied; and to the extent reasonably practicable,
the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to
the copyright notice or licensing information for the Work. Such credit may be implemented in any reasonable manner;
provided, however, that in the case of a Collective Work, at a minimum such credit will appear where any other comparable
authorship credit appears and in a manner at least as prominent as such other comparable authorship credit.
d. For the avoidance of doubt, where the Work is a musical composition:
i. Performance Royalties Under Blanket Licenses. Licensor reserves the exclusive right to collect, whether
individually or via a performance rights society (e.g. ASCAP, BMI, SESAC), royalties for the public performance
or public digital performance (e.g. webcast) of the Work if that performance is primarily intended for or
directed toward commercial advantage or private monetary compensation.
ii. Mechanical Rights and Statutory Royalties. Licensor reserves the exclusive right to collect, whether
individually or via a music rights agency or designated agent (e.g. Harry Fox Agency), royalties for any
phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license
created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions), if Your
distribution of such cover version is primarily intended for or directed toward commercial advantage or
private monetary compensation.
– Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor reserves
the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the
public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the
US Copyright Act (or the equivalent in other jurisdictions), if Your public digital performance is primarily intended for or
directed toward commercial advantage or private monetary compensation.
30
License (3)
5. Representations, Warranties and Disclaimer
UNLESS OTHERWISE MUTUALLY AGREED BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO
REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR
OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR
PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF
ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF
IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.
6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES
ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
7. Termination
a. This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms
of this License. Individuals or entities who have received Collective Works from You under this License, however,
will not have their licenses terminated provided such individuals or entities remain in full compliance with those
licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
b. Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the
applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work
under different license terms or to stop distributing the Work at any time; provided, however that any such
election will not serve to withdraw this License (or any other license that has been, or is required to be, granted
under the terms of this License), and this License will continue in full force and effect unless terminated as stated
above.
8. Miscellaneous
a. Each time You distribute or publicly digitally perform the Work or a Collective Work, the Licensor offers to the
recipient a license to the Work on the same terms and conditions as the license granted to You under this
License.
b. If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or
enforceability of the remainder of the terms of this License, and without further action by the parties to this
agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and
enforceable.
c. No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or
consent shall be in writing and signed by the party to be charged with such waiver or consent.
d. This License constitutes the entire agreement between the parties with respect to the Work licensed here. There
are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall
not be bound by any additional provisions that may appear in any communication from You. This License may not
be modified without the mutual written agreement of the Licensor and You.
31