Download presentation
Presentation is loading. Please wait.
Published byAnna Hutchinson Modified over 9 years ago
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 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.
2
Today How Java works, Cont. Begin Java Syntax: –Class Structure. –Primitive Types. Fall 2015CISC124 - Prof. McLeod2
3
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 2015CISC124 - Prof. McLeod3
4
Fall 2015CISC124 - Prof. McLeod4 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.
5
Fall 2015CISC124 - Prof. McLeod5 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.
6
Fall 2015CISC124 - Prof. McLeod6 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!
7
Fall 2015CISC124 - Prof. McLeod7 Text Editor Compiler Linker Loader Source Errors Bytecode Library Executable Input Data Output Data text binary successful not successful Eclipse javac.exe java.exe
8
Fall 2015CISC124 - Prof. McLeod8 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???
9
Fall 2015CISC124 - Prof. McLeod9 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
10
Fall 2015CISC124 - Prof. McLeod10 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.
11
Fall 2015CISC124 - Prof. McLeod11 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 ).
12
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 2015CISC124 - Prof. McLeod12
13
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 2015CISC124 - Prof. McLeod13
14
Fall 2015CISC124 - Prof. McLeod14 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.
15
Fall 2015CISC124 - Prof. McLeod15 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.
16
Fall 2015CISC124 - Prof. McLeod16 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”)
17
Fall 2015CISC124 - Prof. McLeod17 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) {…}
18
Fall 2015CISC124 - Prof. McLeod18 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.
19
Fall 2015CISC124 - Prof. McLeod19 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.
20
Fall 2015CISC124 - Prof. McLeod20 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];
21
Fall 2015CISC124 - Prof. McLeod21 The return Statement - Cont. The type of “ literal|expression ” must match the return type specified in the method declaration statement.
22
Fall 2015CISC124 - Prof. McLeod22 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
23
Fall 2015CISC124 - Prof. McLeod23 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; else return b; } // end lowest Does this have to be here?
24
Attribute Examples public static double aVar; public static int aNum = 100; private static String hello = “Hello”; Fall 2015CISC124 - Prof. McLeod24 Note: these methods and attributes are written as if they are in the same class as main and main will be using them.
25
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 2015CISC124 - Prof. McLeod25
26
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 2015CISC124 - Prof. McLeod26
27
Fall 2015CISC124 - Prof. McLeod27 Primitive Types in Java Java primitive types: char byte short int long float double boolean
28
Fall 2015CISC124 - Prof. McLeod28 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.
29
Fall 2015CISC124 - Prof. McLeod29 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.
30
Fall 2015CISC124 - Prof. McLeod30 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
31
Fall 2015CISC124 - Prof. McLeod31 Real Primitive Types Also called “Floating Point” Types: float, double For float, (4 bytes) roughly ±1.4 x 10 -38 to ±3.4 x 10 38 to 7 significant digits. For double, (8 bytes) roughly ±4.9 x 10 -308 to ±1.7 x 10 308 to 15 significant digits.
32
Fall 2015CISC124 - Prof. McLeod32 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.
33
Fall 2015CISC124 - Prof. McLeod33 Boolean Primitive Type boolean is either true or false.
34
Fall 2015CISC124 - Prof. McLeod34 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.