Outline. Java An Introduction. Java Virtual Machine. same compiled program running on many different types of computers.

Similar documents
Java Interview Questions and Answers

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

An Overview of Java. overview-1

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Pemrograman Dasar. Basic Elements Of Java

Crash Course in Java

AP Computer Science Java Subset

Java Programming Fundamentals

C++ INTERVIEW QUESTIONS

Habanero Extreme Scale Software Research Project

The C Programming Language course syllabus associate level

Introduction to Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

public static void main(string[] args) { System.out.println("hello, world"); } }

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Chapter 1 Java Program Design and Development

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Java Application Developer Certificate Program Competencies

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

CS 111 Classes I 1. Software Organization View to this point:

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Computing Concepts with Java Essentials

Moving from CS 61A Scheme to CS 61B Java

Java CPD (I) Frans Coenen Department of Computer Science

Java Crash Course Part I

CS 106 Introduction to Computer Science I

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

The programming language C. sws1 1

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Glossary of Object Oriented Terms

Fundamentals of Java Programming

DNA Data and Program Representation. Alexandre David

Object Oriented Software Design

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Chapter 7D The Java Virtual Machine

Getting Started with the Internet Communications Engine

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Sources: On the Web: Slides will be available on:

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Java programming for C/C++ developers

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Java (12 Weeks) Introduction to Java Programming Language

Object Oriented Software Design

Introduction to Java. CS 3: Computer Programming in Java

java Features Version April 19, 2013 by Thorsten Kracht

Object Oriented Software Design II

Chapter 2 Introduction to Java programming

C++ Programming Language

History OOP languages Year Language 1967 Simula Smalltalk

Chapter 13 - Inheritance

Praktikum im Bereich Praktische Informatik Entwicklung eines Ray-Tracing Systems. computer graphics & visualization

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

An Incomplete C++ Primer. University of Wyoming MA 5310

Part I. Multiple Choice Questions (2 points each):

Basic Programming and PC Skills: Basic Programming and PC Skills:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 5: Java Fundamentals III

Computer Programming I

Lecture 1 Introduction to Android

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Web Development in Java

Semantic Analysis: Types and Type Checking

El Dorado Union High School District Educational Services

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Programmation 2. Introduction à la programmation Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CSC 551: Web Programming. Fall 2001

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

7.1 Our Current Model

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Copyright. Restricted Rights Legend. Trademarks or Service Marks. Copyright 2003 BEA Systems, Inc. All Rights Reserved.

C++FA 5.1 PRACTICE MID-TERM EXAM

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Caml Virtual Machine File & data formats Document version: 1.4

Instruction Set Architecture (ISA)

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

VB.NET Programming Fundamentals

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Introduction to Python

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Syllabus for CS 134 Java Programming

Programming Language Inter-conversion

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Explain the relationship between a class and an object. Which is general and which is specific?

Transcription:

Java An Introduction Outline The Java Virtual Machine Java essential language features data types operators References: Internet Course notes by E.Burris Computing Fundamentals with Java, by Rick Mercer Beginning Java Objects - From Concepts to Code by Jacquie Barker Object-Oriented Design & Patterns, Cay Horstmann Introduction to Java Programming, David Eck Sun white papers on Java 9/1/2010 1 9/1/2010 2 Java Virtual Machine Java uses combination of compilation and interpretation You compile a Java source file (.java) into intermediate byte code (.class file) machine language for a computer that doesn't really exist. The byte codes are interpreted at runtime by the Java Virtual Machine (JVM). Java program can run on any computer that has an interpreter for Java bytecode The interpreter simulates the JVM like Virtual PC simulates a PC same compiled program running on many different types of computers. 9/1/2010 3

Java Virtual Machine It's called a virtual machine because most implementations of the virtual machine will be in software on top of another hardware machine platform. The JVM runs under the OS and the Java program runs under the JVM. Java Structure Java language Java JVM APIs & Libraries Java can be used as a stand-alone language. Using VM: you write java code and VM translates code into machine language needed. Combination of Java and Java bytecode is platform independent. 9/1/2010 5 9/1/2010 6 Java Objects & Methods Java is an object-oriented language Except for simple, native data types, all data in Java are objects All GUI building blocks (windows, buttons, etc) are objects All functions are called methods The main program is a main method that is contained within a class - not stand alone 9/1/2010 7 Structure of a Java Program /** FirstProgram.java A trivial program to illustrate Java programming elements. */ Class wrapper public class FirstProgram{ Main method signature Introductory Comments public static void main(string[] args) { System.out.println( Hello World! ); 9/1/2010 8

Java doesn't have any implementation-dependent behaviors. All primitive types are guaranteed to be a specific size. For example, in C++ an int may be 16-bits or 32-bits. In Java an int is always 32-bits. C++ allows stand-alone or global functions. In Java all functions must be the member of some class. Static member functions don't require the instance of a class to execute. 9/1/2010 9 9/1/2010 10 In C++ you have to declare a function (provide a prototype) before you can use it. Not necessary in Java because compiler makes multiple passes. In C++ objects can be created at compile time or at run time. In Java all objects are allocated dynamically from the heap (i.e. at run time). In Java cannot directly manipulate memory locations by address. No pointer arithmetic with arrays 9/1/2010 11 Pointers vs Object References Java always uses pointers. They re called references An object variable is a reference to an object -- a value that describes the location of the object. Java C++ C++ Bank b; Bank *b; (dynamic) Bank b; (static) b = new Bank(); b = new Bank(); In Java, only the dot operator is used for dereference (no arrows or *s). Java C++ C++ b.setbalance(100); b->setbalance(100); b.setbalance(100); (*b).setbalance(100); 9/1/2010 12

