Assumptions. History

Similar documents
Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Introduction to Programming Using Java (98-388)

Selected Java Topics

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Introduction to Java

The Java Programming Language

CA341 - Comparative Programming Languages

VARIABLES AND TYPES CITS1001

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Selected Questions from by Nageshwara Rao

CS 231 Data Structures and Algorithms, Fall 2016

CS 376b Computer Vision

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

JAVA An overview for C++ programmers

Short Notes of CS201

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

377 Student Guide to C++

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS201 - Introduction to Programming Glossary By

Kickstart Intro to Java Part I

Chapter 1 Getting Started

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS201- Introduction to Programming Current Quizzes

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Lecture 2, September 4

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Programming. Syntax and Semantics

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Program Fundamentals

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

2 rd class Department of Programming. OOP with Java Programming

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSC1322 Object-Oriented Programming Concepts

COMP 2355 Introduction to Systems Programming

Brief Summary of Java

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

Introduction To C#.NET

From C++ to Java. Duke CPS

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CE221 Programming in C++ Part 1 Introduction

CS2141 Software Development using C/C++ C++ Basics

Programming Language Concepts: Lecture 2

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Special Topics: Programming Languages

Scope. Scope is such an important thing that we ll review what we know about scope now:

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Chapter 2. Procedural Programming

a data type is Types

Java Primer 1: Types, Classes and Operators

Java: framework overview and in-the-small features

CS3157: Advanced Programming. Outline

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Fast Introduction to Object Oriented Programming and C++

Introduction to Java

