Basic Programming Language Syntax

Similar documents
Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 1. Introduction

Programming with Java

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

2 rd class Department of Programming. OOP with Java Programming

First Java Program - Output to the Screen

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

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

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Lab # 2. For today s lab:

CS112 Lecture: Primitive Types, Operators, Strings

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

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Full file at

COMP 202 Java in one week

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

C: How to Program. Week /Mar/05

Course Outline. Introduction to java

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

CS112 Lecture: Working with Numbers

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Fundamentals of Programming. By Budditha Hettige

Chapter 2: Programming Concepts

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Basics of Java Programming

Pace University. Fundamental Concepts of CS121 1

COMP 202 Java in one week

CSI Lab 02. Tuesday, January 21st

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Some Computer Preliminaries

Elementary Programming

IT 374 C# and Applications/ IT695 C# Data Structures

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

ICOM 4015: Advanced Programming

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

Chapter 2 - Introduction to C Programming

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Lesson 1: Writing Your First JavaScript

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Java Bytecode (binary file)

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Introduction to JAVA

An overview of Java, Data types and variables

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Programming for Engineers Introduction to C

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to Software Development (ISD) David Weston and Igor Razgon

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Software and Programming 1

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Perl Basics. Structure, Style, and Documentation

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CPS122 Lecture: From Python to Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Software Development 2

Chapter 17. Fundamental Concepts Expressed in JavaScript

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Introduction to Java Applications; Input/Output and Operators

CS11 Java. Fall Lecture 1

Chapter 2: Data and Expressions

Programming Language Concepts: Lecture 2

CompSci 125 Lecture 02

St. Edmund Preparatory High School Brooklyn, NY

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CMPT 125: Lecture 3 Data and Expressions

Chapter 2 Author Notes

Week - 01 Lecture - 04 Downloading and installing Python

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

A variable is a name for a location in memory A variable must be declared

Transcription:

Java Created in 1990 by Sun Microsystems. Free compiler from Sun, commercial from many vendors. We use free (Sun) Java on UNIX. Compiling and Interpreting...are processes of translating a high-level programming language to a lower-level language. Example: Java source-code to Byte code (or, Byte code to machine language). The implementation process (Java) 1. programmer types source code 2. programmer starts compiler 3. Java compiler translates source code to byte code 4. programmer starts Java runtime environment (JRE) 5. JRE translates byte code to machine code and executes immediately 6. program runs 7. programmers finds errors and starts from the beginning Example: Basic Programming Language Syntax Programming languages have very strict rules (unlike English or other human languages). The text book has a lot of information on this topic. Java source code (*.java-files) must adhere to such rules. We introduce a few syntax rules for Java (compare with textbook listing 1.1 on page 30). comments: are lines in the source code ignored by the compiler. The first two characters of a comment line must be //

identifiers: are strings that are use for naming program objects in your code. So far, the only use for an identifier we know is the name of the program (class). In the textbook listing, Lincoln is an identifier. naming conventions: identifiers must adhere to certain rules as well. The basic rules are: the first character of an identifier must be a letter (a...z,a...z) or an underscore (_) or a dollar sign ($), for the remaining characters digits (0...9) are allowed as well. The textbook contains a syntax diagram visualizing this concept (page 35). reserved words: are strings that the compiler uses, and which therefore cannot be used as identifiers (e.g., public, class). A list of Java reserved words is on page 35. white space: It is possible (and recommended) to nicely format source code through generous use of space characters and/or the breaking down of lines. Examples for well and badly formatted code (which is all the same to the compiler though) are in the textbook, Listings 1.1. through 1.3. basic program structure: every Java program (technically, every class file) at this point must begin with the words public class, followed by an identifier (the class name). The next line in all our simple programs for the next few weeks should be public static void main (String[] args). Compare with the textbook. parentheses (), brackets [], and braces {} are used for different purposes in programs. For now, we only need braces {}, to structure our code. After the ``main'' line (above), the compiler expects a pair of braces, which enclose your entire program. Further pairs of braces will be necessary sometimes, details will follow. Each opening brace must have a corresponding closing brace. Forgetting those is one of the most common programming errors. Syntax and Semantics A computer executes a program by following the programmer's instructions PRECISELY. A computer error is (almost) always a programmer's error. Computers do not make errors calculating your bank account balance. Programmers make errors describing the calculation to the computer. Computers do exactly what they are TOLD, not what the programmers WANTS. If programmer makes any mistake, computer usually produces an error. Three classes of errors: Syntax Error. Programmer breaks the basic rules of the programming language. compiler complains during compilation Runtime Error. Programmer overlooks some condition in the execution of a program (later). JRE complains during program execution

Logical Error. Computer does not complain, but program does something different from what the programmer WANTED. Elementary Programming in Java Printing to the Standard Output Window The standard output window is the window in which you type your commands (such as javac Xyz.java or java Xyz). The mode of communication in that window is called command line interaction since you type commands on one line. The computer responds with text output in the same way. The concept of a command line is very old, and in fact predates monitors (screens). Command lines were originally used with tele-typewriters (which are like electric typewriters with a computer connection). Therefore, the computer can only print one character at a time, from left to right, and when a line is full (typically a line has 80 characters), the computer will continue printing on the next line. The position where the computer is going to print next (on a typewriter that would be the position of the print head) is called the cursor position. The cursor is the (possibly blinking) line showing where a character will appear on the screen if you type one. For the next few weeks, any input or output, that is your communication with a running Java-program, will occur in the command line fashion. We will first discuss output, that is communication from the computer program to you. The First Programming Construct: println The simplest thing you can make a Java-program do is print a predefined text to the screen. This is accomplished as follows: System.out.println("Hello World."); The text you want to print must be enclosed in double quotes. The remainder of the statement must be typed exactly as above, including the semicolon at the end. A piece of text enclosed in quotes is called a String and consists of printable characters. The println statement can print strings but also numbers and results of arithmetic expressions. For example: System.out.println(3+4*5); which would print 23 because Java will observe elementary rules of arithmetics. The plus-character has two different meanings in Java: addition of numbers, and concatenation of strings. For example:

