Systems Programming Programming Systems First steps in Java Telematics Engineering M. Carmen Fernández Panadero <mcfp@it.uc3m.es>
Scenary I: Install and configure the environment • Today is your first day at work in the programming department of PROTEL. Your department have to update an old application with new functionality. • Your boss provide you a laptop and a URL where you can download the code developed to date. • Objective: Be able to edit, compile execute and debug an existing program. • Workplan: Download, install and configure the software in order to test (edit, compile, execute and debug) the application mcfp@it.uc3m.es 2010 2
Development Step I: Edit Step II: Compile Architecture Editors Compilers IDEs Grasp - Eclipse JDK -Netbeans Notepad Java -J Builder Bytecode code -Visual Café Others - Java Workshop EditPlus file.java file.class - Visual Age - J++ Otros javac file.java mcfp@it.uc3m.es 2010 3
Step III: Load Execution Step IV: Verify Architecture Step V: Execute Applet’s or Aplications Bytecode format Java Class Libraries (java extension Apis) Class Loader Bytecode Verifier Java Class Libraries (Java Base Apis) Java Virtual Machine Java Interpreter JIT (Just in Time Compiler) Java Runtime Operative System Hardware java file 4 mcfp@it.uc3m.es 2010 4
What can go wrong? • Compile “Syntax Error” • Load “Class not found Exception” • Verify “Security Exception” • Execute “Null Pointer Exception” mcfp@it.uc3m.es 2010 5
Where do I start? • Development environment: JDK – http://java.sun.com/products/jdk • Editor: Eclipse – http://www.eclipse.org • Documentation: Java API – http://java.sun.com/javase/6/docs/api/ • Configuration: – CLASSPATH: Set of directories containing the files.class you want to execute (not necessary since v1.2). It must contain, at least, $JAVA_HOME/lib/files.class o .tar – PATH: Directories to search for executable files It must contain, at least $JAVA_HOME/bin mcfp@it.uc3m.es 2010 6
How to configure Environment Variables Windows 95-98 (Type in MSDOS Window or modify c:\autoexec.bat): set PATH =c:\jdk1.2\bin;C:\WINDOWS\COMMAND\ set CLASSPATH =c:\jdk1.2\lib\classes.zip;. Preserving the old value of environment variables: set PATH =c:\jdk1.2\bin; %PATH% set CLASSPATH =c:\jdk1.2\lib\classes.zip; %CLASSPATH% ;. Linux (Type in a terminal window or modify in .bash file to conserve the value) : PATH =$JAVA_HOME/bin:/usr/bin CLASSPATH =$JAVA_HOME/lib/classes.zip:. Preserving the old value of environment variables : PATH =$JAVA_HOME/java/bin: $PATH CLASSPATH =$JAVA_HOME/lib/classes.zip: $CLASSPATH mcfp@it.uc3m.es 2010 7
How to configure Environment Variables Windows NT – Start – Control panel – System – Select: Environment -[look for user and system variables] Windows 2000 – Start – Control panel – System – Select: Advanced -[look for user and system variables] Windows XP – Start – Control panel – System – Select: Advanced – click on environment variables Windows ME – Start – Program files - Accesories – System tools – System info – Select: Tools-System configuration – Select: Environment- [select variable]- click edit Windows 7 – Start – Control panel – System and Security – system – System advanced configuration – Advanced options – Environment variables mcfp@it.uc3m.es 2010 8
System Programming Programming System Java Language Code Structure Telematics Engineering M. Carmen Fernández Panadero mcfp@it.uc3m.es 2010 mcfp@it.uc3m.es 2010 9
Scenary II: Understanding java code • Your first programmers’ meeting will be in an hour. By this time you must have reviewed the code and you must have understood how the application works. • Objective: Be fluent reading java structures related with classes, attributes and methods. Understand, at a glance, a complex java program with several files. • Workplan: – Review Java sintax ( identifiers, reserved words, etc.) in order to distinguish between words from java-language and nomenclature for a specific application – Identify language structures related with class declaration, attribute declaration (basic and reference types) and method declaration . – Draw UML diagrams to represent a set of java files in order to identify object types, their characteristics (attributes) and behaviour (methods) – Understand and explain the main method (when exists) to see in which order the objects are created, the method invoked and the sentences executed mcfp@it.uc3m.es 2010 10
Code Structure Program Classes (files.java) Members Attributes Methods Primitive Normal Special Reference Type Methods methods Type Constructor Main Object Arrays String mcfp@it.uc3m.es 2010 11
How to represent classes and Objects in java • Class declaration • Attribute declaration OO (constants or variables) • Method declaration • Object creation • Identifiers • Reserved words Java • Primitive and reference types in Java mcfp@it.uc3m.es 2010 12
Identifiers • Identifiers are used to give a name to variables, methods, classes, objects, and every thing that the programmer needs to identify. • Starting with a letter, an underscore or a $ sign • Case-sensitive and have no maximum length • By convention : – The names of variables, methods and objects begin with lowercase. – The class names begin with uppercase – If contain several words use camel-case likeInThisExample (avoid spaces, underscores and hyphen) Identifiers can not be reserved words mcfp@it.uc3m.es 2010 13
Reserved words Reserved : abstract double int static boolean else interface super break extends long switch byte final native synchronized case finally new this catch float null throw char for package throws class goto private transient* const * if protected try continue implements public void default import return volatile do instanceOf short while Reserved (not used): cast future generic inner operator outer rest var mcfp@it.uc3m.es 2010 14
Comments • 3 Types: // Implementation comment (1 line) /* Implementation block comment. continue finish */ /** Documentation comment to generate javadoc For classes @see ref to other class or method and methods @version information about version number @author author name For classes optional @since Date since code is available @param Params recived by the method @return Information and data type returned by the method For methods @throws Exceptions that throws this method @deprecated The method is old */ mcfp@it.uc3m.es 2010 15
Class declaration public class Car{ //Attribute declaration // (color, speed, etc) //Method declaration // (start, stop, etc.) } Car.java Sintax Style (modifiers) class className{ •File name= class name •1st letter capitalized //class implementation • No blanks } • Camel case MyFirstClass • Indentation ����������������������������������������������������� ������������ 16 mcfp@it.uc3m.es 2010 16
Variable Declaration public class Car{ //Atribute declaration String color; int speed; //Method declaration // (start, stop, etc.) } Sintax Car.java Style type name; type name1, name2, name3; •Intuitive names type name = value; •1st letter capitalized • No blanks • Camel case: myVariable Initialize the variable • Indentation 17 mcfp@it.uc3m.es 2010 17
Variables • Variables are fields in which programs store information • To declare a variable is to specify its name and type. • We can find variables: – As members : Instance and class variables (within a class) – As local variables (within a method) – As parameter (within a method declaration). mcfp@it.uc3m.es 2010 18
Variables • 3 Types : Default values: – Instance variables numbers = 0 – Class variables booleans = false – Local variables references = null • Variables – can be initialized in the declaration – may be declared uninitialized – when have been not initialized they have a default value (except local variables) • Constants (variables that can not been modified): – Use reserved word: final – It is mandatory to initialize in declaration mcfp@it.uc3m.es 2010 19
Scope • The scope of a variable is the part of the program over which the variable name can be referenced. • Instance or class variable can be referenced inside the body of the class or from other classes depending on the permissions set: – private – protected – public – friendly • Local (Can be referenced inside a statement block in brackets, such as inside a method or inside a while or for loops) • Parameters (Can be referenced inside the body of the method) mcfp@it.uc3m.es 2010 20
Data Types in Java • All variables belong to a data type • The data type determines: – The values that the variable can take – The operators that can be used • We will study: – Primitive types – Reference types (objects and arrays) mcfp@it.uc3m.es 2010 21
Recommend
More recommend