Java, language differences (non-syntax) compilation, execution, file structure, memory management
Java, language differences (non-syntax)
cs382- translating java code
cs330 - what is a module?,
structure of a basic python program
comprehensions - an overview
Qualities/Design Goals
DPOSDMAHRS
Dynamic
Portable
what makes java portable and archtecture neutral?
Hybrid language
step 1. source code(.java) compiled into object(.class, bytecode)
step 2. JVM interprets the object(byte)code
2 advantages
programs can run on any cpu as long as JVM is installed
interpretation in JVM takes less time since it is interpreting low-level(object/byte) code, not high-level like purely interpreted langs(python)
Object Oriented
Simple
example
a c++ programmer can pick up java with little training needed
Secure
Distributed
Multithreaded
Architecture neutral
High Performance
this has been mproved in java due to the recent introduction of
in the case of loops, the statment within a loop must be interpreted each time around the loop
execution times are especially long
code executed immediately after editing, no compilation step in between
execution time is increased since each line must be interpreted each time the program is executed
1 advantage
1 disadvantage
Interpreted language
Python
High Level language
C++
Compiled language
Source code compiled into
object code
Linked(translated) into
machine code
executable
Execution
symbolic addresses replaced with real addresses
machine
a single processor, or an entire computer
native code
machine code readable by the machine that the program is to be run on
aka native machine code
by the
Linker
machine code with symbolic addresses
Preprocessor
optional, in the case of c/c++, turns source code into preprocessed source code
preprocessor statements, ex: #include
program structure determined by #include statements
program structure in c++ vs java
filenames in java must match their class name(including capitalization), in c++ thats just a suggestion for the header/imp filenames
java class Foo will have its source code stored in
and its compiled code stored in
Foo.class
Foo.java
package named Baz, containing classes Foo and Bar will have their source and compiled code stored in
Baz/Foo.java
Baz/Foo.class
Baz/Bar.java
Baz/Bar.class
java uses relations between class/file names to find the source/compiled code for classes mentioned in a program
Compiler
javac
in java object code is stored in a .class file as bytecode
JVM interprets the bytecode
it is not uncommon to have multiple .class files after compiling
default java compiler
both make sure that file structure matches the program structure
source code is compiled into an executable file, written in native machine code
code is executed as-is
Interpreter
no native/machine code is ever produced in java
executes the code while reading/writing input/output
No compiler
No Object code
No executable
No linker
No preprocessor
Just-in-time compilation
reduces execution time in cases where methods are executed frequently(functions/blocks of code are called frequently like in a loop)
reduces JVM overhead
overhead
increased computation time/resources(ex:memory or bandwidth)
converts bytecode to native machine code at runtime
removing the need for the JVM to interpret it
helps ensure memory is available
Robust
what makes java robust?
automatic garbage collection
garbage
memory allocated on the heap that is no longer reachable by pointer
example
linkedList* someList = new LinkedList();
someList = nullptr;
capacity of a computer system to handle the errors during execution and manage the incorrect input of data
JDK
provides:
Includes the JRE, JVM, Javac, Jar, Javadoc
JAR
7 benefits of Jar files
SDCPPPP
packaged for sealing
Compressed
Decreased Download Time
leading to
because it is
packaged for extension
allows your software to become extensions of the java platform
portable
Java core api provides handling for jar files
any platform with java on it can handle jars
packaged for versioning
jars contain version and vendor info
jar feature that allows the package to enforce version consistency
sealed when all the classes and source code of each class belonging to that package are stored in the same jar
package
named with the URL of the author/organization that wrote the code contained within
can contain multiple other packages
directory/folder containing the source and compiled code for each class we want in our package
(in java) a group of classes
as well as time of creation
result in multiple jars with a different time of creation
creating a jar with the same name and inputs multiple times will
jar cf jar-file input-file(s)
create jar w/default manifest file
jar commands
view jar contents
jar tf jar-file
extract a jar file's contents
extract certain files from a jar
jar xf jar-file certain-files(s)
jar xf jar-file
run an application packaged as a jar file
requires 2 things
2.
1.
java -jar app.jar
java command
execute a java file in another directory while you're in a different one
java -cp
java -cp allInOne.jar RunAnywhere
vs
java -jar allInOne.jar
1
The first java command uses the -cp option to tell where to look for compiled code (in the jar file) and explicitly names the program (RunAnywhere) we want to look for
both should work
2
The second simply states that we want to execute the default program in that jar file
create/edit the main-class manifest header without needing to create or edit it first
e flag
2 ways to set entrypoint
entrypoint of a jar app
set's jar application's entrypoint
does what
main-class manifest header
ex: entrypoint of our jar app is class Foo, what would we put in the manifest?
Main-Class: Foo (followed by an enter)
located in manifest file
is
class with a method with the signature public static void main(string[] args)
manifest file with:
1.Main-Class manifest header
2.listing a class(that must exist) w/ method signature:
public static void main(string[] args)
followed by a newline/carriage
return
must have:
manifest file
must be named Manifest.txt
only 1 per jar file
default manifest
created when a jar is created
contains manifest specification version and JDK version
does not contain information on other files within the jar, but can be modified to do so
2 steps to do so
create a text file with the information you want to add
use the Jar tool's m option to add it to the manifest
used to include information from another manifest file or add custom info to the manifest during the creation of a jar file
ex
jar cmf jar-file existing-manifest input-file(s)
//f and m must be in the same order as the order of the corresponding argument
m option
ex
jar cfm jar-file manifest-addition input-file(s)
information contained within depends on what you plan to use the jar for
invoke an applet packaged in a jar file
<applet code=AppletClassName.class
archive="JarFileName.jar"
width=width height=height>
</applet>
encoded in UTF-8
edit the manifest file's main-class header
entrypoint flag
Main.class is in a package called foo, set the entry point with the e flag
jar cfe Main.jar foo.Main foo/Main.class
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
change this to set myApp as the entrypoint
jar cfe MyJar.jar myApp myApp.class
options
v option
verbose output, spits out name of each file as its added to the jar file while the jar is being built to stdout
0 option
jar file will not be compressed
c option
creating file(ex: jar)
can be in any order, but no spaces between them
f option
output will go to a file not stdout
given this class hierarchy, what command would you use to create a jar file, given audio and images are directories?
jar cvf TicTacToe.jar TicTacToe.class audio images
//this will output each file name being added from the directories thanks to the v option
-c option
change the directory during execution of a command
compare the effects of the -C and . options in the second command to the results of the first command:
jar cf ImageAudio.jar images audio
jar cf ImageAudio.jar -C images . -C audio .
-C images . // go to images directory, archive the files in there
-C audio. //go to audio directory, archive files in there
using this structure
results/output/file hierarchy of first command:
META-INF/MANIFEST.MF
images/cross.gif
images/not.gif
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
results/output/file hierarchy of second command:
META-INF/MANIFEST.MF
cross.gif
not.gif
beep.au
ding.au
return.au
yahoo1.au
yahoo2.au
default manifest will not be made
M option
*
wildcard
used to avoid having to type out every file/directory name you want to include in a command, as long as there are no unwanted files in the file/directory preceding the symbol
ex
how would we build a jar file for TicTacToe.jar if we wanted to include every file in the TicTacToe.jar directory?
jar cvf TicTacToe.jar *
jar -cf allInOne.jar .class
vs
jar -cfe allInOne.jar RunAnywhere .class
Use the first form if you are simply packing a group of “utility” classes, none of which is a full executable application, or if you are choosing to package together several different programs, none of which is more deserving of a “default” status than any other.
delete all the class files in a directory
rm *.class
show possible directories/files with names that begin with the same character
ls beginningOfDirName/*
Secure
Jar files can be digitally signed by their author
users of the software who recognize the signature can choose to grant it permissions
a single archive containing all the class files and auxilary resourcess needed for certain applets/applications
due to this bundling, only one HTTP transaction is needed, instead of one per file
is a variety of zip file
zip files
good for 4 things
compression
advantages/disadvantages
reduced file size, decreased download time
requires extraction, increased time to load the program since it requires a decompressing operation
decompression
archiving
unarchiving
meaning this command will work
unzip -l allInOne.jar
Java Archiver/Java archive tool, java archive file
JVM
Java virtual machine
an instance of this is created when the java launcher tool is ran
Java Development Kit
java platform
is unique because it is a platform made purely of software that runs on top of other platforms such as
windows, linux, mac os
typical platforms
hardware or software in which a program runs, often described as a cross between the operating system and underlying hardware
Platform
includes two things
1
2
java API
provides wide array of classes for us to use in our code
User interface toolkits to build GUIs
JavaFX
Java 2D
Swing
application programming interface
program structures
c++
#include
header
implementation(.cpp)
java
import
package
click to edit
python
_import_
package
module(.py)
program
from: cs333 - Straight-Line Computation in C++
collection of instructions for manipulating data
data types
categories of data types
primitive data types
type builders
c++
java
python
data
instructions
abstract data types
classes
3 kinds of type builders
arrays
structured types
address types
3 kinds of primitives in c++
integer numbers
floating point numbers
logical values
bool
boolean
float
double
int
long int
unsigned int
unsigned long
char