will print 7 and 34, respectively. System.out.println(3+4); System.out.println("3"+"4"); A small but important point is that the decision which meaning the plus-character has is made by the left operand, i.e., the symbol to the left of the +. That is, is a valid statement and will print 34. System.out.println("3"+4); Grouping elements with parentheses will change the order of computation (usually leftto-right) and also the interpretation of results. This is important in expressions that ``add'' more than two things. For example, the result of (3+4) is always the number 7, whereas the result of 3+4 could be something else, as in the following example: System.out.println("Three plus four is "+(3+4)); System.out.println("Three next to four is "+3+4); which would print Three plus four is 7 and Three next to four is 34, respectively. The latter statement is equivalent to System.out.println(("Three next to four is "+3)+4); because of the left-to-right evaluation in Java. One last minor point about strings is the impossibility to include a quote (") in a string, since strings are enclosed in quotes. As a workaround, Java uses the escape character (the backslash), used as follows: System.out.println("My name is John \"Buster\" Smith"); which would print My name is John "Buster" Smith. If you ever need to print a backslash, use two consecutive backslashes, as in "\\". The Second Programming Construct: print A variation of the println-statement is the print statement, which works exactly like println, with the exception that the cursor is not advanced to the next line at the end of the statement. For example: System.out.print("Ja"); System.out.print("va");

would print Java. If you use the print-statement, you should always use at least one println statement after the last print-statement (why?). Objects Object-Oriented Thinking Java is a so-called Object-Oriented Programming Language. The other well-known such language is C, but most current programming languages have at least some objectoriented concepts. There is no good definition for object, since it is a very general concept. We will explain objects through their properties. Objects have two major properties: 1. Objects can execute ``orders'' which are conveyed to them through ``messages''. 2. Objects can ``remember'' information about themselves. In the appropriate terminology: 1. Objects can execute methods which are invoked through method calls. 2. Objects can store information about themselves in the form of variables. For example, a cell in an Excel spreadsheet could be implemented (and most likely is implemented) as an object. Such an object could remember its content, color, font, font size, background color, number format etc., and display its content in the correct format when ordered to do so. That way, when changing the properties of a cell in Excel, one actually updates the variables in the cell-object, and when the spreadsheet is being recomputed by the computer (i.e., every time you change a number or a formula in the sheet), the cell does its share of the work by displaying its own content on the screen. One way of looking at objects is as independent agents that manipulate their own data when instructed to do so, and sometimes return the results of such manipulation in some form. The advantages of objects (over older programming techniques) are many and will become clearer throughout the course. Some initial thoughts are: 1. objects can be written once, and then used many times, in the same program or in other programs 2. objects are usually self-contained and can be developed independently from one another 3. objects can store information about themselves, and use such information at the appropriate time

4. objects can act as independent entities in a program, and even run in parallel (at the same time) with other objects on a single CPU. Some of these features also exist in non-object-oriented systems, but object-oriented systems have all these advantages at the same time. Note: a common abbreviation for ``object-oriented'' is ``OO'', such we can say ``Java is an OO-language''. Classes and Instances Many objects in a program are similar. In particular, the methods, i.e., the functionalities of many objects are equal. For example, all cells in a spreadsheet should be able to perform the same tasks. Therefore, it makes sense to write a ``program'' for only one cell in the spreadsheet, and simply ``load the program into memory'' as many times as the spreadsheet has cells. The program that one would write to describe the capabilities of an object is called a class. Whenever such a class is loaded into memory to become an active piece of a program, that copy in memory is called an instance. The process of loading a copy of a class into memory is called instantiation. Example: In our (slightly silly) example from the lecture, we had a number of assistant cooks who were taught the exact same skill set in cooking school. So each cook had the same skills, but still the cooks were not identical. For example, they have different social security numbers (object identifiers), and they stand in different places in the kitchen (memory locations). Also, each cook could remember different things (and do different things) from each other cook at any one time. So the ``assistant cook class'' is described by the common skill set of all these cooks (methods), plus the kinds of things they could possibly remember (variables). Each individual cook is an instance of the ``assistant cook class''. The process of instantiation would correspond to the hiring of a new assistant cook straight out of cooking school. Since many people are not sure about the exact meaning of the word object, I will try to avoid referring to objects in class and instead exclusively use the terms class and instance in the meanings defined above. Summary Classes are templates for meaningful units of the program you are writing. You can instantiate a class one or many times, which means loading it into memory and start using it. Each time you do that, you get an instance. Classes have methods, which are sets of instructions to perform certain tasks, and variables which are places for the temporary storage of information.

Classes interact by sending messages to each other. Most such messages are transmitted in the form of method calls, which is a concept similar to that of subroutine calls of other programming languages, but is more flexible and powerful.