Anda di halaman 1dari 3

Java 211 - Lecture I

Introduction to Java
Yosef Mendelsohn

What is a programming language? A collection of words, symbols, and grammer rules that allow us to control the computers behavior. With proper training, we as programmers can harness the incredible computative power of todays processors to accomplish incredibally complex tasks. Steps in programming: 1. Define the problem 2. Break problem down into bite-sized, manageable pieces 3. Speculate on possible solutions and alternatives. Refine. 4. Implement the solution (code) 5. Test and fix Notice how the coding step (step 4) is only one of a series of steps that must be factored into the software design. Why Java? - Not as syntactically complex (can focus on programming issues and details less relatively) - Can be executed on the web (less significant than you might imagine) - Lots of supporting libraries - Fully object oriented - Widely used - Security - Networking capabilities A Java Program: FirstProgram.java A second Java program: TemperatureConvert.java Notice how in the temperature conversion program we were unable to read input from the user. In some languages, reading input from the user is very simple. It takes a few additional steps in Java, so for now, you can simply hard code values for your variables. In other words, instead of asking the user to enter a value for the temperature, you can simply assign it by saying, for example, double farenheit = 102.4;

Compilation
Machine language - unique to each CPU e.g. Pentium v.s. PowerPC v.s. Sparc

expressed in binary adding two numbers together and store the result might require four machine language instructions (just 1 in a high-level language) see fig 1.20 p.35 (Sparc processor)

Assembly language - replace binary digits with mnemonics - easier to code in, but must be translated into machine language before the program can run High Level Language - much more English-like - must also be translated into machine language Compilers: - a tool that converts high-level code (source code) into machine language code (object code) - most commercial compilers are also packaged with text editors that have extra functionality such as color coding, automatic indenting, etc - Compilers check your code for errors. They will catch syntax errors, but not logic errors. - Compiler messages can be helpful, but also can be notoriously misleading - Sometimes one missing semicolon can lead to 25 errors The Java Interpreter - Java is a bit different: the source code gets compiled into byte code. A Java interpreter exists which knows how to convert the byte code into the machine language for each specific computer. - In other words, regardless of platform (Sparc, Sun, PC, etc), the byte code will be the same. However the Java interpreter for that platform will convert the byte code into the appropriate machine code. - We say that Java is architecture neutral - Therefore, as long as a Java interpreter is available for a specific platform, Java programs can be executed. - If you wish to create Java programs, you need (at least) the Java Software Development Kit (SDK or JDK). This kit combines an editor, compiler, and various other tools to allow you to create Java programs. (See Loftus web site for more info) - Question: What do you need if you want only to run but not create Java programs? Programming Language Syntax - Each language has a unique syntax. Java syntax is similar but not identical to C++ syntax. - A few sample Java syntax rules

Identifiers (e.g. variable or method names) must begin with a letter (not a digit or symbol) - Classes must be delimited using braces { and } - Statements must be ended with a semicolon Programming languages evolve. New features are added, and others go away (are deprecated). - E.g. If a given functionality that worked in Java 1.0 but does not work in Java 2.0, we say that it has been deprecated.

Comments & Documentation Documentation is vital to quality programming! You are writing your code for other people, not just for you. Also, youd be surprised how quickly you forget why you chose to implement a piece of functionality in just such a way. - Important (not just nice) - // - /* to */ - Javadoc (We will not discuss Javadoc in this course, but you may hear about it from time to time).

Anda mungkin juga menyukai