Programming Language Concepts: Lecture 2

Similar documents
Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 1

CS 11 java track: lecture 1

Getting started with Java

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

An overview of Java, Data types and variables

Introduction to Programming Using Java (98-388)

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Kickstart Intro to Java Part I

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

CS 231 Data Structures and Algorithms, Fall 2016

Pace University. Fundamental Concepts of CS121 1

Java Bytecode (binary file)

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Lecture 2, September 4

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

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

Brief Summary of Java

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

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

Lecture 1: Overview of Java

More on Java. Object-Oriented Programming

CMSC131. Introduction to your Introduction to Java. Why Java?

Lecture 02, Fall 2018 Friday September 7

Introduction to Java

Objects and Classes. Objects and Classes

Values and Variables 1 / 30

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Index COPYRIGHTED MATERIAL

Java Identifiers, Data Types & Variables

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

1.1 Your First Program

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

Getting Started with Java. Atul Prakash

Array. Prepared By - Rifat Shahriyar

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Software and Programming 1

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Short Notes of CS201

CSC Java Programming, Fall Java Data Types and Control Constructs

Introduction Basic elements of Java

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

Assumptions. History

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

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

Crash Course Review Only. Please use online Jasmit Singh 2

CS201 - Introduction to Programming Glossary By

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Handout 7. Defining Classes part 1. Instance variables and instance methods.

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

BM214E Object Oriented Programming Lecture 8

Introduction to Java

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

COMP 250 Winter 2011 Reading: Java background January 5, 2011

1.1 Your First Program

COMP-202: Foundations of Programming

Software and Programming 1

Modern Programming Languages. Lecture Java Programming Language. An Introduction

THE CONCEPT OF OBJECT

Program Fundamentals

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Lecture 2: Intro to Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CHAPTER 7 OBJECTS AND CLASSES

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

1.1 Your First Program

377 Student Guide to C++

CS 61C: Great Ideas in Computer Architecture Introduction to C

CompSci 125 Lecture 02

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

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

COP 3330 Final Exam Review

Packages & Random and Math Classes

A Foundation for Programming

About this exam review

CS11 Java. Fall Lecture 1

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

Java Fundamentals (II)

Computer Programming, I. Laboratory Manual. Final Exam Solution

Lecture Set 2: Starting Java

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Object Oriented Programming in C#

Compiling Techniques

JAVA: A Primer. By: Amrita Rajagopal

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

Lecture Set 2: Starting Java

Programming Language Concepts: Lecture 10

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Java is an objet-oriented programming language providing features that support

Computer Science II (20082) Week 1: Review and Inheritance

Transcription:

Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2011 PLC 2011, Lecture 2, 6 January 2011

Classes and objects A class is a template for a datatype Instance variables or fields Functions, or methods, to operate on data An object is an instance of a class Private copy of instance variables Methods implicitly attached to objects e.g., s.pop() Ideally an object is a black box whose internals are manipulated through a well defined interface d :: Date getdate setdate

Public vs private Internal details of a class should be hidden...... but the default is to make them visible! To restore data integrity, classify internals as public or private Private variables are visible to other objects of the same class e.g. Check if two objects have same contents Methods may also be private e.g. Private method to extend a stack if it becomes full

Static components Exist without instantiating a class Static methods Like normal functions in other languages Required to start off the computation Also useful for library functions Static methods should not refer to non-static fields Static fields Global constants class Math { public static double PI = 3.1415927; public static double E = 2.7182818; public static double sin(double x) {...... Constants local to a class private static e.g., base interest rate for bank deposits

Constants class Math { public static double PI = 3.1415927;... User can modify PI!

Constants class Math { public static double PI = 3.1415927;... User can modify PI! Declare PI to be final class Math { public static final double PI = 3.1415927;... What could it mean for a function to be final?

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class java xyz interprets and runs bytecode in class file

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); Store in helloworld.java javac helloworld.java to compile to bytecode Creates helloworld.class java helloworld to execute Note: javac requires extension.java java should not be provided.class javac automatically follows dependencies and compiles all classes required

Constructors Can we initialize an object? Analogue of int i = 10; Special methods called constructors Invoked once, when an object is created Usually have the same name as the class class Date{ private int day, month, year; public Date(int d, int m, int y){ day = d; month = m; year = y; Date d = new Date(27,1,2009);

Constructors... Can have more than one constructor public Date(int d, int m){ day = d; month = m; year = 2009; Invoke appropriate constructor by context Date d1 = new Date(27,1,2008); Date d2 = new Date(27,1); Two functions can have the same name, different signatures Overloading

Constructors... A later constructor can call an earlier one class Date{ private int day, month, year; public Date(int d, int m, int y){ day = d; month = m; year = y; public Date(int d, int m){ this(d,m,2009); this refers to the object to which method is associated Objects have a notion of self!

Constructors... Can reverse the order class Date{ private int day, month, year; public Date(int d, int m){ day = d; month = m; year = 2009; public Date(int d, int m, int y){ this(d,m); year = y; Call to other constructor must be first instruction

Constructors... If no constructors are defined, default constructor initializes methods to default values Date d = new Date(); Note the brackets after Date Default constructor is available only if no constructors are defined

Back to Java Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class java xyz interprets and runs bytecode in class file

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean Size of int etc are fixed, operational semantics is wrt JVM char is 2 bytes, Unicode, but otherwise behaves as usual char c = a ; c = X ; if (c!= ) {... Explicit boolean type boolean b, c = false; b = true; b = (i == 7);

Java syntax... Expressions are similar to C x = 7 returns value 7 flag = (x = 0) is caught as a syntax error Compound statements are familiar if (condition)... else... while (condition)... do... while (condition) for (i = 0; i < n; i++)... No goto, but labelled break and continue outer_loop: // this is a loop label for (i = 0; i < n; i++){ for (j = 0; j < n; j++){ if (a[i][j] == k){ break outer_loop; // exits the outer for loop

Java syntax, strings String is a built in class String s,t; String constants enclosed in double quotes String s = "Hello", t = "world"; Strings are not arrays of characters Cannot write s[3] = p ; s[4] =! ; Instead, invoke method substring in class String s = s.substring(0,3) + "p!"; + is overloaded for string concatenation If we change a String, we get a new object After the update, s points to a new String Java does automatic garbage collection

Java syntax, arrays Arrays are also objects Typical declaration int[] a; a = new int[100]; Can write int a[] instead of int[] a Can combine as int[] a = new int[100]; Aside: Why the seemingly redundant reference to int in new? Can create new arrays at run time a.length gives size of a Note, for String, it is a method s.length()!

Java arrays,... public class arraycheck{ public static void main(string[] argv){ int[] a; int i, n; n = 10; a = new int[n]; for (i = 0; i < n; i++){ a[i] = i; n = 20; a = new int[n]; for (i = 0; i < n; i++){ a[i] = -i;

Java syntax class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); args is an array of String argv in C Don t explicitly need the number of arguments (argc in C) Use args.length to get this

More about private fields Should private fields be visible to other objects of the same type? How do we check if two objects are equal? Date s,t;.. if (s == t) {... ; == checks whether s and t are the same object Date s = new Date(27,1,2009); s == t Date t = s; Date s = new Date(27,1,2009); s == t Date t = new Date(27,1,2009); We want to check if the contents of s and t are the same

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year) Invoke as s.isequal(t) (or t.isequal(s)) The object that executes isequal needs access to private information of the other object...... but both are objects of the same type, so internal structure is not a secret! Note the use of this to refer to the parent object this can be omitted if context is clear