PIC 20A The Basics of Java

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Computer Components. Software{ User Programs. Operating System. Hardware

Inheritance, and Polymorphism.

Java Classes & Primitive Types

Java Classes & Primitive Types

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

PROGRAMMING FUNDAMENTALS

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

PIC 20A Number, Autoboxing, and Unboxing

C and C++ I. Spring 2014 Carola Wenk

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

G52CPP C++ Programming Lecture 16

Lab5. Wooseok Kim

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 12, 2011

Input And Output of C++

Programming Language Concepts: Lecture 2

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Java Fall 2018 Margaret Reid-Miller

Memory and C++ Pointers

Computational Expression

Getting started with Java

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

WARM UP LESSONS BARE BASICS

CSC Java Programming, Fall Java Data Types and Control Constructs

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

CSE 12 Spring 2016 Week One, Lecture Two

Introducing C++ to Java Programmers

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Object Reference and Memory Allocation. Questions:

15CS45 : OBJECT ORIENTED CONCEPTS

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

EE219 Object Oriented Programming I Repeat Solutions for 2005/2006

CS250 Final Review Questions

Transcription:

Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand that this presentation is just a feature overview. Only a fraction of Java s features are presented and we barely scratch the surface of the Java API. January 11, 2017 Brief Intro. to Java: Part 1 Brief Intro. to Java: Part 1 Java Overview History Programs written in Java are executed on a Java Virtual Machine (JVM) Java can be run any platform for which a JVM has been implemented Write once, run anywhere Java is compiled to an intermediate language called bytecode Bytecode is either interpreted, instruction by instruction by the JVM (slow), or sent through a Just-in-time compiler (JIT) which translates some of it into machine code just prior to execution (much faster!) Code is written in.java files; These are converted into.class files (bytecode) Designed in the early 90 s by Sun Microsystems (now part of Oracle) Motivations: Provide an alternative to C++ which reduced developer errors: Cleaner syntax (no pointers!) Garbage collection vs. manual memory management Pure object-oriented language (no global code or data) Execute on a wide range of devices Execute code directly on a web page Java Applets (deprecated); now Java Web Start Since 2008: Primary language for apps on Android Brief Intro. to Java: Part 1 Brief Intro. to Java: Part 1

Comparison with other Languages API Run time when using a JIT (from http:en.wikipedia.org/wiki/java_performance): 1-4 times slower than C/C++ Approximately the same as other JIT compiled languages such as C# Much faster than pure interpreted scripting languages such as Perl, Python, and Ruby Development time: Twice as fast as C++ (from Thinking in Java ) Slower than scripting languages (Hard to find an objective source for this information) The two components of the Java Platform are the JVM and the Java API. The API provides a massive set of classes for numerous applications: String processing Data structures Networking Handling media files (images, video, audio,...) Graphical User Interfaces (GUI): AWT, Swing, and JavaFX...etc Brief Intro. to Java: Part 1 Brief Intro. to Java: Part 1 Everything is an Object In Java, there is no code that exists outside of a class. Even the main method must appear within a class: public class HelloWorld { System. out. println("hello World"); Similar to C++ Classes are similarly defined (although no.h and.cpp separation) public has roughly the same meaning, although here it is used twice for both the class and the main method Different from C++ main exists within a class. There is a standard String class APointClassinJava public class Point { private double x, y ; / Constructor. / public Point( double x, double y ) { this. x = x ; this. y = y ; public double getx () { return x ; public void setx( double inx) { x = inx ;...

APointClassinC++ Testing the Point Class in Java class Point { private : double x, y ; public : / Constructor. / Point( double x, double y ) { this >x = x ; this >y = y ; ; ; double getx () { return x ; ; void setx( double inx) { x = inx ; ;... In C++ we can implement methods within the.h file or the.cpp file. In Java there is only the.java file. No including type stuff required ( yet) public class TestPoint { Objects are always constructed on heap Point p = new Point (4, 10); p is called a reference variable p. setx (5); String concatenation with + System. out. println("x: " + p. getx ()); Brief Intro. to Java: Part 1 Testing the Point Class in C++ Variables: Primitive Types #include <iostream> #include "Point.h" using namespace std ; int main( int argc, char argv) { Here we construct the object on the stack. Point p (4, 10); Calling a public method p. setx (5); Using the overloaded "<<" to concatenate. cout << " x : " << p. getx () << endl ; All variables are either primitive types or references. Primitive Types (most common in bold): byte, short, int, long, float, double, boolean, char. int: 32-bit integers from -2,147,483,648 to 2,147,483,647 double: 64-bitrationalnumbers 15 significant decimal digits; range of about ±1.7 10 308 boolean: Booleanvalueswrittenastrueorfalse char: 16-bitcharacters Brief Intro. to Java: Part 1 Brief Intro. to Java: Part 1

Usage of Primitives Similar to C++ public class Primitives1 { Declare and utilize as in C++. int i = 4; i++; System. out. println("i : " + i ); Error to use an uninitialized value! double x ; System. out. println("x : " + x ); Logic and comparison. boolean a = false ; boolean output = a && ( i < 100); Lazy! System. out. println("output: " + output ); Initialization of Primitive Data Members Primitive members initialized to 0 (false for booleans). public class Primitives2 { private int i, j ; Will be initialized to 0 private long k = 12; public Primitives2 () { j = 7; j was already initialized to 0. public void printout () { System. out. println("i : " + i + ", j : " + j + ", k: " + k ); Primitives2 p2 = new Primitives2 (); p2. printout (); A method call! Conversion between Primitive Types You can convert between types where the appropriate widening relationships exist: long > > > int > > > short > > > byte double > > > float public class Primitives3 { Maximum values for each type. byte b = 127; long l = 9223372036854775807l ; Suffix l l = b ; b = l 1; Can t do this If you know the loss of precision is acceptable you can cast between types. b = (byte) (l 1); Conversion Issues int: 10 digits max., long: 19 digits max. float 6-7 significant digits, double 15 significant digits public class Primitives4 { long l = 9223372036854775807l ; float f = 2.0F ; Use suffix F for floats Loss of precision! f = l ; System. out. println("l : " + l ); System. out. println("f : " + f ); System. out. println("l == f : " + (l == f )); The long is converted to a float in the comparison, so the result is true!

Control Flow Statements Reference Variables public class ControlFlow { public static void main ( String [ ] args ) { Loops, i f e l s e, s t a t e m e n t s, a l l a s C++ for ( int i=0; i <3; i++) System. out. println ( " i : " + i ) ; But t h e c o n d i t i o n s f o r t h e s e s t a t e m e n t s o n l y a c c e p t b o o l e a n s! int i = 3 ; w h i l e ( i ) { Can t do t h i s while ( i > 0 ) { System. out. println ( " i : " + i ) ; i ; Objects are referred to through reference variables, which are essentially pointers without the horrible syntax. public class References { public static void main ( String [ ] args ) { D e c l a r e a r e f e r e n c e t o a new P o i n t Point a = new Point ( 0, 0 ) ; A n o t h e r r e f e r e n c e p o i n t i n g a t t h e same o b j e c t Point b = a ; Where Variables Live and Garbage Collection Local variables, including both primitive types and references live on the stack. Objects (but not references) are allocated with new and live on the heap. There is no delete keyword! When there are no references to an object remaining, the object becomes available to the garbage collector (GC). The GC uses its own logic to determine when to re-claim unused memory. Therefore, you should not make any assumptions about when your object is deleted. There is no destructor in Java, but there is a finalize method that is used in unusual circumstances to de-allocate memory allocated using a non-standard mechanism (e.g. via C++). Brief Intro. to Java: Part 1 Brief Intro. to Java: Part 1 Garbage collection may occur when no references to an object remain. References can go away by going out of scope or by being explicitly set to null. (Aside: Uninitialized ref s are set to null). public class GarbageCollection { public static void main ( String [ ] args ) { Point a = new Point ( 0, 0 ) ; { Point b = a ; { Point c = b ; Now t h r e e r e f s t o o b j e c t Now two a = null ; Now j u s t one b. setx ( 4 2 ) ; System. out. println ( " a. getx ( ) : " + a. getx ( ) ) ; Brief Intro. to Java: Part 1 No r e f s t o o b j e c t. I t can be g a r b a g e c o l l e c t e d. ( But don t c o u n t on i t! )

Reference Equivalence vs. Object Equivalence If a comparison operator such as == is applied to a reference variable, it is applied to the reference, not the object. public class RefEquiv { Point p1 = new Point (2, 0); Point p2 = new Point (2, 0); Point alias1 = p1 ; p1 and alias1 refer to same object System. out. println("p1 == alias1 : " + (p1 == alias1 )) p1 and alias1 refer to different objects System. out. println("p1 == p2: " + (p1 == p2 )); Output : p1 == alias1 : true p1 == p2 : false Singly-Rooted Hierarchy The Java class hierarchy, including standard Java API classes and your classes, all inherit from the Object class. This provides several useful methods that can be applied to any object. public class ObjectExample { Point p = new Point (2, 0); System. out. println( p. tostring () ); Output : Point@6d06d69c Class pclass = p. getclass (); System. out. println( pclass. getname () ); Output : Point Arrays Arrays in Java: (1) They are actual objects and have a public length data member; (2) Arrays of primitives are automatically initialized; (3) Out-of-bounds access generates an exception. public class Arrays1 { Array declaration and initialization int [] array = new int [10]; Array access. Also, use of length for ( int i=0; i < array. length ; i++) assert array [ i ] == 0; Java arrays check their index! int i = array [10]; Exception thrown Creating an array of objects does not create the actual objects. public class Arrays2 { String [] array = new String [3]; No actual strings have been created! The for each construct for ( String s : array) System. out. println( s ); Prints null! for ( int i=0; i<array. length ; i++) array [ i ] = new String("string #" + i ); OR array [ i ] = " string #" + i ; Aggregate initialization is possible Point [] points = { new Point (1,1), new Point (2,2) ;

this keyword Two uses: (1) Refer to current object; (2) Call other constructor public class Rectangle { private int x, y, width, height ; public Rectangle( int x, int y, int w, int h ) { this. x = x ; this. y = y ; width = w ; this not needed here height = h ; public Rectangle () { this (0, 0, 0, 0); Unnecessary public Rectangle( int width, int height) { this (0, 0, width, height );