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

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

CS 376b Computer Vision

CE221 Programming in C++ Part 1 Introduction

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

Lec 3. Compilers, Debugging, Hello World, and Variables

Assumptions. History

C10: Garbage Collection and Constructors

Object-oriented Programming and Introduction to C++

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

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

AN OVERVIEW OF C++ 1

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

Chapter 2. Procedural Programming

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

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

Lecture 2, September 4

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

Selected Questions from by Nageshwara Rao

Homework 4. Any questions?

Kickstart Intro to Java Part I

Polymorphism Part 1 1

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

Programming Language Concepts: Lecture 2

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

HW3a solution. L1 implies there must be an f1 in Base L2 implies there must be an f2 in Base. So we know there is an f1 and f2 in Base

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

1 Shyam sir JAVA Notes

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

Lecture 15a Persistent Memory & Shared Pointers

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Programmazione. Prof. Marco Bertini

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

An overview of Java, Data types and variables

ECE Fall 20l2, Second Exam

C11: Garbage Collection and Constructors

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

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)

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

PROGRAMMING IN C++ CVIČENÍ

STRUCTURING OF PROGRAM

Section 2.2 Your First Program in Java: Printing a Line of Text

CA341 - Comparative Programming Languages

Computer Science II Lecture 1 Introduction and Background

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

Fast Introduction to Object Oriented Programming and C++

EMBEDDED SYSTEMS PROGRAMMING Language Basics

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

Introduction to Java Written by John Bell for CS 342, Spring 2018

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Lecture 1: Overview of Java

Polymorphism CSCI 201 Principles of Software Development

C++ Lab 03 - C++ Functions

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

2. The object-oriented paradigm!

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

377 Student Guide to C++

Introduction to Java

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Character Stream : It provides a convenient means for handling input and output of characters.

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Packages & Random and Math Classes

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Computer Programming. Dr. Fahad Computer Science Deptt.

( &% class MyClass { }

Lab 1: First Steps in C++ - Eclipse

CS

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

From C++ to Java. Duke CPS

Separate Compilation Model

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

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

A Tour of the C++ Programming Language

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

UEE1303(1070) S 12 Object-Oriented Programming in C++

CSE 303: Concepts and Tools for Software Development

Programming Language Concepts: Lecture 2

CSE 12 Spring 2018 Week One, Lecture Two

COMP 2355 Introduction to Systems Programming

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Functions and Recursion

Fundamentals of Programming. By Budditha Hettige

Program Organization and Comments

Exceptions, Case Study-Exception handling in C++.

C++ Important Questions with Answers

BM214E Object Oriented Programming Lecture 8

PIC 10A Objects/Classes

GCC : From 2.95 to 3.2

Functions and Methods. Questions:

Answer1. Features of Java

Preview from Notesale.co.uk Page 3 of 36

Certified Core Java Developer VS-1036

15: Polymorphism & Virtual Functions

Transcription:

Java vs C++ 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory

Outline 1 2 3

Outline 1 2 3

Outline 1 2 3

Tabular comparizon C++ Java Paradigm Procedural/Object-oriented Object-oriented is highly recommended Compilation/Running Compiled to machine code that can run on specific system Compiled to byte code that can be run on system with JVM Standard libraries Limited to the very basic functionality (e.g. Grows with each release STL supports regular expression only since C++11) Multiple inheritance Full support, including virtual inheritance One can achieve similar result using interfaces Run-time object information Only type names are available Full meta-information support, including dynamic code invocation Type instances passing by value/by reference/by pointer instances of primitive types by value, rest by reference (that can be perceived as pointers) Memory management explicit one has to force system to free disposing memory is managed by, so memory called, garbage collector Overloading operators most of operators can be overloaded does not allow to overload operators Logic program hierarchy namespaces package = path to the source file, filename = inner type name Inline abstract classes/interfaces not possible allowed imple- mentation

Hello world in Java Java source code 1 package hw; 2 3 import java.lang.system; 4 5 public class Hello { 6 public static void main(string args[]) { 7 System.out.println("Hello world"); 8 } 9 } C++ source code 1 #include <iostream> 2 3 using std::cout; 4 using std::endl; 5 6 int main(int argc, char **argv) { 7 cout << "Hello world" << endl; 8 } Compiling 1 javac path/to/file/filename.java Running 1 java path.to.file.filename

Creating jar file Compiled classes by javac are stored as files with extension class in a file system. While application grows there are plenty of such class files. To make the application transferable one have to use some other mechanisms that merge the number of files in a single executable application. To handle this problem Java applications make use of jar files. Command creating jar 1 jar cfe path/to/jarfile/jarfilename.jar path.to.mainclass.mainclass classfile[,...] Running jar 1 java -jar jarfile.jar

Tasks Complete the tasks, answer questions 1 Create hello world application in Java and create executable executable jar file.

A few guidelines Access modifier (without colon) before every class member declaration Inheritance of classes using extends keyword Implementing interface using implements keyword To declare constant values use final keyword To use some class from outside of the package use import keyword Virtual methods don t have to be additionally specified every method is virtual and can be overridden To declare abstract method use abstract keyword Use dot to access members of the object instance Use dot to access the package elements Use dot to access a static class members

Example car/car.java 1 package cars; 2 3 import vehicles.vehicle; 4 import vehicles.istartenginable; 5 6 public class Car extends Vehicle implements IStartEnginable { 7 public int cylinders = 10; 8 9 public void startengine() { 10 System.out.print("B"); 11 for (int i = 0; i < this.cylinders; i++) { 12 System.out.print("R"); 13 } 14 System.out.println(); 15 } 16 17 public static void main(string[] args) { 18 Car car = new Car(); 19 car.startengine(); 20 } 21 }

Tasks Complete the tasks, answer questions 1 Use a class hierarchy from the first presentation (car.h/cpp, volkswagen.h/cpp, hyundai.h/cpp, main2.cpp) and rewrite its functionality to Java language. Remember to create a jar file.