Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last...

34
Fall 2015 CISC124 - Prof. McLeod 1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been added to the “Instructor” page. Labs start next week: Go to meet your TA. Work on Exercises. Exercise 1 to 3 are good preparation for assignment 1. Ask any questions you have about getting IDE working, exercises, lecture material, etc.

Transcript of Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last...

Page 1: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 1

CISC124

• Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.)

• TA names have been added to the “Instructor” page.

• Labs start next week:– Go to meet your TA.– Work on Exercises. Exercise 1 to 3 are good

preparation for assignment 1.– Ask any questions you have about getting IDE working,

exercises, lecture material, etc.

Page 2: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Today

• How Java works, Cont.

• Begin Java Syntax: – Class Structure.– Primitive Types.

Fall 2015 CISC124 - Prof. McLeod 2

Page 3: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

History of Java, From Last Lecture

• Java was first used to build applets attached to web pages to make them interactive.

• These were compact, platform-independent byte code files.

• One compiler, many virtual machines. The VMs were embedded in the various browsers.

• Later Java was modified to produce stand-along applications.

Fall 2015 CISC124 - Prof. McLeod 3

Page 4: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

4Fall 2015 CISC124 - Prof. McLeod

How Java Works, Cont.• So, Java can be used either to create applets for

use in web pages or for stand-alone applications.

• Most “Integrated Development Environments” or IDE’s will support the development of either kind of program.

Page 5: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

5Fall 2015 CISC124 - Prof. McLeod

How Java Works, Cont.

• However, all IDE’s, including Eclipse, must use the appropriate JDK in the background.

• Two components of the JDK are the programs “javac.exe” and “java.exe”.

• javac.exe is the byte code compiler, and java.exe is the JRE which executes the byte code file.

• “Compilation” is the process of converting the *.java file to a *.class file (the byte code file). This is done by calling javac.exe in the background, and supplying that program with all the required command line parameters.

Page 6: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

6Fall 2015 CISC124 - Prof. McLeod

How Java Works, Cont.

• The java.exe program:– accepts the byte code file, – links in any required libraries,– creates executable code in memory– converts it to machine language– and sends it to the CPU.

• The java.exe program must know the right machine language commands for just the type of CPU it is designed for!

Page 7: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

7Fall 2015 CISC124 - Prof. McLeod

Text Editor

Compiler

Linker

Loader

Source

Errors

Bytecode

Library

Executable

Input Data

Output Data

text

binary

binary

binary

successful

not successful

Eclipse

javac.exe

java.exe

Page 8: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

8Fall 2015 CISC124 - Prof. McLeod

Questions:

• Suppose we have two kinds of errors:– Syntax errors– Runtime errors

• What is the difference?• Considering the two step process of running a

Java program, at what stages will these errors be caught and who does the catching?

• Who does the fixing???

Page 9: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 9

OOP in Java

• A class or “object definition” or an “object”, consists of instance variables and/or methods.

• By convention, instance variables are all declared before the methods:

public class ShowStructure {

// instance variables or “attributes” here

// methods here

} // end class ShowStructure

Page 10: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 10

OOP in Java – Cont.

• By convention, class names start with a capital letter, member names (attributes and methods) start with a lower case letter.

• In Java, a class is an Object, and an Object is a class (rather Zen is it not!)

• Code and attributes cannot be defined outside of a class.

• The only code that can exist outside a method are attributes or other (“inner”) class definitions.

Page 11: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 11

Attributes

• Also called “class variables” or “instance variables” or “fields”.

• Declared within a class at the same level as the method declarations.

• These variables are known to all methods within a class (their scope).

• You can control their privacy and the way they are stored in memory (using public/private/protected and static).

Page 12: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Aside – Access Modifiers

• public means the attribute or method is available to any external class (as well as inside the class).

• private means that the attribute or method, the “member”, is only available inside the class in which it is declared.

• protected means the member is only public to classes in the same package as the class in which the member is declared.

Fall 2015 CISC124 - Prof. McLeod 12

Page 13: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

static (First Pass)• static means different things depending on

where it is used. For now, consider:

• public static members are available outside the class without the need to instantiate the class.

• Any static member remains in memory until the program is complete.

• Since main is static, it can only invoke other static methods when they are in the same class.Fall 2015 CISC124 - Prof. McLeod 13

Page 14: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 14

Attribute Declaration

• Syntax:

[private|public] [static] [final] type attributeName [= literalValue];

• Note that the type part is not optional – this is why java is a declarative language.

• And, a variable cannot change its type later (static typing).

• You cannot use a variable unless you have declared it first.

Page 15: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 15

Variable Declaration

• Declaring a variable inside a method gives that variable the scope of just inside the method, not outside the method.

• Generally a variable is only available inside the block ({…}) in which it is declared.

• The syntax for declaration inside a method is the same except you don’t need the [private|public] [static] [final] parts.

Page 16: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 16

• The syntax for simple method declaration:

[private|public] [static] [final] returnType methodName ([parameterList]) {…}

