<<

Today’s topics programs

Parsing  Java programs are created as text files using a text editor (like Java Programming ) Notes from Tammy Bailey  Save to disk with .java file extension HelloWorld.java Reading  The file contains characters (stored as ) Great Ideas, Chapter 3 & 4  file can be printed, displayed on monitor, or edited  file cannot be directly executed (run) by the computer system  Java must first translate the program into before it can be run

CompSci 001 6.1 CompSci 001 6.2

Bytecodes Java (JVM)

 Java  Bytecode (class) file will contain exactly the same bytecodes no matter what computer system is used  machine instruction for the Java processor  Bytecode file is executed by a  Java translates the source program into bytecodes  processor specific program  Each type of computer system has its own Java interpreter that  Bytecode file has same name as the source program with a can run on that system .class file extension: HelloWorld.class  Any computer system can execute Java bytecode programs if it has a Java interpreter  Computers with Java interpreters are called Java Virtual Machines  a “computer” with a Java processor that can run Java bytecodes HelloWorld.java javac HelloWorld.class

source program Java Java bytecodes CompSci 001 compiler 6.3 CompSci 001 6.4 Java applets Data types

 An applet is a Java bytecode program that runs on a Web  Computer memory stores arbitrary bit patterns browser  Meaning of a bit pattern depends on its use  Most newer Web browsers have Java interpreters  Pattern used for a particular string of bits is a data type  Web pages on the Internet contain instructions that send Java  values are any kind of data a computer can process bytecodes to your computer  all values are represented using some data type  Web browser runs the with its built-in interpreter  Example: What does the following pattern of 16 bits represent? 0000000001100111  No way to know without more information  If data type is short (a Java type) it represents 103

CompSci 001 6.5 CompSci 001 6.6

Java data types Primitive data types

 Primitive  All primitive values belong to one of eight primitive types  types of data that are so fundamental ways to represent short int long float them are built into Java double char boolean  Object  Primitive data types use a fixed number of bytes  built-in or user-defined  four of these types designate different sizes of bounded integers: byte, short, int, long  A programmer can not create new primitive data types  Any data type you invent will be a type of object  Most commonly used types in practice: int, boolean, and double

CompSci 001 6.7 CompSci 001 6.8 Java primitive data types Basic operators

Primitive Type Description Range Operator Java Description byte 8-bit integer -128 to 127 Assignment = assigns rhs to lhs short 16-bit integer -32768 to 32767 addition, subtraction, Arithmetic +,-,*,/,% multiplication, division, -2147483648 to int 32-bit integer remainder 2147483647 negative, auto increment, auto Unary -,++,-- long 64-bit integer -263 to 263-1 decrement float 32-bit floating point 10-46 to 1038 Equality ==, != equals to, not equals to double 64-bit floating point 10-324 to 10308 less than, less than or equals to, Relational <,<=,>,>= greater than, greater than or equals char Unicode character to boolean Boolean variable false and true Logical &&,||,! AND, OR, NOT

CompSci 001 6.9 CompSci 001 6.10

Variable declaration Examples

 int x, y, z;  Declaration type ;  int sum = 0;  Declaration + initialization:  float f; type = ;  double pi = 3.14;  Variable names  char first = ‘T’,  any combination of letters, numbers, and the underscore middle = ‘L’, character last = ‘B’;  may not start with number  char first = ‘T’;  may not be reserved word char middle = ‘L’; • e.g. int, return, if, for, while char last = ‘B’;  may not be same as a subroutine name  case-sensitive (num and Num are different)

CompSci 001 6.11 CompSci 001 6.12 Operator precedence When in doubt, use parentheses

 Evaluate a + b *  a + b * c = a + (b * c)  multiplication first? a + (b * c)  because * has higher priority than +  addition first? (a + b) * c  To perform the + operation first we need to use parentheses   Java solves this problem by assigning priorities to operators (a + b) * c (operator precedence)  If in any doubt use extra parentheses to ensure the correct Operator priority order of evaluation  operators with high priority  parentheses are free! are evaluated before (highest to lowest)  cause no extra work for the computer operators with low priority  only make it easier for you to work out what is happening  operators with equal priority 1. ( ) are evaluated left to right 2. * / % 3. + - 4. =

