Programming. Syntax and Semantics

Similar documents
Introduction to Programming (Java) 2/12

Getting started with Java

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

CMPT 125: Lecture 3 Data and Expressions

Java Overview An introduction to the Java Programming Language

History Introduction to Java Characteristics of Java Data types

2 rd class Department of Programming. OOP with Java Programming

Full file at

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Introduction to Programming Using Java (98-388)

An overview of Java, Data types and variables

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

From C++ to Java. Duke CPS

Declaration and Memory

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Operators and Expressions

CS11 Java. Fall Lecture 1

B.V. Patel Institute of BMC & IT, UTU 2014

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

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

1007 Imperative Programming Part II

Chapter 1 Introduction to Computers, Programs, and Java

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Lecture 1: Overview of Java

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Introduction to Java

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Praktische Softwaretechnologie

CS 11 java track: lecture 1

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Programming Language Basics

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Java Notes. 10th ICSE. Saravanan Ganesh

Types and Expressions. Chapter 3

CHAPTER 1. Introduction to JAVA Programming

Course information. Petr Hnětynka 2/2 Zk/Z

Chapter 2: Data and Expressions

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 3: Operators, Expressions and Type Conversion

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

1 Shyam sir JAVA Notes

Section 2: Introduction to Java. Historical note

Java Programming Language Mr.Rungrote Phonkam

Operators. Java operators are classified into three categories:

Kickstart Intro to Java Part I

3. Java - Language Constructs I

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

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

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

PROGRAMMING FUNDAMENTALS

Chapter 1 Introduction to java

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Selected Java Topics

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

Chapter. Let's explore some other fundamental programming concepts

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

Introduction Basic elements of Java

JAVA Programming Fundamentals

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java: framework overview and in-the-small features

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

Object Oriented Programming with JAVA UNIT - I

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

Introduction to Java

Zheng-Liang Lu Java Programming 45 / 79

Special Topics: Programming Languages

Question. 1 Features of Java.

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CSC 1214: Object-Oriented Programming

Data Types, Literals, Operators

JAVA OPERATORS GENERAL

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

Assumptions. History

Values and Variables 1 / 30

Java Programming. Atul Prakash

Pace University. Fundamental Concepts of CS121 1

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java Fall 2018 Margaret Reid-Miller

Program Fundamentals

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

Index COPYRIGHTED MATERIAL

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

SECTION II: LANGUAGE BASICS

Language Fundamentals Summary

Transcription:

Programming For the next ten weeks you will learn basic programming principles There is much more to programming than knowing a programming language When programming you need to use a tool, in this case the tool will be a language In this course you will use java to explore programming You will use other languages to program 9/10/2003 Java 1 Syntax and Semantics When using a programming language you must understand both the syntax and the semantics of the language. Syntax refers to the rules you must follow to form valid statements in the language. The syntax of a language is like English grammar. Semantics describe the meaning of a statement in a language. 9/10/2003 Java 2 1

Language Translation The only language that a computers understands is machine language A program, written in a programming language other than machine language, must be translated into machine language in order for it to run on a computer Programs are typically either compiled or interpreted 9/10/2003 Java 3 Compiling When a program is compiled it is converted directly into machine language Sort of like having a book translated from English to German The program that does the translation is called a compiler Source Code Object Code Executable Code compile link 9/10/2003 Java 4 2

Interpreted Languages Another approach is to convert the program to machine language while it is running An interpreter translates and immediately executes a program Someone, who understands German, reads the book in German and translates while reading to English Source Code interpret Execute 9/10/2003 Java 5 What Is Java Java started as a programming language for embedded systems (toasters, microwave ovens, washers, etc.). needed to be portable. had to be reliable. The original language was called oak (rumor has it that Gosling has a large oak tree outside the window of his office). Marketing decided Java was a better name. 9/10/2003 Java 6 3

Sun s Slant According to Sun: Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language Java is a lot like C/C++ but there are a number of important differences 9/10/2003 Java 7 Program Structure A program in Java consists of one or more class definitions. One of these classes must define a method main(), which is where the program starts running // A Java Hello World Program public class HelloWorld { public static void main( String args[] ) { System.out.println( "Hello World" ); } } 9/10/2003 Java 8 4

