Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.

Slides:



Advertisements
Similar presentations
MC697 Object-Oriented Programming Using Java. In this class, we will cover: How the class will be structured Difference between object-oriented programming.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
Object Oriented Programming in JAVA
Introduction to Java IEEM241 Routing and Fleet Management Feb 3, 2005.
Object Orientated Programming
1 Fall 2008ACS-1903 Chapter 1 Topics Java History Java Programs Why Program? Computer Systems: Hardware and Software Programming Languages What Is a Program.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Introduction to Java Programming, 4E
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
Introduction to Java.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
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.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
Introduction to Java Tonga Institute of Higher Education.
Introducing Java.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
Chapter 1: Creating Java Programs
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
CMSC 202 Computer Science II for Majors Object-Oriented Programming.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Introduction to Java Programming with Forte Y. Daniel Liang.
COP 3330 Notes 1/12. Today's topics Downloading Java and Eclipse Hello World Basic control structures Basic I/O Strings.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
JAVA Programming “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
introductory lecture on java programming
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Java Programming, 4E Y. Daniel Liang.
CS 201 Lecture 1 (b) Using an IDE Tarik Booker CS 201: Introduction to Programming California State University, Los Angeles.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Before You Begin Nahla Abuel-ola /WIT.
Introduction to.
Java programming lecture one
Introduction to Java Dept. Business Computing University of Winnipeg
Chapter 1 Coding Introduction.
Java Intro III.1 (Fr Feb 23).
Introduction CSC 111.
(Computer fundamental Lab)
Chapter 1: Programming Basics, Python History and Program Components
Chap 1. Getting Started Objectives
Computer Programming-1 CSC 111
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

Object Oriented Programming Lecture 3

Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual machine (JVM)  The Java platform

Java Programming Language  An object-oriented programming language, which is developed by Sun Microsystems.  A familiar C/C++ style of notation.  Use the object-oriented programming methodology.  Allow the same program to be executed on multiple operating systems.  Contain build-in support for using computer networks.

The Java Platform  The Java platform, is the environment in which a program runs. Two components:  The Java virtual machine  The Java application programming interface (API)

The Java Virtual Machine  The heart of the Java Platform is the concept of a "virtual machine" that executes Java bytecode programs.Java bytecode  Sits between the Java program and the machine it is running on.  Offers the program an “abstract computer” that executes the Java code  virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

Contd…..  Through the Java VM, the same application is capable of running on multiple platforms.

Java Programming Process

Key benefits of Java  Simple: automatic memory allocation and garbage collection  Platform independent.  Network-Centric programming, Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.

Java Editions  J2ME (Micro Edition) – Used to create programs that run on small handheld devices, such as phones, PDAs (personal digital assistants), and appliances.  J2SE (Standard Edition) – Used primarily to create programs for desktop computers or for many computers too large for J2ME and too small for J2EE.  J2EE (Enterprise Edition) – Used to create very large programs that run on servers managing heavy traffic and complicated transactions. These programs are the backbone of many online services, such as banking, e-commerce systems etc.

Installing Java  The Java Development Kit (JDK) is a collection of software available at no charge from Sun Microsystems, Inc.  The v1.6 download is available at java.sun.com.

Java Program Structure  A Java program always consists of one or more classes.  You typically put the program code for each class in a separate file, and  You must give each file the same name as that of the class that is defined within it.  A Java source file name must have the extension.java.

Examples

Sample Java Program class Hello { public static void main ( String[] args ) { System.out.println("Hello World!"); }

Getting Started: Create the source file: open a text editor, type in the code which defines a class Hello and then save it in a file (Hello.java) file and class name are case sensitive and must be matched exactly (except the.java part)  Java is CASE SENSITIVE! Be Careful When You Type  Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher tool (java) are case-sensitive, HelloWorldApp helloworldapp

 The compiler requires the source code files to be named according to specific rules:  Correct: SimpleProgram.java  Incorrect: simpleprogram.java (wrong case) SimpleProgram.java.doc (wrong extension) Simple~1.java.doc (Microsoft Windows short- names not allowed)

(2) Compile the program: compile Hello.java by using the following command: javac Hello.java it generates a file named Hello.class

At the prompt, type the following command and press Enter. javac HelloWorldApp.java The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.

3) Run the program: run the code through: java Hello Note that the command is java, not javac, and you refer to Hello, not Hello.java or Hello.class

Compiler Problems :Common Error Messages  'javac' is not recognized as an internal or external command, operable program or batch file  If you receive this error, Window cannot find the compiler ( javac ).  Suppose you installed the JDK in C:\jdk6. At the prompt you would type the following command and press Enter: C:\jdk6\bin\javac HelloWorldApp.java If you choose this option, you'll have to precede your javac and java commands with C:\jdk6\bin\ each time you compile or run program.

Compiler Problems :Common Error Messages  Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested  If you receive this error, you forgot to include the.java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.

Runtime Problems :Common Error Messages  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp  If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.  One of the places java tries to find your.class file is your current directory. So if your.class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:  cd c:\java The prompt should change to C:\java>. If you enter dir at the prompt, you should see your.java and.class files. Now enter java HelloWorldApp again.

Runtime Problems :Common Error Messages  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class  A common mistake made by beginner programmers is to try and run the java launcher on the.class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename.  Exception in thread "main" java.lang.NoSuchMethodError: main  The Java VM requires that the class you execute with it have a main method at which to begin execution of your application.

Java Class  A class is an object oriented construct. It is designed to perform a specific task.  A Java class is defined by its class name, an open curly brace, a list of methods and fields, and a close curly brace.  The name of the class is made of alphabetical characters and digits without spaces, the first character must be alphabetical.

 The line public static void main ( String[] args ) shows where the program will start running. The word main means that this is the main method –  The JVM starts running any program by executing this method first.  The main method in Hello.java consists of a single statement System.out.println("Hello World!");  The statement outputs the characters between quotes to the console.

Syntax Errors public Class Hello { public static void main ( String[]args) { System.out.println("Hello World!"); }  The required word "class" has been changed to "Class" with a capital "C". This is called a syntax error.  A syntax error is a “spelling or grammatical error" in the program.  The error message is not very clear. But at least it shows where the error is.  The compiler will not create a new bytecode file because it stops compiling when it gets to an error.

Comments // This program outputs “Hello World I am // learning Java” to screen public class MyFirstProgram { public static void main ( String[] args ) { // the output statement starts here System.out.println("Hello World!"); // this statement outputs “Hello World” System.out.println(“I am learning Java”); }  A comment is a note written to a human reader of a program. The program compiles and runs exactly the same with or without comments. Comments start with the two characters "//" (slash slash). Those characters and everything that follows them on the same line are ignored by the java compiler.

Comments /* This is my first Java program and I am pretty excited about it. */ public class MyFirstProgram { public static void main ( String[] args ) { System.out.println("Hello World!"); // this statement outputs “Hello World” System.out.println(“I am learning Java”); }  With this style of comment, everything between the two characters "/*" and the two characters "*/" are ignored by the compiler. There can be many lines of comments between the "/*" and the "*/".