• If main invokes methods or uses attributes in the same class as itself then those attributes and methods must also be declared static. (If you forget the pre-compiler in Eclipse will remind you!)

Method Declaration (a “Header”)

Page 17: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 17

Method Declaration - Cont.

• A method must have a returnType.• The returnType can be any single Object or a

primitive type. (For example: an int, double, String, an array (like int[], or any other pre-defined Object.)

• If the method does not return anything, then the keyword void is used instead.

• The main method does not return any value, so that’s why it is declared as in:

public static void main (String[] args) {…}

Page 18: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

18Fall 2015 CISC124 - Prof. McLeod

Aside - The main Method

• For the JVM to run an application, it must know where to start.

• By design, the starting point is always the execution of the main method.

• The JVM expects the main method to be declared exactly as shown – the only thing you can change is the name of the String array, called args above.

Page 19: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 19

Method Declaration - Cont.

• parameterList provides a means of passing items, or parameters, into a method.

• It is optional.• It can consist of one or many parameters,

separated by commas.• Each parameter type must be declared in the

parameter list, as in type variableName, type variableName, …

• Java does not have named or optional parameters as does Python.

Page 20: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 20

Methods - the return Statement

• Also important: Unless the return type is void, the method must contain at least one return statement. A void method can also use a return statement without anything after it, as a means of terminating the method.

• A method always stops (terminates) when a return statement is encountered.

• Syntax:

return [literal|expression];

Page 21: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 21

The return Statement - Cont.

• The type of “literal|expression” must match the return type specified in the method declaration statement.

Page 22: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 22

Method Examples

public static void printHello() {System.out.println(“Hello”);

} // end printHello

public static void printHelloName(String yourName) {System.out.println(“Hello “ + yourName);

} // end printHelloName

public static void printAvg(int a, int b) {System.out.println((a + b) / 2.0);

} // end printAvg

Page 23: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 23

Method Examples - Cont.

public static double average(double a, double b) {return (a + b) / 2;

} // end average

public static int lowest(int a, int b) {if (a <= b)return a;

elsereturn b;

} // end lowest

Does this have to be here?

Page 24: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Attribute Examples

public static double aVar;

public static int aNum = 100;

private static String hello = “Hello”;

Fall 2015 CISC124 - Prof. McLeod 24

Note: these methods and attributes are written as if they are in the same class as main and main will be using them.

Page 25: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Example: A Simple Class

public class Simple {

public static int aNum = 100;

public static int sumNums(int num1, int num2) {return num1 + num2;

}public static void main(String[] args) {

int anotherNum = 200;System.out.println(sumNums(aNum,

anotherNum));}

}

Fall 2015 CISC124 - Prof. McLeod 25

Page 26: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Python versus Java (Round 1)• You can see some major syntactical differences

already:– Variable type declaration in mandatory. Non-void return

type declaration is mandatory. Parameter type declaration is mandatory.

– Variable types are not dynamic!– A class header is required.– public static, The ;– Indentation in Java is for style only. { } are used to

indicate code blocks or code containment.– println() must be invoked as a member of the System.out object.

Fall 2015 CISC124 - Prof. McLeod 26

Page 27: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 27

Primitive Types in Java

• Java primitive types:

charbyte shortintlongfloatdoubleboolean

Page 28: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 28

Primitive Types?

• What is a primitive type anyways?

• Everything else in Java is an Object.• A variable declared as one of the types shown on

the previous slide is not an Object.• Why does Java have primitive types?

• Object construction often involves the data abstraction of several primitive type attributes.

Page 29: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 29

Integer Primitive Types

• byte, short, int, long

• For byte, from -128 to 127, inclusive (1 byte).• For short, from -32768 to 32767, inclusive (2

bytes).• For int, from -2147483648 to 2147483647,

inclusive (4 bytes). • For long, from -9223372036854775808 to

9223372036854775807, inclusive (8 bytes).

• A “byte” is 8 bits, where a “bit” is either 1 or 0.

Page 30: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 30

Aside - Number Ranges

• Where do these min and max numbers come from?

• Memory limitations and the system used by Java (two’s complement) to store numbers determines the actual numbers.

• The Wrapper classes can be used to provide the values - for example:

Integer.MAX_VALUE // returns the value 2147483647

Page 31: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 31

Real Primitive Types

• Also called “Floating Point” Types:

• float, double

• For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 1038 to 7 significant digits.

• For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits.

Page 32: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 32

Character Primitive Type

• char

• From '\u0000' to '\uffff' inclusive, that is, from 0 to 65535 (base 10) or 0 to ffff (base 16, or “hexadecimal”). A variable of the char type represents a Unicode character. Can also be represented as 'a' or '8', etc.

Page 33: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 33

Boolean Primitive Type

• boolean is either true or false.

Page 34: Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.

Fall 2015 CISC124 - Prof. McLeod 34

String Objects

• String’s are not primitive data types, but are Objects.

• A String is declared in the same way as a primitive type using the keyword: String.