Lecture 2: Do you speak Java?

Download Report

Transcript Lecture 2: Do you speak Java?

Lecture 2: Do you speak
Java?
From Problem to Program
• Last Lecture we looked at modeling with objects!
• Steps to solving a business problem
– Investigate current problem (Analysis)
– Transform this model to logical models (Design)
– Transform this model to software (implementation)
• Need to translate model to something computers
can understand (mathematical equations)
Parlez Vous Binary?
• 1001110000010101010000001111
• How do we translate our business problem into
machine code?
• Initially, a programmer writes a program in a
particular programming language. This is called
source code!!!!!
• To execute the program however, the
programmer must translate it into machine
language……the language that the computer
understands. How do we do this?
– A COMPILER does this!!!!!
Compiler
• A compiler converts source code into
machine level instructions
• Almost every programming language
comes with a compiler
• In effect, the compiler is the language
because it defines which instructions are
acceptable
Javac
• The Java compiler (javac) is the component of
the Java Developers kit (JDK) USED TO
TRANSFORM Java source code files into
bytecode (not machine code)
– Javac phil.java
• Bytecode is the compiled format for Java
programs. Once a Java program has been
converted to bytecode, it can be transferred
across a network and executed by Java Virtual
Machine
Java Virtual Machine
• Bytecode is not really machine code and cannot
be used by a computer – it must be interpreted
• The Java Virtual Machine (JVM) is an
interpreter, it interprets byte code and can thus
execute quickly
• The Java interpreter is built into Java compatible
web browsers such as Netscape navigator which
enables applets to run
Why Compile and Interpret?
• Because byte code is used instead of machine
code, a java program can be executed on any
machine that has a Java interpreter
• This makes java extremely portable……a
program can be compiled on one machine and
executed on any
• Java used extensively on web sites
• Java not only offers portability but identical
behaviour on different systems
• “WRITE ONCE, RUN EVERYWHERE”
Compiling and interpreting
• Java source code is written with a text editor and saved
in plain ASCII text with file extension .java
• These files are compiled using the javac compiler
– E.g. javac Examplehelloworld.java
• Executable bytecode class files have the extension
.class and they represent a Java class in its usable form
• The Java compiler generates exactly one .class file for
each class you create. It is possible to define more than
one class in a single source file, it is therefore possible
for the compiler to generate multiple class files from a
single source file.
Java and the Web
• Applet written and compiled in exact same
way as Java application
• Applets downloaded when user navigates
to page
• Browsers contain Java virtual machine
and applet is executed on client machine
Java Applets
• Applets were originally small applications but there
is no size restriction on Applets
• As applets are downloaded, there is a risk that they
may
– Contain Bugs
– Be written maliously
• However, browsers are usually configured to prevent
applets from accessing files on the machine on
which they execute
• Applets can access files on the server from which
they were downloaded
• Java applications will be covered in this course but
applets will be revisited