J01-JavaEnvironment
J01-JavaEnvironment
Version 3.3.1
© Maurizio Morisio, Marco Torchiano, 2023
Java Timeline
§ 1991: develops a programming
language for cable TV set-top boxes
w Simple, OO, platform independent
§ 1994: Java-based web browser
(HotJava),
w The idea of “applet” appears
§ 1996: first version of Java (1.0)
9
Java features
§ Platform independence (portability)
w Write once, run everywhere
w Translated to intermediate language
(bytecode)
w Interpreted (with optimizations, i.e. JIT)
§ High dynamicity
w Run time loading and linking
w Dynamic array sizes
Java features (cont’d)
§ Robust language, less error prone
w Strong type model and no explicit
pointers
– Compile-time checks
w Run-time checks
– No array overflow
w Garbage collection
– No memory leaks
w Exceptions as a pervasive mechanism to
check errors
11
Java features (cont’d)
§ Shares many syntax elements w/ C++
w Learning curve is less steep for C/C++
programmers
§ Quasi-pure OO language
w Only classes and objects (no functions,
pointers, and so on)
w Basic types deviates from pure OO...
§ Easy to use
12
Java features (cont’d)
§ Supports “programming in the large”
w JavaDoc
w Class libraries (Packages)
§ Lots of standard utilities included
w Concurrency (thread)
w Graphics (GUI) (library)
w Network programming (library)
– socket, RMI
– applet (client side programming)
13
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
w Rule: one file per class
w Enforced automatically by IDEs
w Case-wise name correspondence
14
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)
First.java Output
javac First.java
Java
Virtual Machine
Java compiler
First.class
java First
bytecode
Note: no extension
Cafe Babe
§ Magic number
w Specific initial sequence of a file that let
identify the type of the file
w E.g. PDF files starts with chars “%PDF”
§ Java class files starts with the
following 4 bytes:
0xCAFEBABE
Java Ecosystem
§ Java language
§ Java platform
w JVM
w Class libraries (API)
w SDK
Dynamic class loading
§ JVM loading is based on the classpath:
w locations whence classes can be loaded
§ When class X is required:
w For each location in the classpath:
– Look for file X.class
– If present load the class
– Otherwise move to next location
Example: source code
File: First.java:
public class First {
public static void main(String[] args){
int a;
a = 3;
System.out.println(a);
}
}
Example: execution Name of the class
23
Types of Java programs
Deprecated since Java 9
§ Applet (client browser)
w Java code dynamically downloaded
w Execution is limited by “sandbox”
§ Servlet (web server)
w In J2EE (Java 2 Enterprise Edition)
§ Midlet (mobile devices)
w In J2ME (Java 2 Micro Edition)
§ Android App (Android device)
w Java
24
Java development environment
§ Java SE 17
(http://www.oracle.com/technetwork/java/javase)
w javac compiler
w jdb debugger
w JRE (Java Run Time Environment)
– JVM
– Native packages (awt, swing, system, etc)
§ Docs
w http://docs.oracle.com/javase/8/
§ Visual Studio Code + Java Extension Pack
w https://code.visualstudio.com
w Integrated development environment (IDE)
25
Coding conventions
§ Use camelBackCapitalization for
compound names, not underscore
§ Class name must be Capitalized
§ Method names, object instance names,
attributes, method variables must all
start in lowercase
§ Constants must be all uppercases (w/
underscore)
§ Indent properly
26
Coding conventions (example)
class ClassName {
27
Deployment - Jar
§ Java programs are packaged and
deployed in jar files.
§ Jar files are compressed archives
w Like zip files
w Contain additional meta-information
§ It is possible to directly execute the
contents of a jar file from a JVM
w 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
w 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 “powerful”: Java or C?
w Performance: C is better though non that
much better (JIT)
w Ease of use: Java
w Error containment: Java
§ How can I generate an “.exe” file?
w You cannot. Use an installed JVM to
execute the program
w GCJ: http://gcc.gnu.org/java/
FAQ
§ I downloaded Java on my PC but I
cannot compile Java programs:
w Check you downloaded Java SDK
(including the compiler) not Java RTE or
JRE (just the JVM)
w Check the path includes pathToJava/bin
§ Note: Eclipse uses a different compiler
than javac
FAQ
§ Java cannot find a class
(ClassNotFoundException)
w The name of the class must not include
the extension .class:
– Es. java First
w Check you are in the right place in your
file system
– java looks for classes starting from the
current working directory
Wrap-up
§ 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”
34