CompSci 001 6.13 CompSci 001 6.14

Examples Syntax and semantics

 Java adheres to traditional order of operations  Addition, subtraction: + and –, int and double  * and / have higher priority than + and – int x = 3 + 5 * 6; (x = 33) int x = 21+4; (x = 25) int y = (3 + 5) * 6; (y = 48) double y = 14.1-2; (y = 12.1)  Parentheses are free, use them liberally  Multiplication: *, int and double int z = ((3 + 5) * (6)); (z = 48) int x = 21*4; (x = 84)  Equal priority operations are evaluated left-to-right in the double y = 14.1*2.5; (y = 35.25) absence of parentheses  Division: /, different for int and double int w = 3 * 4 / 2 * 6; (w = 36) int x = 21/4; (x = 5) int x = 3 * 4 / (2 * 6); (x = 1) double y = 21/4; (y = 5.0) int y = 3 * 4 + 2 * 6; (y = 24) double y = 21/4.0; (y = 5.25) int z = 3 * (4 + 2) * 6; (z = 108)  Modulus: %, only for int int x = 21%4; (x = 1)

CompSci 001 6.15 CompSci 001 6.16 Automatic type conversion More expressions

 Mixed type expressions are Example: int g = 12 + 2.5; int n = 1 – 2 * 3 – (4 + 5); converted to higher Convert Fahrenheit to Celsius What is the value of g? compatible types a. 0 What is the value of n?  b. 12 If all operands are of type double F=41.0; int then result is type int c. 14 double C=(F-32.0)*(5/9); d. 14.5  If any operand is of type e. error double then result is of type double Question: What is the value of C?  Cannot convert to a lower a) 5 type b) 0.0 int x = 8 * (7 – 6 + 5) % (4 + 3 / 2) – 1;  Conversion may result in c) 9.0 What is the value of x? loss of precision d) 5.0 a. -1 e) 9 b. 0 c. 2 d. 3 e. none of the above CompSci 001 6.17 CompSci 001 6.18

Syntax errors Logic errors

 The following Java subroutine computes the inclusive sum  The computer will do precisely what you say even though it between two integers. Find all the syntax errors. may not be what you want  What is wrong with this code?

int sumBetween( x, y ) int sumBetween( int x, int y ) { { int z = x; int z = x; Int sum = 1; int sum = 1; while( z <= y ){ while( z <= y ) sum = sum*z; sum = sum*z; z++ z++; } } CompSci 001 } 6.19 CompSci 001 6.20 Java objects Real-world objects

 Java is an object-oriented  Suppose we want to describe a car in terms of its attributes and functionality  use objects to define both the data type and the operations that can be applied to the data  Attributes:   Objects have attributes and functionality int year; int mileage;  String make; String model;  attributes describe the state of the object  boolean manual_transmission;  the functionality of an object is the set of actions the object  Methods: can perform  void brake()  In Java, we define an object’s attributes using variables and its  int getMileage() functionality using methods  boolean needsGas()  void shift(int gear)

CompSci 001 6.21 CompSci 001 6.22

Java classes Java String class

 Java objects are created using classes  The String class represents character strings  Encapsulation String first = “Tammy”;  combining elements to create a new entity String last = “Bailey”;  A class encapsulates the variables and methods that define an object  Strings can be concatenated (added together) using the concatenation  Instantiation operator +  the act of creating an object String fullname = first + “ ” + last;   objects are called class instances Testing for equality:  Java provides many predefined classes first.equals(“Tammy”); /* returns true */  You can also define your own classes first.equals(“Amy”); /* returns false */

CompSci 001 6.23 CompSci 001 6.24