1 / 65

Programming Languages and Java

Programming Languages and Java. Programming languages Java language The basic program Java syntax. Programming. The process of writing programs called a programming . The process requires: Programming language A translator of the programming language into machine language

johnawalker
Télécharger la présentation

Programming Languages and Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Languages and Java • Programming languages • Java language • The basic program • Java syntax

  2. Programming • The process of writing programs called a programming. • The process requires: • Programming language • A translator of the programming language into machine language • Programs are written in programming language.

  3. Programming language • A textual language used to write a program. • The language has to be well defined syntactically and semantically. • The meaning of what you write has to be precisely defined and understood by the computer.

  4. Syntax • Syntax describes the grammatical rules of a language. • Valid words • Valid grammar constructions • Valid punctuation • Programs must be syntactically correct.

  5. Semantics • Semantics gives the meaning of what you write with a language. • A programming language must precisely define the meaning of every statement that can be written with it. • Programs must be semantically correct.

  6. Example • The following sentence is syntactically correct but semantically incorrect. “ Java programs are green and yellow.” • All words are correct words. • Punctuation is correct. • But logically the sentence makes no sense.

  7. Programming languages • There are several families of the programming languages: • Procedural ( Pascal, C++, Java) • Logical ( Prolog) • Visual ( Visual Basic) • Document (HTML, LATEX)

  8. Programming languages • Procedural languages can be divided into: • Machine languages • Low-level (Assembler ) • Intermediate-level (C) • High-Level (C++, Java)

  9. Programming languages • Writing programs in machine language is possible but very difficult and time consuming. • Programs usually written in a more human readable language - high-level language. • A program written in any computer language must be translated into a machine language in order to be executed.

  10. Translators • Compiler • A compiler is a program that translates a source program (usually high level language) into target program (usually machine language program). • The resulting program can be executed many times. • Interpreter • An interpreter is a program that reads, translates and executes the source program statement by statement. • The translation is done each time the program runs.

  11. Java • Java is a high-level, third generation programming language, like C, Fortran, Smalltalk. • Shares much of C's syntax. • Designed by a group at Sun MicroSystems. • Originally called Oak.

  12. Java advantages • Platform independent “Write once, run anywhere”. • Improve robustness, remove unsafe language loopholes. • According to Sun: “ Java is simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithread, and dynamic language.”

  13. Java disadvantages • The main Java disadvantage: Efficiency concerns. Looking to the future: VM performance will improve.

  14. Java environments • Java has many environments. • The number grows as Java evolves. • Among them are: • Text applications • Beans • Applets • GUI Applications • Packages

  15. Two main environments The two main environments: • Web Browser • Operating System

  16. Web Browser • In the browser environment the browser acts as an intermediate between the program and the operating system. • The JVM resides inside the browser. • The program can be simpler. • The program has to work in graphical mode. • The program is called “Applet” (a small application).

  17. Operating System • In the operating system environment the program is called “application”. • Application has a more rigid structure. • Application can be textual or graphical. • Application is less secure.

  18. Learning Java The specific challenge is twofold: • Learning the language technical aspect: the large set of • new features • class libraries • classes • methods which are provided as part of the language. • Learning to maximize the benefits of the object-oriented paradigm.

  19. OOA and OOD vs. OOP OOA is a way of analyzing - OOD is a way of thinking – OOP is a way of doing. OOA is about “why” – OOD is about “what” – OOP is about “how”.

  20. Starting to Program • The basic idea of the course is to start programming as soon as possible. • At first you will have to take a lot of things on trust. • Promise: we will return to the details later.

  21. Java programs All Java programs have three parts: • Auxiliary prescriptions • A class – an envelope around the other components • Code per se – • Optionally – other components which include the instructions themselves • Instructions to be executed

  22. Writing a Java Program • Use an editor to type the program code. • Save the code file. • Compile the file with Java language compiler. • Fix the bugs! • Run the program.

  23. A basic Java Program // Prints “How are you?” to the screen class My { public static void main (String[] args) { System.out.println(“How are you?”); } }

  24. A basic Java Program // Prints “How are you?” to the screen class My { public static void main (String[] args) { System.out.println(“How are you?”); } }

  25. Java program structure • A Java is made up of one or more classes. • Classes have names. • The ‘My’ program consists of one class named ‘My’.

  26. Java program structure • A Java class contains one or more methods. • Methods have names too. • The program (the class) has to have a main method. • The method is called (surprisingly) – main. • The main method has a standard syntax: public static void main(String args[]) { … // your code }

  27. Java program structure • There can be only one main per file. • The main method contains the code to be executed when the program runs.

  28. Java program structure • A method contains one or more statements. • The main method of My program has a single statement: System.out.println(“How are you?”);

  29. Stages of Writing and Executing Assumptions: • Using only the basic JDK software • Assuming the JDK was installed properly • In the WIN/DOS environment • Class name is My • File name is My.java • Directory (folder) name is c:\java

  30. Stages of Writing and Executing • Open a text editor that can produce a plain text file (such as Notepad). • Create a source code file with the extension on the file name being .java . The file has to have the same name as the outer class. • Start a the DOS command environment by opening MSDOS prompt (using the Start->Programs->MSDOS Prompt series of choices).

  31. Stages of Writing and Executing • Change directory to the directory containing the source file. • Compile the file, using the prompt command: javac My.java • The result is My.class in the same folder. • Run using the prompt command: java My

  32. Java Compiler • The Java compiler is called javac. • Java programs are compiled to byte code.

  33. Byte code (revisited) • The Java compiler translates Java program into a special representation called byte code. • Java byte code is a machine code for Java virtual Machine(JVM). • VM is a platform-specific tool to interpret the byte code and to translate it to commands for certain processor and OS. • The use of the byte code makes Java platform independent.

  34. Compile-interpret-execute cycle

  35. Compile-interpret-execute cycle

  36. Errors • A program can have three types of errors: • Syntax and semantic errors – called compile-time errors • Run-time errors – occur during program execution • Logical errors

  37. Errors • Compile-time errors occur during program compilation and an executable version of the program is not created. • Run-time errors occur during program execution and cause abnormal program termination. • Logical errors occur during program execution and produce incorrect results.

  38. Java Syntax • To write without syntax mistakes you have to know Java syntax. Syntax - the study of the patterns of formation of sentences and phrases from words and of the rules for the formation of grammatical sentences in a language.

  39. Java Syntax • Case-sensitive • Semi-colon (;) is line terminator • Curly braces ({,}) used for block structure • Several keywords

  40. Comments There are two kinds of comments: •  /* text */ A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored. •  // text  An end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored.

  41. Comments Comments do not nest. • /* and */ have no special meaning in comments that begin with //. • // has no special meaning in comments that begin with /* or /**.  As a result, the text: /* this comment /* // /** ends here: */ is a single complete comment.

  42. Identifiers An identifier is: • an unlimited-length sequence of Java letters and Java digits, • the first of which must be a Java letter.  An identifier cannot have the same spelling (Unicode character sequence) as: • a keyword, • boolean literal, • the null literal

  43. Identifiers • Letters and digits may be drawn from the entire Unicode character set (The character set that uses 16 bit per character). •  Identifier can be written in most writing scripts in use in the world today, including: • Hebrew, • Chinese, • Japanese, • Korean • Practically all languages

  44. Identifiers • Uppercase and lowercase are different. All the following are different identifiers: MY my My mY my1 Examples of identifiers are: String i3 isLetterOrDigit מונה MAX_VALUE

  45. Keywords The following are reserved words called keywords and cannot be used as identifiers: goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while abstract boolean break byte byvalue case cast catch char class const continue default do double else extends false final finally float for future generic

  46. Keywords While true and false might appear to be keywords, they are technically Boolean literals. Similarly, while null might appear to be a keyword, it is technically the null literal.

  47. Literals A literal is a source code representation of a value of: • a primitive type, • the String type, • the null type  Kinds of Literals:   IntegerLiteral FloatingPointLiteral BooleanLiteral CharacterLiteral StringLiteral NullLiteral  

  48. Integer Literals An integer literal may be expressed in: • decimal (base 10), • hexadecimal (base 16), • octal (base 8)

  49. Hexadecimal numeral • A hexadecimal numeral consists of the leading characters 0x or 0X followed by one or more hexadecimal digits. • It can represent a positive, zero, or negative integer. • Hexadecimal digits with values 10 through 15 are represented by the letters a through f or A through F, respectively.    HexDigit is one of: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

  50. Octal numeral • An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7. • It can represent a positive, zero, or negative integer. •  Octal numerals always consist of two or more digits.

More Related