what we will do today
play

What We Will Do Today Topic 2 Introduction to Java Programming - PowerPoint PPT Presentation

What We Will Do Today Topic 2 Introduction to Java Programming What are computer languages? Java editors When a programming language is created that allows text editor and command line programmers to program in BlueJ


  1. What We Will Do Today Topic 2 Introduction to Java Programming � What are computer languages? � Java editors “When a programming language is created that allows – text editor and command line programmers to program in – BlueJ simple English, it will be � First programming concepts discovered that programmers – output with println statements cannot speak English.” – syntax and errors - Anonymous � structured algorithms with static methods � identifiers, keywords, and comments Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/ CS305j Introduction to Introduction to Java Programming 1 CS305j Introduction to Introduction to Java Programming 2 Computing Computing Computers and Computer Definitions Languages � program : A set of instructions that are to be carried � Computers are everywhere out by a computer. – how many computers do you own? � Computers are useful because they run � program execution : The act of carrying out the various programs instructions contained in a program. – this is done by feeding the instructions to the CPU – program is simply a set of instructions to complete some task � programming language : A systematic set of rules – how many different programs do you use in a used to describe computations, generally in a day? format that is editable by humans. – in this class will are using Java CS305j Introduction to Introduction to Java Programming 3 CS305j Introduction to Introduction to Java Programming 4 Computing Computing

  2. Machine Code High Level Languages � John von Neumann - co-author of paper in 1946 with Arthur W. Burks and Hermann H. Goldstine, � Computers are fast – "Preliminary Discussion of the Logical Design of an – Pentium 4 chip from 2001 can perform approximately Electronic Computing Instrument" 1,700,000,000 computations per � One of the key points second – program commands and data stored as sequences of – made up of 42,000,000 transistors bits in the computer's memory (a switch that is on or off) � Computers are dumb � A program: – They can only carry out a very limited set 1110001100000000 of instructions 0101011011100000 • on the order of 100 or so depending on 0110100001000000 the computer's processor • machine language instructions, aka 0000100000001000 instruction set architecture (ISA) 0001011011000100 • Add, Branch, Jump, Get Data, Get 0001001001100001 Instruction, Store 0110100001000000 CS305j Introduction to Introduction to Java Programming 5 CS305j Introduction to Introduction to Java Programming 6 Computing Computing Say What? High Level Languages � Programming with Strings of bits (1s or 0s) is not � Assembly language, still not so easy, and lots the easiest thing in the world. of commands to accomplish things � Assembly language � High Level Computer Languages provide the – mnemonics for machine language instructions ability to accomplish a lot with fewer commands .ORIG x3001 than machine or assembly language in a way LD R1, x3100 that is hopefully easier to understand AND R3, R3 #0 int sum; LD R4, R1 int count = 0; BRn x3008 int done = -1; ADD R3, R3, R4 while( list[count]!= -1 ) ADD R1, R1, #1 sum += list[count]; LD R4, R1 BRnzp x3003 CS305j Introduction to Introduction to Java Programming 7 CS305j Introduction to Introduction to Java Programming 8 Computing Computing

  3. Java A Picture is Worth… � There are hundreds of high level computer languages. Java, C++, C, Basic, Fortran, The output of the compiler is .class Cobol, Lisp, Perl, Prolog, Eiffel, Python file � The capabilities of the languages vary widely, but they all need a way to do – declarative statements – conditional statements – iterative or repetitive statements � A compiler is a program that converts commands in high level languages to The Interpreter's are sometimes referred to as the Java Virtual machine language instructions Machines CS305j Introduction to Introduction to Java Programming 9 CS305j Introduction to Introduction to Java Programming 10 Computing Computing A Simple Java Program More Definitions � code or source code : The sequence of public class Hello instructions in a particular program. { public static void main(String[] args) – The code in this program instructs the computer to print a { System.out.println("Hello World!"); message of Hello, world! on the screen. } } � output : The messages printed to the computer user by a program. This would be in a text file named Hello.java DEMO of writing and running a program via notepad and � console : The text box or window onto which output the command line is printed. CS305j Introduction to Introduction to Java Programming 11 CS305j Introduction to Introduction to Java Programming 12 Computing Computing

  4. Compiling and Running The command line To run a Java program using � Compiler : a program that converts a program in your Command Prompt: one language to another language source code – compile from C++ to machine code � change to the directory (Hello.java) – compile Java to bytecode of your program � Bytecode : a language for an imaginary cpu compile cd � Interpreter : A converts one instruction or line of byte code code from one language to another and then � compile the program executes that instruction (Hello.class) javac Hello.java – When java programs are run the bytecode produced by the compiler is fed to an interpreter that converts it to execute � execute the program machine code for a particular CPU – on my machine it converts it to instructions for a Pentium output java Hello cpu CS305j Introduction to Introduction to Java Programming 13 CS305j Introduction to Introduction to Java Programming 14 Computing Computing Structure of Java programs Another Java program public class <name> { public class Hello2 { public static void main(String[] args) { public static void main(String[] args) { System.out.println("Hello, world!"); <statement(s)> ; System.out.println(); } System.out.println("This program produces"); } System.out.println("four lines of output"); � Every executable Java program consists of a } } class ... � The code in this program instructs the – that contains a method named main ... • that contains the statements to be executed computer to print four messages on the � The previous program is a class named screen. Hello , whose main method executes one statement named System.out.println CS305j Introduction to Introduction to Java Programming 15 CS305j Introduction to Introduction to Java Programming 16 Computing Computing

  5. Syntax and syntax errors Java terminology � syntax : The set of legal structures and commands � class : that can be used in a particular programming (a) A module that can contain executable code. language. (b) A description of a type of objects. (seen later) � syntax error or compiler error : A problem in the � statement : An executable piece of code that structure of a program that causes the compiler to represents a complete command to the fail. – If you type your Java program incorrectly, you may computer. violate Java's syntax and see a syntax error . – every basic Java statement ends with a semicolon ; public class Hello { � method : A named sequence of statements pooblic static void main(String[] args) { System.owt.println("Hello, world!")_ that can be executed together to perform a } particular action or computation. } CS305j Introduction to Introduction to Java Programming 17 CS305j Introduction to Introduction to Java Programming 18 Computing Computing Fixing syntax errors Compiler Output � Notice how the error messages are sort of cryptic and do not always help us understand what is wrong: � The program on the previous slide produces H:\summer\Hello.java:2: <identifier> expected pooblic static void main(String[] args) { the following output when we attempt to ^ compile it – We'd have preferred a friendly message such as, "You misspelled 'public' " compiler output: � The compiler does tell us the line number on which it found H:\summer\Hello.java:2: <identifier> expected the error, which helps us find the place to fix the code. pooblic static void main(String[] args) { ^ – The line number shown is a good hint, but is not always the true H:\summer\Hello.java:5: ';' expected source of the problem. } � Java has a fairly rigid syntax. ^ 2 errors Tool completed with exit code 1 CS305j Introduction to Introduction to Java Programming 19 CS305j Introduction to Introduction to Java Programming 20 Computing Computing

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend