Learning objectives � Understand the basic features of Java � What are portability and robustness? � Understand the concepts of bytecode The Java Environment The Java Environment and interpreter � What is the JVM? � Learn few coding conventions � How shall I name identifiers? Version 1.1 Oct 2006 2 Java timeline Java timeline (cont’d) � 1996: Netscape supports Java � 1991: SUN develops a programming � Popularity grows language for cable TV set-top boxes � 1996: Java 1.02 released, followed by many � Simple, OO, platform independent updated releases in close rounds � 1994: Java-based web browser � 1997: Java 1.1 released, major leap over for (HotJava), the idea of “applet” comes the language out � 1998: Java 2 platform (1.2 ver) released (libraries) � 1996: first version of Java (1.0) � 2005: Java 5 (language enhancements) � New features marked with 3 4
OO language features Java features � OO language provides constructs to: � Platform independence (portability) � Write once, run everywhere � Define classes (types) in a hierarchic way � Translated to intermediate language (inheritance) (bytecode) � Create/destroy objects dynamically � Interpreted (with optimizations, e.g. JIT) � Send messages (w/ dynamic binding) � High dynamicity � No procedural constructs ( pure OO � Run time loading and linking language) � Dynamic array sizes � no functions, class methods only � Automatic garbage collection � no global vars, class attributes only 5 6 Java features (cont’d) Java features (cont’d) � Robust language, i.e. less error prone � Shares many syntax elements w/ C++ � Learning curve is less steep for C/C++ � Strong type model and no pointers programmers – Compile-time checks � Quasi-pure OO language � Run-time checks � Only classes and objects (no functions, – No array overflow pointers, and so on) � Garbage collection � Basic types deviates from pure OO... – No memory leaks � Easy to use � Exceptions as a pervasive mechanism to check errors 7 8
Java features - Methods Java features - Classes � There is one first level concepts: the � In Java there are no functions, but only methods methods within classes class � The execution of a Java program starts publ i c cl ass Fi r st { publ i c cl ass Fi r st { from a special method: } � The source code of a class sits in a � publ i c st at i c voi d m publ i c st at i c voi d m ai n( St r i ng[ ] ar gs) ai n( St r i ng[ ] ar gs) .java file having the same name � Note � return type is voi d � Rule: one file per class voi d ar gs[ 0] is the first argument on the � Enforced automatically by IDEs � ar gs[ 0] command line (after the program name) 9 Build and run Building and running (simple) Build environment Run-Time environment First.java Output Byte code Java Source Loader ( .java ) javac First.java Java Byte code Java Compiler Verifier Virtual Machine ( javac ) Java compiler Just In Time (JIT) Interpreter Compiler Java ByteCode Java ( .class ) Virtual Machine (JVM) First.class Run time java -cp . First bytecode OS/HW Note: no extension 12
Example Java features (cont’d) � Supports “programming in the large” � File: First.java: � JavaDoc � Class libraries (Packages) public class First { � Lots of standard utilities included public static void main(String[] args){ � Concurrency (thread) int a; a = 3; � Graphics (GUI) (library) System.out.println(a); � Network programming (library) } – socket, RMI } – applet (client side programming) 14 Types of Java programs Types of Java programs � Application � Applet (client browser) � It’s a common program, similarly to C � Java code dynamically downloaded executable programs � Execution is limited by “sandbox” � Runs through the Java interpreter ( java ) � Servlet (web server) of the installed Java Virtual Machine � In J2EE (Java 2 Enterprise Edition) � Midlet (mobile devices, e.g. public class HelloWorld { public static void main(String args[]){ smartphone and PDA) System.out.println(“Hello world!”); � In J2ME (Java 2 Micro Edition) } } 15 16
Java development environment Coding conventions � Use camelback capitalization for � JSE 1.5.0_08 compound names, not underscore � javac compiler � Class name must be capitalized � jdb debugger � JRE (Java Run Time Environment) � Method name, object instance name, – Interpreter attributes, method variables must all – Native packages (awt, swing, system, etc) start in lowercase � Docs � Constants must be all uppercases (w/ � http://java.sun.com/j2se/1.5.0/docs/ underscore) � Eclipse editor � Indent properly � http://www.eclipse.org/ 17 18 Coding conventions (example) Wrap-up session � Java is a quasi-pure OO language class ClassName { � Java is interpreted const double PI = 3.14; � Java is robust (no pointers, static/dynamic checks, garbage collection) private int attributeName; � Java provides many utilities (data types, threads, networking, graphics) public void methodName { int var; � Java can used for different types of if ( var==0 ) { programs } � Coding conventions are not “just aesthetic” } } 19 20
FAQ FAQ � Which is more “powefull”: Java or C? � I downloaded Java on my PC but I cannot compile Java programs: � Performance: C is better though non that much better (JIT) � Check you downloaded Java SDK � Ease of use: Java (including the compiler) not Java RTE or JRE (just the JVM) � Error containment: Java � Check that the shell path include � How can I generate an “.exe” file? pathToJava/bin � You don't do it. Use an installed JVM to � Note: Eclipse uses a different compiler execute the program than javac � GCJ: http://gcc.gnu.org/java/ FAQ � Java cannot find a class ( ClassNotFoundException ) � The name of the class must not include the extension .class : – Es. java Prova � Check you are in the right place in your file system – java looks for classes starting from the current working directory
Recommend
More recommend