Download presentation
Presentation is loading. Please wait.
Published byMerryl O’Neal’ Modified over 8 years ago
1
Introduction to Java John Lewis
2
Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced Object Concepts Graphical User Interfaces Reflection and JavaBeans Distributed and Web Based Computing Databases
3
Introduction Java is both a language and a platform. You write programs in the language Compile them into Java bytecode Run the bytecode on the Java Virtual Machine.
4
Traditional Model Source Code (C,C++ etc.) Native Machine Code Compiler Executed Directly by Processor
5
Java Model Source Code (Java) Java Bytecode Compiler Executed by Java Virtual Machine
6
Platform Independence Because Java bytecode is executed by a Java Virtual Machine (JVM), and not directly on the processor, you get platform independence. Each platform must have it’s own JVM. Sun provides JVMs for Solaris, Windows and Linux.
7
Platform Independence In C and C++ – Source code is portable, to a certain extent. –The machine code is not. In Java –Source code is portable in same way –Java bytecode is also portable.
8
Compile once – Run Anywhere Can compile application or applet on any machine. Should run on any other machine with JVM, irrespective of architecture.
9
Performance Having the JVM execute Java bytecode is slower than having the processor execute native machine code. So, Java is slower than C (in execution, at any rate.) However, only about 10% slower. Just-In-Time Compiler inside JVM turns bytecode into native code.
10
Traditional programs consist of a set instructions acting on a store of data: Instructions can be grouped into functions Data can be grouped into structures and arrays. That’s it! Also known as the imperative approach to programming. The Theory of Objects DataFunctions
11
The Theory of Objects In the Object Orientated model, developed from the 1960’s onwards, instead of functions and data, we have objects and messages. A program consists of a set of objects sending messages to one another.
12
An object consists of two sets: –Attributes: Information describing the state of the object. (adjectives). Attributes have values, which can be objects themselves. –Behavior: How it responds to messages (verbs) Objects Behavior Attributes
13
Real World Objects The text book has (among others) the following attributes: –Title = “Thinking in Java” –Author = “Bruce Eckel” –Number of Pages = 1124 –ISBN Number = “0-13-027363-5” –Publication Date = 2000
14
Real World Objects And it has the following behaviors, also called “methods”: –TurnPage (responds to TurnPage message) –Open (responds to Open message) –Close (responds to Close message) –ReadPage (responds to ReadPage message)
15
Difference Imperative (C, BASIC, Pascal) : –Data Store –Stateless functions on Data Object Orientated (Java, C#, C++) –Objects with Attributes and Behavior –Messages
16
Object Classes Earlier we looked at the attributes and behavior of the textbook. Here is another, heavier, yet shorter Java book. It has the same attributes and behavior as the big book: –Title, author, number of pages, ISBN, year –Open, Close, TurnPage, ReadPage
17
Object Classes Both books, “Thinking in Java” and “Java Programming: Advanced Topics” belong to the class “Book” “Thinking in Java” IS_A “Book” “Java Programming: Advanced Topics” IS_A “Book” “Thinking in Java” and “Java Programming: Advanced Topics” are instances of the class “Book” They have common attributes and behavior, but different values for those attributes.
18
Interface and Implementation An important concept in Object Orientated Programming is separating the Interface from the Implementation What? The Interface: How other objects interact with your object. The Implementation: How the object works internally.
19
Interface and Implementation The first reason for this separation is to protect the users (clients/consumers) of our object from internal changes. The second reason is to protect the internals of our object from the clients.
20
Interface and Implementation In Java, there are two ways to separate interface from implementation: –Setting access modifiers for methods and attributes. –Defining and implementing interfaces.
21
Your First Java Program Its “Hello World”! But instead of just printing “Hello World!”, –We create a World class, –Create an instance of that class. –Set the message we want to say. –And call the sayIt method.
22
World.java public class World { // behavior public void setMessage(String msg) { message = msg; } public void sayIt() { System.out.println(message); } // attributes private String message; }
23
HelloWorld.java public class HelloWorld { public static void main(String[] args) { World w = new World(); w.setMessage(“Hello World!”); w.sayIt(); }
24
Compiling and Running Compiling (on any platform) –javac World.java –javac HelloWorld.java Running (on any platform) –java HelloWorld
25
Basic syntax Program consists of classes. Classes consists of attributes and methods. Methods consist of statements separated by semi-colons. Everything is a class, except atomic types. Atomic Types: byte, char, short, int, long, float, double, boolean
26
Declaring Variables Atomic Types: –byte b; –char c; –int i; –boolean bool; –long l; Objects: –World w = new World(); –String s = new String(); –Vector v = new Vector();
27
Examples int x; int y = 100; char c = ‘a’; float f = 1.234; double d = 10000000000000000000000; String s = “This is a string”; s = “s is now equal to this”;
28
Operators assignment: = arithmetic: +, -, *, / boolean:,, ==, != logical: &&, ||, &, | In Java, you cannot overload operators.
29
Operators x = x+1; x++ x = x-1; x--; x = x+y; x+=y; x = x-y; x-=y; x = x*y; x*=y; x = x/y; x/=y; x = x%y; x%=y;
30
Flow Control: if if (expression) { // expression evaluates to true } if (expression) { // expression evaluates to true } else { // expression evaluates to false }
31
Flow Control: if Expression must evaluate to a boolean, true, or false Not an integer (zero or non-zero) as in C,C++ etc.
32
Flow Control: while while (condition) { // statements to execute while true } // Example: int x; while (x < 100) { x++; }
33
Flow Control: do do { // execute while true } while (condition); // example: int x = 0; do { x++; } while (x < 100);
34
Flow Control: for for (initializer, conditional, increment) { // body } // example: for (int i = 0; i < 100; i++) { System.out.println(i); }
35
Flow control: switch switch(ordinal) { case option1: /* execute this */; break; case option2: /* execute this */; break; case option3: /* execute this */; break; default: /* do this if we can’t match */ } // Example int i = 2; switch(i) { case 1: System.out.println(“one”); break; case 2: System.out.println(“two”); break; case 3: System.out.println(“three”); break; }
36
Classes Classes are declared using the class keyword. The class name and the file name must match, is class X must be in file X.java Methods and attributes are defined inside the class body. Access modifiers are declared for each class, each method, and each attribute separately.
37
Class Declaration & Definition [public|private] class ClassName { [public|private][static][void|typeName] methodName([parameterList]) { // method body; [return someThing;] } [public|private] typeName attributeName [= something| null;] }
38
Creating instances of classes (Objects) Use new keyword: className objectName = new ClassName(); Access public method and attributes using. (dot) operator. –objectName.methodName(); –objectName.attributeName = something; –attributeClass c = objectName.attributeName ;
39
Maths.java pubic class Maths { public float add(float a, float b) { return a + b; } public float mult(float a, float b) { return a * b; } public float sub(float a, float b) { return a - b; }
40
Driver.java public class Driver { public static void main(String[] args) { Maths m = new Maths(); float six = m.mult(2.0, 3.0); float ten = m.add(5.0, 5.0); float two = m.sub(7.0, 2.0); System.out.println(“Six = “ + six + “, Ten = “ + ten + “, Two = “ + two); }
41
Person.java class Person { public String firstName; public String surname; public int age; }
42
People.java public class People { public static void main(String[] args) { Person person1 = new Person(); person1.firstName = “George”; person1.surame = “Bush”; person1.age = 57; // ?? Person person2 = new Person(); person2.firstName = “Saddam”; person2.surname = “Hussein”; person2.age = 60; // ?? }
43
Getters and Setters If you make your attributes public, other objects can mess around with the internal state of your object. This is Bad! You can protect your attributes by making them private, and creating getter and setter methods to access and modify the value.
44
Getters & Setters Naming Rules For private attribute named “type attribute”: Getter: public type getAttribute() { return attribute; } Setter: public void setAttribute(type attribute) { this.attribute = attribute; }
45
Getters & Setters public class Person { private String name; private String surname; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } }
46
toString() methods A toString method allows an object to return a human readable string describing itself. The System.out.println(…) method calls the toString method of an object (but not atomic types)
47
toString() public class Person { private String firstName; private String surname; private int age; public string toString() { return firstName + “ “ + surname + “, age “ + age; }
48
toString() public class People { public static void main(String[] args) { Person p = new Person(); p.setName(“George”); p.setName(“Bush”); p.setAge(57); System.out.println(p); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.