Introduction to Java University of Sunderland CSE301 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
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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
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.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
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.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
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.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © Curt Hill Structured Data What this course is about.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
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.
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.
Chapter 7 User-Defined Methods.
Object-Oriented Programming with Java
Java Primer 1: Types, Classes and Operators
Java Programming Language
Object-Oriented Software Engineering
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Language
Presentation transcript:

Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD

Notes We will explore basic C syntax that is reused in Java. We will also have one or two lectures on the new stuff in Java 5.0. Today we will examine some differences between Java 1.4 and C++

Java Java is just one of several 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 5.0 based on Flanagan, 2005, Java in a Nutshell, 5th edition. For more detail, see the Sun introductory tutorials and Flanagan’s book.

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 1.4.

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 cold. I guarantee there will be an exam question of some sort worth 10 marks on 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 (and interfaces) 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. Know the three basic reference types!

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() …; These are always available for a reference type. 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 memory (heap or free store). When a reference type object goes out of scope, it gets marked for later clean-up. You never 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 that you need to memorize. 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 classes) No unions No enums (use object constants, changed for Java 5.0) No bitfields No typedefs No method pointers (use functors) No variable-length argument lists (changed for Java 5.0)

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 serious problems. My exam questions are designed to be nearly impossible to answer based only on a knowledge of C/C++. Know the similarities and the differences!