How Is Java Different Java differs from other popular languages: It is interpreted Architecture neutral There are no C/C++ style pointers, only references Garbage collected Comes with a sophisticated class library Includes support for concurrency, networking, and graphics 9/10/2003 Java 9 Executing Java Programs Java Source Program emacs filename.java Java Compiler javac filename.java Java Class File java filename Win98/NT JVM MacOS JVM Solaris JVM 9/10/2003 Java 10 5

Java Environments There are lots of commercial Java programming environments. IBM s Visual Age. Visual J++. Semantic Café. many others (most of which cost money). Sun provides the JDK (Java development Kit) for free. 9/10/2003 Java 11 The JDK The JDK consists of the following: The Java development tools, including the compiler, debugger and the Java Interpreter. The Java class libraries organized as a collection of packages. A number of demonstration programs. Various supporting tools and components, including the source code of the classes in the library. Get it from http://www.java.sun.com. 9/10/2003 Java 12 6

Java Resources Java Home Page http://www.java.sun.com (http://www.javasoft.com) The Java Tutorial http://www.java.sun.com/docs/books/tutorial Java Developer Connection http://developer.java.sun.com The Swing Connection http://java.sun.com/products/jfc/tsc 9/10/2003 Java 13 Other Resources RIT Course Pages http://www.cs.rit.edu/~cs1 http://www.cs.rit.edu/~cs2 http://www.cs.rit.edu/~cs3 NT-EMACS http://www.cs.washington.edu/homes/voelker/ntemacs.html JDE http://sunsite.auc.dk/jde/ 9/10/2003 Java 14 7

Applications and Applets Java programs come in two forms: Applications. Applets. Applets typically are downloaded into a browser and are run by the Java Virtual Machine that is part of the browser. Usually are restricted as to what they can do. Applications are standalone programs that can do just about anything. 9/10/2003 Java 15 Basic Java Syntax The Java language will be described by working through its features: variable types and expressions. selection and iteration. classes. exceptions. Small sample programs will be provided to illustrate how each feature is used. 9/10/2003 Java 16 8

Applications and Applets Java programs come in two forms: Applications. Applets. Applets typically are downloaded into a browser and are run by the Java Virtual Machine that is part of the browser. Usually are restricted as to what they can do. Applications are standalone programs. 9/10/2003 Java 17 Things & Stuff Any program that you will write will manipulate things Numbers Strings Objects We need to be able to refer to these sorts of items in a program 9/10/2003 Java 18 9

Identifiers Identifiers are used in a programming language to refer to things You can think of an identifier as a shortcut to a memory location somewhere in the computer Declarations in a program introduce identifiers and the type of thing they will refer to All identifiers must be declared before they may be used 9/10/2003 Java 19 Rules Identifiers start with an alphabetic character can contain letters, digits, or _ are unlimited in length Examples Answer, total, last_total, relativeposition, gridelement Person, Place, Stack, Queue 9/10/2003 Java 20 10

Declaring Variables The basic syntax for declaring variables is: typename identifer; It is possible to declare two or more variables of the same type in a single declaration statement. 9/10/2003 Java 21 Categories of Variables There are two categories of variables: Variables of primitive type which directly contain a representation of a value of a primitive type. Variables of a reference type which hold a reference to an object or the value null (which is the null reference). All variables must be declared and initialized before being used. 9/10/2003 Java 22 11

Primitive Types The primitive types represent the basic, built-in types that are part of the Java language. Two basic categories: Boolean - boolean. Numeric. Integral -byte, short, int, long, char. Floating point - float, double. 9/10/2003 Java 23 Primitive Types Type Description boolean Has two values, true and false. byte 8-bit signed 2 s complement integers, range: -128 to 127 short 16-bit signed 2 s complement integers, range: -32768 to 32767 int 32-bit signed 2 s complement integers, range: -2147483648 to 2147483647 long 64-bit signed 2 s complement integers, range: - 9223372036854775808 to 9223372036854775807 char 16-bit unsigned values from 0 to 65535, representing Unicode characters float Single precision, 32-bit format IEEE 754 floating-point values, range: 1.40239846e-45 to 3.40282347e+38 double Double precision, 64-bit format IEEE 754 floating-point values, range: 4.9406564581246544e-324 to 1.79769313486231570e+308 There are special floating point values: positive infinity, negative infinity, and not a number (NaN). Note: these types are platform independent 9/10/2003 Java 24 12