Object References Multiple variables can store references to the same object. For example, after the following statements: Bank bank = new Bank(); Bank anotherbank = bank; both object variables (bank and anotherbank) refer to the same object Java References Pointers in Java are safe You cannot create invalid pointers The garbage collector automatically reclaims unused objects -- you can't have a memory leak. 9/1/2010 13 9/1/2010 14 Implicit Parameter this this refers to the object on which the method was invoked. Assume class Bank has the method public void setname (String name) { this.name = name; then bank.setname( First National ) sets the name field of the Bank object to the value of the explicit parameter which is also called name. Use of this resolves ambiguity between the name field and name parameter 9/1/2010 15 Java super Method In Java the keyword super refers to the superclass or parent class of a class. super is a reference you can use in a subclass to call a method in the parent class. public void paintcomponent(graphics g) { super.paintcomponent(g); Call to super must be the first statement in method 9/1/2010 16

Java super Method The reference super is also used to call a specific constructor in a parent class. public class TypeSafeVector extends Vector { This Class s Constructor TypeSafeVector(int size, Object elementtype) { super(size);... Vector class constructor In C++ the integer 0 can be exchanged for the bool value false and any non-zero integer can be exchanged for the value true. In Java you can't interchange integer and boolean values. In Java both primitive types and reference types are passed by value. There is no pass by reference (&). 9/1/2010 17 9/1/2010 18 Java objects don't have destructors. Java doesn't have a pre-processor. Java has no typedefs, #defines or structs There is no goto statement in Java. (goto is a reserved word, but it isn't used.) But you can add a label to a loop and use that label as an argument on a break or continue statement. 9/1/2010 19 Java has no arithmetic operator overloading. Effects of operator overloading can be achieved by declaring a class, appropriate instance variables, and appropriate methods to manipulate those variables. 9/1/2010 20

: Exceptions Exception handling is optional in C++; it is mandatory in Java. If you do something that causes ( throws ) an exception, then the program must handle ( catch ) it. More on exceptions later No multiple inheritance. Java uses interfaces to implement multiple inheritance. An interface is a definition of a set of methods that one or more classes will implement. An important issue of interfaces is that they declare only methods and constants. Variables may not be defined in interfaces. (Sun) 9/1/2010 21 9/1/2010 22 Java Data Types & Operators 9/1/2010 23 Data Types Two categories of data types in Java: 1. primitive 2. reference A primitive data type represents a single value of a certain type and size recognizable by the compiler. For example, int i = 5; defines a 4-byte storage area that will hold integer values in the two's complement data format. A reference data type represents a reference to an instance of a class. 9/1/2010 24

Data Types Data Types There are 8 primitive data types in Java: Integers Data type Length byte 1 byte -128.. 128 short 2 bytes -32,768.. 32,767 int 4 bytes -2,147,483,648.. 2,147,483,647 long 8 bytes -9,223,372,036,854,775,808.. 9,223,372,036,854,775,807 9/1/2010 25 Floating Point Data type Length Format float 4 bytes IEEE 754 double 8 bytes IEEE 754 Other Data type Length Format char 2 bytes Unicode boolean N/A true/false Note: no unsigned int type 9/1/2010 26 Char Data Types // Character type // Characters are represented in Unicode -- 16-bit encoding standard char c; // 2 byte Unicode // boolean -- note, this is different from C++(bool) boolean bl; // can be true or false b = 128; // this an error in Java // boolean and int are not interchangeable Type Conversions Automatic type conversions are done only if no precision will be lost in the conversion (C++ will convert and truncate if necessary) int x; double y = 3.5; x = y; //in Java, will be an error Explicit type conversion can be done with a cast to override this decision: x = (int) y; //cast the double to an int 9/1/2010 27 9/1/2010 28

Constants Decimal constants (such as 1.2) are by default of type double. You can't directly assign or convert a double to a float because there might be a loss of data. You specify a decimal constant of type float by following the value with an F. For example: 1.2F. Initializing Variables Default values: boolean answer = false; //note spelling int i=0; double rate = 0.0; char ch = ; //blank space String name = null; 9/1/2010 29 9/1/2010 30 Naming conventions Variable names are lowercase if one word. If compound words, the first letter of second (third ) word is capitalized. item productnumber nameofthiscustomer A long name is preferable to a confusing short one! Constants The key word final denotes a constant: final int MAX_VALUE = 3; You can assign a value to a final variable only once. You don't have to assign a value in the definition, but it's good programming style. Final variables are ALL CAPS. 9/1/2010 31 9/1/2010 32

Other Operators Dot operator (.) The only operator used to access the methods or member data of an object. instanceof operator Tests whether a reference is of a particular type or is to a class that implements a particular interface Object obj = e.nextelement(); if (obj instanceof Shape) { Shape s = (Shape) obj; cast operator (type). Converts from one type to another. The example here casts a reference to an Object to a reference to a Shape. Shape s = (Shape) e.nextelement(); 9/1/2010 33