Download presentation
Presentation is loading. Please wait.
Published byNora Goodwin Modified over 7 years ago
1
Lecture 2: Introduction To Computers, Programming, and Java
John Hurley CS 201 Cal State LA
2
NOTICE! The definitions in this lecture will appear on a closed book quiz next week! Be ready to write the definitions from memory and to answer questions about them.
3
Software Software Program Algorithm
The instructions and data that are necessary for operating a computer Program A software application, or a collection of software applications, designed to perform a specific task Algorithm A set of instructions that will solve a problem in a finite amount of time A program normally implements one or more algorithms
4
Software Programming memorize this! the preparation of programs!
requires a programming language From Wikipedia: A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. memorize this!
5
Programming Computers execute machine code, also called machine language, which consists of instructions expressed as base 2 numbers different machine languages for different types of processors very few programmers can write machine language (but hardware engineers who design chips occasionally do) some programming is done in assembly language, which is a human- understandable set of mnemonic codes that corresponds closely to machine instructions Different for different types of CPUs Assembly language is fun to write, but is only occasionally used in practical programming today. CS majors study it to understand some fundamental concepts in the field, rather than to use it on the job.
6
Programming Languages
Assembly language programming involves manipulation of very small machine operations. Over time, easier-to-use programming languages have evolved to allow greater programmer productivity
7
Programming Languages
Memorize this slide! Low level language: few machine instructions per line of programming-language code. a fancier way to put this: little abstraction from the machine operations High level language: many machine instructions per line of code greater abstraction from machine operations Because high-level languages do not directly manipulate machine operations, they can be standardized across different operating systems, and even across different hardware (that uses different sets of machine operations.) Lower level languages often generate programs that run more efficiently (faster or with less memory use) than higher level languages Higher level languages usually offer better programmer productivity
8
Low-level Vs. High-Level
Typical assembly language instruction: mov cx,0x10 Sets the value held in CPU register cx to the base 16 value 10 Typical high-level language statement: area = 5 * 5 * ; Sets the value of a variable called “area” to 5* 5 * pi would take many lines of assembly language to do this
9
Expense Resources are not free; in programming lingo, they are said to be expensive Programmers’ time CPU usage Memory Storage Minimizing expense is a key part of programming use as little CPU capacity, memory, storage, etc. as possible Use programmers’ time as productively as possible
10
Expense Hardware has become much less expensive over the history of software engineering. In 1970, a computer and its peripherals cost millions of dollars and filled a small building. Today most code runs on computers that cost less than $2,000 each. Programmers in developed countries get paid about the same now in constant dollars as they did 45 years ago Thus, programmers have become more expensive relative to hardware. The expense of paying us is a much larger part of the total expense of developing software than it was, for example, in This favors programming languages and techniques that make programmers more productive, rather than those that execute most efficiently
11
Java Java 1995 developed at Sun Microsystems
easier to use than C++ in many ways gives up some of C’s low-level capabilities approximately tied with C++ as the most widely used general-purpose programming language today Designed to allow identical code to produce the same results on many different platforms (types of hardware and operating systems)
12
Java Main uses of Java today: Used in many web-based applications
Used for programming devices like video game consoles, Android phones, BluRay players, etc. Desktop applications that need to be easily ported (moved) to various operating systems. Originally expected to find widespread use in embedded systems (software that runs on computers included in appliances, cars, etc), but this did not pan out
13
Programming Language to Machine Instructions
source code = instructions written in a programming language Assembly language is translated to machine code using an assembler High level languages are translated using interpreters and/or compilers interpreter translates into machine language at runtime; no need to store extra files compiler translates into machine language ahead of time and stores the executable file: faster at runtime than in interpreter
14
Programming Language to Machine Instructions
Traditional BASIC and JavaScript (language used for writing programs that run in your web browser) use interpreters, as do many newer languages like PHP. C and FORTRAN use compilers Java uses a two-step process that attempts to make the best of both MEMORIZE THIS! Java is compiled before runtime to an intermediate language called Java bytecode The Java Virtual Machine interprets bytecode to machine language at runtime All forms of JVM run the same bytecode, but each platform (operating system or hardware type) requires a different type of JVM. Everything that is specific to the operating system or hardware is contained within the JVM, not the compiler. Therefore, Java bytecode is analogous to an assembly language for the JVM
16
Pros and Cons of JVM Main Advantage Main Drawbacks:
Java is highly platform-independent Write Once, Run Anywhere Usually easy to run the same Java code in UNIX/OSX/Windows/ Android/Toaster/Antilock Brake System/Whatever reduces the amount of knowledge the programmer needs to have about the specific platform adds long-term robustness. Your Java code will still run in Windows 18.7 as long as there is a JVM. Main Drawbacks: JVM itself uses some memory, CPU capacity, and other system resources Adds one extra step at runtime. Java code often runs more slowly than compiled C++
17
Operating Systems An operating system (OS) is software that manages hardware and software resources and provides common services for computer applications. (Wikipedia) Other than very small embedded systems, like the ones that run digital watches, almost all computers use operating systems. OSs save programmers from the necessity to work with low-level aspects of computing like Choosing specific blocs of memory to use Deciding which program can use the CPU at a particular time Working directly with storage devices The most common operating systems in general usage are Windows UNIX Linux Mac OSX
18
Memory And Data Memory volatile = data is normally lost when power lost (reboot or power interruption) every location in memory has a numeric address all information is measured in bits A bit has two possible values which you can conceptualize as true or false, on or off, or 0 or 1 a bit is a unit of information, not a physical device. corresponds to the state of particular electronic components, but programmers rarely need to think about this. values of single or multiple bits are conveniently represented as base 2 (binary) numbers
19
Bytes memorize this slide! 1 byte = 8 bits
28 = 256, so the value of 1 byte ranges from to binary = 0 to 255 decimal 1 byte is usually the minimum amount of information that you can manipulate Terms like kilobyte and megabyte refer to quantities of bytes in units that are powers of 2 close to the decimal numbers implied in the names, 1K = bytes; 1 MB = bytes Most of the time, the actual storage units are invisible to the programmer. For example, you will usually deal with an integer without worrying about the fact that the computer sees it as 4 bytes
20
Data Types Every data item (variable, constant, or even literal, such as the integer 3 or the string “Godzilla”) has a specific data type. Each type of data is stored with a specific amount of memory and handled differently by the compiler Thus, you must think carefully about the type of every piece of data you use. a character takes less space to store than an integer you can’t find the product of two variables unless they are both numbers If you divide one integer by another and store the quotient as an integer, you may lose information. It may not be good enough to say that 5 / 4 = 1
21
Variable and Constant Declarations
Programs often use variables and constants You already know these concepts from algebra You must tell the computer to set aside memory space for any variable or constant you use int radius = 0; finds space to store an integer, names it radius, and sets the value of radius to 0
22
Data Types Storage affects the possible values for a piece of data.
Java uses 4 bytes = 32 bits to store the value of an integer The (complex) method for tracking the sign uses one bit. 231 = Therefore, a Java integer has possible values from to = garbage Memorize this: “the minimum value for a Java integer is a little less than negative 2 billion. The maximum value is a little more than positive 2 billion.”
23
Data Types Primitive Data Types
primitive = built in to the programming language as a basic building block Many other data types are built into Java, but are made up of primitive types plus methods Later you will learn how to build your own data types using primitive and other built-in types as building blocks
24
Numeric Data Types
25
Floating Point Types “Floating point types” inlude the types float and double Internal representation of floating points types includes an exponent, much as in scientific notation These types store values with limited precision, essentially rounding to the closest value they can hold double doesn’t just hold a larger range of values than float, it also provides greater precision If you want to learn more about this, see
26
Numeric Data Types Tradeoffs in choosing numeric data types
Minimize resource usage; use smallest type that will hold the values you expect to use Minimize risk of error; pick safest choice Most of the time, programmers use int for integers and double for floating point numbers Use long if the value may be larger than an int can store Rarely use byte or short
27
Numeric Data Types Declare a double:
double gpa; declare a double and set its value to 1 double gpa = 3.75; double gpa = 4d; // d for double; otherwise the compiler // would treat 4 as an int
28
Methods Procedures for performing particular tasks
Java methods are equivalent to functions, subroutines, or procedures in other languages All you need to understand right now about methods is how to see what is inside one and what isn’t. Most of our code will be inside methods. A method: public int get Product(int firstInt, int secondInt) { return x * y; }
29
Classes A class is a template for objects.
You will learn to understand classes in more depth later in this course, but all of our work in this class will be done inside classes. In Java and many other object-oriented languages, a program is defined by one or more classes.
30
main() public static void main(String[] args) {
The main() method is the starting point for the flow of control in the program. The JVM executes the application by invoking (running) the main method of some class. For the time being, all our classes will contain main() methods main() often calls other methods, including ones that are in other classes main() looks like this: public static void main(String[] args) { // Statements; }
31
Skeleton Java class Start with this code: public class Skeleton{
public static void main(String[] args){ // early in the course, all your code will go inside main() } // end main method } // end class
32
System.out.println The simplest way to show output
By default, prints to the command console System.out.println("Hello, World!"); prints the message “Hello, World!” System.out.println(i) prints the value of a variable named i System.out.println("i = " + i); prints the literal text "i =" followed by the value of i The + means "concatenate the next item to this output," ie "add the value of the variable i to the end of the text in the quotes" if the value of i is 1 at the time the statement executes, prints “i = 1”
33
Comments Ignored by the compiler and JVM Used for documentation intended for programmers or other people involved in development Single line comment: A line comment is preceded by two slashes (//) in a line. The rest of the line becomes a comment. Multiline comment: A paragraph comment is enclosed between /* and */ in one or multiple lines. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command.
34
Running A Java Program Later in the course, we will use Eclipse, an Integrated Development Environment (a software package that provides tools for programming) For now, we will run programs from a command line Use the lab computers in class, at least for lab 1, but you will need a current Java Development Kit for your homework. If you use Windows, you will need to install Oracle's JDK (see link from course web page.) Allow plenty of time for installation in case things go wrong! MAC OSX has a JDK preinstalled If you use Linux, you will probably find that OpenJDK is already installed. This will work fine until we begin studying GUIs towards the end of the course. At that point you will also need to install Oracle's JDK. You may need to search online for instructions that apply for your particular distro. Allow plenty of time for this! You may use your own laptop in class, but you will need to use the lab computers on the exams, so get used to them at some time *well in advance of the midterm.*
35
JDK Versions Java Standard Edition (J2SE)
J2SE is used to develop client-side standalone applications or applets. We will use this edition. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. It is OK to get this one, but the download is bigger and we won’t use the additional functionality. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile and small embedded systems
36
Running A Java Program Java source code is stored in plain text files with .java file name extension. You can save them on a flash drive or your server space (ask IT, not me, where that is!) or them to yourself. For the time being, write your code with a plain text editor Recommended for Windows: Notepad++ or Geany. Notepad, TextEdit, etc. will work too, but Notepad++ is better. You can use a word processor, but if you do you will soon realize why I don’t recommend it. Recommended for OSX: TextWrangler. TextEdit will trip you up. If you use Linux you probably already have an opinion about text editors. Save as plain text using extension .java. If your editor contains a “save as java source” option, that will work fine, too.
37
Running A Java Program Instructions for running a Java program from the command line are in Section 1.8 of the book Key steps: Add JDK bin directory to path, e.g. set path=c:\java\jdk1.8.0\bin Be *careful* editing your path! Open a command prompt and navigate to the folder where you saved the file Type javac filename.java Compiles Java source to Bytecode for example, if the program file is called Hello.java, type javac Hello.java Notice that there is now a file called filename.class, eg Hello.class. This file contains the bytecode Type java classname Runs the JVM with the bytecode as input for example, java Hello If you get a NoClassDefFound error, type set classpath= then try again
38
NoClassDefFoundErrors
NoClassDefFoundErrors are usually caused by incorrect settings for the classpath variable. More instructions and an explanation of classpath are at For the time being, just navigate to the directory where the java file is before you compile and run
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.