Chapter 1 Overview: The Mental Landscape TCC Slides for Java I by David H. Straayer is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://math.hws.edu/javanotes/. 1.1 The Fetch and Execute Cycle: Machine Language • CPU - Central Processing Unit • Program • Machine Language • Main memory • Locations, addresses • Fetch-and-execute cycle • Program Counter • Bits and bytes 1.2 Asynchronous Events: Polling Loops and Interrupts • I/O devices • Device drivers • Busses I/O (Input/Output) devices • A hard disk or solid state drive for storing programs and data files • A keyboard and mouse for user input. • A monitor and printer which can be used to display the computer’s output. • An audio output device that allows the computer to play sounds. • A network interface that allows the computer to communicate with other computers that are connected to it on a network, either wirelessly or by wire. • A scanner that converts images into coded binary numbers that can be stored and manipulated on the computer. •… How does the CPU handle data? • Polling • Keep checking for data (in a loop) • Simple • Inefficient • Interrupts • When an event happens, it sends a signal to the CPU. • A special type of program is invoked: an interrupt handler • First: make note of what we were doing • Now, do what is needed to deal with this new data • Finally, return to what we were doing Interrupts and fetch-and-execute cycle • Usually, the CPU processes instructions in a predetermined order – synchronized. • Interrupts alter this flow – this is referred to as asynchronous processing. • The idea is to avoid having to waste a lot of time waiting for slow I/O devices Analogy with human-human communication • When you are talking with someone, in person or on the phone, you can’t do anything else, you have to wait for their reply every time you say something – your communication is synchronized. • If you are texting or using email (or even snail mail), your communication is asynchronous. While you’re waiting for a reply to one message, you can be doing something else, or even sending messages to other people. • Call waiting on your phone is like an interrupt Terms and concepts • Asynchronous events • Trigger interrupts, and alter the flow of processing • Multitasking • Makes it seem as though the computer is doing several things at once, even though at any instant in time, it is doing only one thing. • Timesharing • Kind of an old term, referring to a computer dealing with many users at one time • Thread or process • Each task that the CPU is working on is called a thread or process. Threads continue to run until: • The thread might voluntarily yield control. • The thread might have to wait, perhaps for data from a drive, or for a user to type something. While it is waiting, it is blocked, and other threads may run. When the event occurs, an interrupt will “wake up” the thread so that it can continue running. • Preemptive multitasking: even though the thread is doing something, it may use up an allotted slice of time. Two tribes of programs and programmers • Applications • Most applications programmers ignore threads and multitasking most of the time: they write the programs people use to get work done. • Operating Systems • Systems programmers work on the operating system, the “glue” software that ties it all together. 1.3 The Java Virtual Machine • High level programming languages • • • • • • • Java Fortran Pascal C Basic Cobol Ada • Machine Languages • Assembler Compilers, interpreters, and Virtual Machines • Compiler: This is a program that reads source code (parses it), and produces object code, a special version of machine language, that can be linked and relocated to run on a particular type of machine. • Interpreter: This is a program that reads source code, line-by-line, and runs it. The fetch-and-execute cycle is at a “higher level”. • Virtual Machine: This is what Java uses, and it is a hybrid of compiling and interpreting. The Java compiler reads the source code and produces a special file of “predigested” but computer-independent bytecodes. Then a virtual machine (JVM- Java Virtual Machine) reads and executes the bytecode very quickly. Why the JVM approach? • Speed: parsing takes time, and this allows it to be done once, the way a compiler does. • Machine-independence: The resulting programs can be run on many different types of computers and operating systems. • Source-code security: Source code is considered a valuable resource. This allows companies to keep it secret, but still distribute their programs to their customers. • Network security: being able to run a program greatly extends what a web page can do, for good or evil. A JVM with limited capabilities can provide some security. J-I-T and all that • Just-In-Time compilers can be even faster than JVM interpreting bytecode. Is this less of an issue today? • Java and Java bytecode are not necessarily linked. Java could be compiled into “native” machine code, but this loses platformindependence. • Some other languages generate java bytecode. 1.4 Fundamental Building Blocks of Programs • Variables: names for locations in memory. • Types: each variable has a type. The type describes how the bits in memory are to be interpreted, and what we can do with them. • Primitive types are the built-in uses of memory • Integers, doubles (real number), characters, strings of characters, etc. • Objects: programmer-defined types that can have more flexibility in the information that can be encoded, and what we can do with them. • Control structures: altering the sequential flow of instructions • Loops • Branches • Subroutines: Modular programming It’s all 1’s and 0’s • That’s all binary computers deal with: 1’s and 0’s. • These are grouped into bytes, words, etc. • But when we deal with the data in a computer, we give names to chucks of memory: we call these chunks variables. • Each variable has a type. The type describes: • How much data is associated with this chunk (bits, bytes, etc.) • How the 1’s and 0’s are to be interpreted (numbers, text, pictures, sounds…) • What we can do with these chunks of memory. Things we can do are called operations, and include things like add, copy, render, etc. Types of types: primitives and objects • Primitives are the built-in types anticipated by the designers of the computer and language. • They are fixed-size chunks of memory. • They include counting numbers, measuring numbers, and characters (text) • Their operations involve operators like +, -, *, /, % • Objects are programmer-defined types. They are whatever you want them to be. • The data stored in the objects can include primitives and other objects • Their operations are defined by the programmer and called subroutines or methods. 1.5 Objects and Object-oriented Programming • History of software engineering • • • • • • Modular programming Structured programming Top-down programming Bottom-up design Information hiding Object-oriented programming Object-Oriented Programming (OOP) • State (chunks of memory: the data in an object) • Messages (subroutines, methods: the things you can do to objects) • Polymorphism: that different objects can respond to the same message in different ways • Class: the Java language item that lets you define the template for an object • Inheritance: Being able to define a new type of object based on an already-defined type of object. 1.6 The Modern User Interface • Batch processing • Command-line interface • Graphical User Interface (GUI) • • • • Graphical display Graphical input device (mouse, touchscreen) Windows Standard GUI interface components (button, checkbox, …) GUI in Java • AWT and Swing and more… • GUI programming is event-driven. Instead of the programmer being in ultimate control, the user is. The programmer writes pieces of code (methods) that respond to events generated by the user. • This is sometimes referred to as messages. 1.7 The Internet and Beyond • Networks connect computers together. • The Internet is a huge network of networks. • Protocols are detailed specifications about how data is sent and received • Internet Protocol (IP) • Transmission Control Protocol (TCP) • Packets • IP Addresses • Domain names Other internet items • Mail: SMPT, POP, IMAP • Web: pages, links, servers, browsers, HTTP, HTML, applets Beyond • Embedded programming: things like thermostats, set-top boxes, video players, game systems, … • Android (Uses Java, but special UI components) • Beyond Java: C, C++, Python, … • It is a lot easier to learn another programming language after you’ve mastered one. Java is a fine starting point.