Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.

Slides:



Advertisements
Similar presentations
Introduction to Programming G51PRG University of Nottingham Revision 1
Advertisements

1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Outline Java program structure Basic program elements
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Introduction to Methods
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
DAT602 Database Application Development Lecture 5 JAVA Review.
Java Language and SW Dev’t
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Introduction to Computer Systems and the Java Programming Language.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CIT 590 Intro to Programming First lecture on Java.
Agenda Object Oriented Programming Reading: Chapter 14.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Applications Development
Introduction to Java Java Translation Program Structure
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
The need for Programming Languages
JAVA MULTIPLE CHOICE QUESTION.
Chapter No. : 1 Introduction to Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Internet and Java Foundations, Programming and Practice
Data types and variables
Programming Language Concepts (CIS 635)
Introduction to Java Dept. Business Computing University of Winnipeg
Introduction to Programming in Java
An Introduction to Java – Part I, language basics
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
Presentation transcript:

Introduction to Java John Lewis

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

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.

Traditional Model Source Code (C,C++ etc.) Native Machine Code Compiler Executed Directly by Processor

Java Model Source Code (Java) Java Bytecode Compiler Executed by Java Virtual Machine

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.

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.

Compile once – Run Anywhere Can compile application or applet on any machine. Should run on any other machine with JVM, irrespective of architecture.

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.

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

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.

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

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 = “ ” –Publication Date = 2000

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)

Difference Imperative (C, BASIC, Pascal) : –Data Store –Stateless functions on Data Object Orientated (Java, C#, C++) –Objects with Attributes and Behavior –Messages

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

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.

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.

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.

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.

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.

World.java public class World { // behavior public void setMessage(String msg) { message = msg; } public void sayIt() { System.out.println(message); } // attributes private String message; }

HelloWorld.java public class HelloWorld { public static void main(String[] args) { World w = new World(); w.setMessage(“Hello World!”); w.sayIt(); }

Compiling and Running Compiling (on any platform) –javac World.java –javac HelloWorld.java Running (on any platform) –java HelloWorld

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

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();

Examples int x; int y = 100; char c = ‘a’; float f = 1.234; double d = ; String s = “This is a string”; s = “s is now equal to this”;

Operators assignment: = arithmetic: +, -, *, / boolean:,, ==, != logical: &&, ||, &, | In Java, you cannot overload operators.

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;

Flow Control: if if (expression) { // expression evaluates to true } if (expression) { // expression evaluates to true } else { // expression evaluates to false }

Flow Control: if Expression must evaluate to a boolean, true, or false Not an integer (zero or non-zero) as in C,C++ etc.

Flow Control: while while (condition) { // statements to execute while true } // Example: int x; while (x < 100) { x++; }

Flow Control: do do { // execute while true } while (condition); // example: int x = 0; do { x++; } while (x < 100);

Flow Control: for for (initializer, conditional, increment) { // body } // example: for (int i = 0; i < 100; i++) { System.out.println(i); }

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; }

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.

Class Declaration & Definition [public|private] class ClassName { [public|private][static][void|typeName] methodName([parameterList]) { // method body; [return someThing;] } [public|private] typeName attributeName [= something| null;] }

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 ;

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; }

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); }

Person.java class Person { public String firstName; public String surname; public int age; }

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; // ?? }

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.

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; }

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; } }

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)

toString() public class Person { private String firstName; private String surname; private int age; public string toString() { return firstName + “ “ + surname + “, age “ + age; }

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); }