Unicode An International Standard that defines the representation of characters from a wide range of alphabets. Unicode stores characters as 16-bit values providing 65,536 different characters. ASCII happens to be the first 127 characters in the Unicode standard. Java uses Unicode as opposed to ASCII. 9/10/2003 Java 25 Unicode Escapes Unicode escapes allow any character to be represented regardless of the editor being used A Unicode escape stands for a character and is represented using the \u escape sequence followed by the hexadecimal digits of the character code Examples: \u0343, \u2f4, \uabcd 9/10/2003 Java 26 13

Literals Type Integer Floating point Boolean Characters Strings Null Examples 0, 123, -456, 55665, 00, 0123, 0777, -045323, 0x0, 0x125, -0xffed, 0xfff Literals of type long (64-bit) are denoted by appending L or l to any integer literal. 1.2345, 1234.423, 0.1, -1.23, By default floating point literals are of type double. If the literal is suffixed with F or f it will be of type float. true, false a, A,!, \b, \f, \n, \r, \t, \\, \ This is a string, Hello World\n null 9/10/2003 Java 27 Assignment Declarations associates a type with an identifier Assignment associates a value with an identifier Assignment is represented by an = sign An identifier will always be on the left side of the equals sign The computer will place a copy of the thing on the right into the area named by the identifier on the left Assignment is not the same as algebraic equality 9/10/2003 Java 28 14

Operators Description Syntax unary postfix []. () ++ -- unary prefix ++ -- + - ~! creation and cast new ( type ) multiplicative * / % additive + - shift << >> >>> (unsigned right shift) relational < > >= <= instanceof equality ==!= and & xor ^ or boolean and && boolean or conditional?: assignment = += -= *= /= %= >>= <<= >>>= &= ^= = 9/10/2003 Java 29 Mixed Mode Expressions What happens if an expression contains two different types of numbers? int a = 4 + 5 *.56; In most cases Java will automatically convert the values in the expression so that it may be evaluated 9/10/2003 Java 30 15

Automatic Type Conversion Java provides a variety of automatic type conversions. The following conversions are supported: Widening primitive conversions. byte to short, int, long, float, or double. short to int, long, float, or double. int to long, float, or double. long to float or double. float to double. 9/10/2003 Java 31 Manual Type Conversion In some situations Java will not perform automatic conversions int x = 3.1456; In these cases you can force a conversion by specifying a cast int x = (int)3.1456; Here information is lost, but the assignment will take place 9/10/2003 Java 32 16

Reference Types Reference types are used to declare variables that will refer to objects The JDK provides a number of classes The String class allows us to declare, create, and manipulate strings Declaring a string is no different from declaring a primitive type: String name; 9/10/2003 Java 33 Creating Objects Before a reference to an object may be assigned to a variable, the object must be created Operator new is used to create new objects String name = new String(); String name = new String( Paul Tymann ); String name = Paul Tymann ; 9/10/2003 Java 34 17

References Variables refer to objects, they do not contain the object Several different variables may all refer to the same object If an object is not referred to by any variables, the object will eventually be destroyed 9/10/2003 Java 35 Methods A reference to an object can be used to invoke a method of the object The dot (.) operator specifies method invocation String name = Paul Tymann ; System.out.println( name.substring( 6 ) ); An attempt to invoke a method using a null reference is an error 9/10/2003 Java 36 18

What Methods? How do you know what methods are available for a given object? Look at the class definition Look at the documentation for the class The JDK provides documentation for its classes using a tool called JavaDoc 9/10/2003 Java 37 19