Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Chapter 7: User-Defined Functions II
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Java Software Solutions
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Subclasses. Inheritance class Animal { int row, column; // will be inherited private Model model; // private prevents inheritance Animal( ) {... } //
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Static Class Members Wrapper Classes Autoboxing Unboxing.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Introduction to Java Programming Language Junji Zhi University of Toronto 1.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CIS 068 JAVA vs. C++ Main Differences. CIS 068 JAVA vs C++ Java is (an interpreted) write once, run anywhere language. –The biggest potential stumbling.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
INTRODUCTION TO JAVA. Introduction to Java Java programming language ◦ Object-Oriented Programming ◦ Compiled to byte code of JVM Java virtual machine.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Copyright © Curt Hill Structured Data What this course is about.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Classes, Interfaces and Packages
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming Language Lecture27- An Introduction.
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.
CIS 068 JAVA vs. C++ Main Differences CIS 068.
Object Oriented Programming in
JAVA MULTIPLE CHOICE QUESTION.
BRIEF Overview ON THE MAJOR Similarities & Differences
Object-Oriented Programming with Java
Java Primer 1: Types, Classes and Operators
C Basics.
Instructor: Ioannis A. Vetsikas
Java Programming Language
Object-Oriented Software Engineering
BRIEF Overview ON THE MAJOR Similarities & Differences
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Language
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD

Java Java is just one of a number of languages designed to occupy the intersection between object-orientation and C. Other such languages include: –C++ –C# –Objective C

Assumptions It is assumed here that you know C++ or C, or some Algol-based language. This lecture discusses the differences between C/C++ and Java based on Flanagan, 2002, Java in a Nutshell, 4th edition. For more detail, see the Sun introductory tutorials and Flanagan.

Preprocessor Java has no preprocessor: No macros. No analogs of #define, #include, or #ifdef. No header files No conditional compilation assert was added as a language statement in Java

Global Variables None, nada. Packages contain classes. Classes contain fields and methods. Methods contain local variables. To simulate a global variable, use a public static member field of some class. For examples of how to do this, look at the Arrays, Math, or Collections classes.

Primitive Types All the primitive types in Java have well-defined, machine- independent sizes and properties. Learn them. These include: –boolean –char –byte –short –int –long –float –double

‘Horses for Courses’ Primitive types lack methods and cannot be stored in collections that expect some sort of object. Each primitive type has a corresponding class (with useful methods) that provides instances that can be stored in a collection. Boolean—boolean Character—char Byte—byte Short—short Integer—int Long—long Float—float Double—double

Pointers There are no programmer-accessible pointers in Java. Classes and arrays in Java are reference types. Java manages the underlying pointers. There is no way to convert from a reference to a primitive object like you can treat a C pointer as an integer type. You cannot use a reference to access a private member attribute.

Objects Reference types inherit from the class Object. Object provides a number of methods, including: –Class getClass(); –String toString(); –boolean equals(Object o); // by value –int hashcode(); // also by value –Object clone() …; toString(), equals(), hashcode(), and clone() should usually be overridden if a class uses them.

Strings A String is an object like a C++ string, not an array like a C string. A String is constant once it is created. If you want to change a String, give the name a new value. Among other ways, Strings can be created by the toString operator applied to an object, by setting the object equal to a literal (String name =“data”;), and by concatenation using + and +=.

Garbage Collection Java manages the heap. When a reference type object goes out of scope, it gets marked for later clean-up. You don’t need to delete or return any storage. Cleanup happens at the convenience of the Java runtime environment. You can suggest that the time is right by calling System.gc(), but that is only a suggestion. This is why Java is unsuitable for real-time applications, even though it was designed for embedded systems. To create an object of a reference type (array or class instance), you usually use the new operator.

Syntax Variables may be declared anywhere. The variable name is in scope in the local block from the point of declaration. Reference types are set to null (non-existent) until they are given a value. Primitive types have a default value. Forward references within a class definition are usually OK, but not within method code. Within a method, local variables must be in scope before they are used. Method overloading is allowed. The argument type list is part of the method signature. No operator overloading (except for the String class, which has + and += defined).

None of the Following are Available: No goto statement No structs (use class) No unions No enums (use object constants) No bitfields No typedefs No method pointers (use functors) No variable-length argument lists

Write Once/Run Anywhere Java is designed to be architecture-independent. The compiler will convert your codefiles into class files that can be executed anywhere. I run it under MacOS X; the Suns run it under Solaris, and Windows also runs it. That makes it slower than native code, but faster than interpreted scripts like PHP or Perl. This scares M$.

Summary Java is based on C and C++, but is not an extension of either. Assuming Java is C with classes will lead you into problems. Know the similarities and the differences!