Computer Science A 15: 17/4. Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Computer Science A 14: 31/3. Inheritence You can extend a class or change behaviour. Inheritance is a method to reuse code. Concept. Extending classes.
The Java Programming Language
Principles of Object-Oriented Software Development The language Java.
Java.1 CSE4100 The Java Language Grammar
Outline Java program structure Basic program elements
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
JAVA PROGRAMMING PART II.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java: Chapter 1 Computer Systems Computer Programming II.
Java Language and SW Dev’t
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2: Java Fundamentals
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Object Oriented Programming Lecture 2: BallWorld.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Information and Computer Sciences University of Hawaii, Manoa
Working with Java.
The Java Language Grammar sun. com/docs/books/jls/index
Lecture 2: Data Types, Variables, Operators, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to Java Programming
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
An overview of Java, Data types and variables
Instructor: Alexander Stoytchev
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
JavaScript Reserved Words
Instructor: Alexander Stoytchev
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
CSE 142, Spring 2012 Building Java Programs Chapter 1
Instructor: Alexander Stoytchev
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
CSE 142, Winter 2014 Building Java Programs Chapter 1
Agenda Types and identifiers Practice Assignment Keywords in Java
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Computer Science A 15: 17/4

Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language can be tricky

Lexical rules Lexical analysis: split the text into words Rule: split at the longest legal sequence Spaces are almost always ignored You can have spaces but not line breaks in text strings x+++++x or x+++++x intx; intintx;

A Java Program public class A { public static void main(String[ ] args) { System.out.println(”Hello”); } or publicclassA{publicstaticvoid main(String[ ]args){System.out.println(”Hello”);} }

Java Language Specification A detailed definition of the the programming language. It describes all language constructions in java, syntax, semantics og pragmatics In the following I present an extract of approximately 2/3 of Java – the parts you are likely to use. I present the syntax in EBNF

EBNF Extended Backus Naur Form Meta language used to describe programming languages { text } repeat text 0 or more times [ text ] include text 0 or 1 time ( text1 | text 2 ) either use text1 or text2 Use nonterminals to allow recursive descriptions: Exp is a nonterminal are describe balanced expressions Exp : “[“ Exp “]” | “[” “]”

Identifiers and numbers Identifier : Letter { digit | letter | ”_” } VarName : Identifier TypeName : Name ClassName : Name Number : digit { digit } [”l” | ”L”] Char : ”’” character ”’” String: ””” {character } ””” character: ”\r” | ”\n” | ”\b” | ”\f” | ”\t” | ”\”” | ”\’” | ”\\” | ….

Simple expressions Primary: ”(” Expression ”)” ”this” [ Arguments ] ”super” [ Arguments ] ”new” Identifier ”[” Expression ”]” { ”[” Expression ”]” } ”new” Identifier Arguments Identifier { ”.” Identifier } [ Arguments ] Number String Char Arguments: ”(” [ Expression { ”,” Expression } ] ”)”

Expressions Expression: Expression1 [AssignmentOperator Expression1] AssignmentOperator: ”=” | ”+=” | ”-=” | ”*=” | ”/=” | ”&=” | ”|=” | ”^=” | ”%=” | ” >=” | ”>>>=” Expression1: Expression2 [ ”?” Expression ”:” Expression1 ] Expression2 : Expression3 { Infixop Expression3 } Expression3 ”instanceof” Type Infixop: ”||” | ”&&” | ”|” | ”^” | ”&” | ”==” | ”!=” | ” ” | ” =” | ” >” | ”>>>” | ”+” | ”-” | ”*” | ”/” | ”%” Expression3: PrefixOp Expression3 ”(“ Type “)” Expression3 Primary {Selector } {PostfixOp } Selector: ”.” Identifier | ”[” Expression ”]” PrefixOp: ”++” | ”--” | ”!” | ”~” | ”+” | ”-” PostfixOp: ”++” | ”--”

Types, etc. Type: Identifier { “.” Identifier } BracketsOpt BasicType BasicType: ”byte” | ”short” | ”char” | ”int” | ”long” | ”float” | ”double” | ”boolean” Catches: CatchClause { CatchClause } CatchClause: ”catch” ”(” FormalParameter ”)” Block ForInit: Expression { ”,” Expression } [”final”] Type VariableDeclarator { ”,” VariableDeclarator } ForUpdate: Expression { ”,” Expression } SwitchBlockStatementGroups: { SwitchLabel {Statement } } SwitchLabel: ”case” Expression ”:” | ”default” ”:”

Statements Statement: Block ”if” ”(”Expression ”)” Statement [“else” Statement ] ”for” ”(” ForInit ”;” [Expression] ”;” ForUpdate ”)” Statement ”for” ”(” Type Identifier ”:” Expression ”)” Statement ”while” ”(”Expression ”)” Statement ”do” Statement ”while” ”(”Expression ”)” ”;” ”try” Block ( Catches | [Catches ] ”finally” Block ) ”switch” ”(”Expression ”)”{ SwitchBlockStatementGroups } ”synchronized” ”(”Expression ”)” Block ”return” [Expression ] ”;” ”throw” Expression ”;” ”break” [Identifier ] ”;” ”continue” [Identifier ] ”;” Expression ”;” Identifier ”:” Statement

Methods consist of statements Block: ”{” { BlockStatement } ”}” BlockStatement : [ ”final” ] Type VariableDeclarator { ”,” VariableDeclarator } ”;” [Identifier ”:”] Statement VariableDeclarator: Identifier BracketsOpt [ ”=” VariableInitializer ] BracketsOpt: { ”[” ”]” } FormalParameters: ”(” [ FormalParameter { ”,” FormalParameter } ] ”)” FormalParameter: Type Identifier BracketsOpt

A class consists of methods ClassBody: ”{” { ClassBodyDeclaration } ”}” ClassBodyDeclaration: ”;” [ ”static” ] Block {Modifier } MemberDecl MemberDecl: [ ”void” | Type ] Identifier FormalParameters BracketsOpt [ ”throws” IdentifierList ] ( Block | ”;” ) Type Identifier BracketsOpt [ ”=” VariableInitializer ] VariableInitializer: ”{” [VariableInitializer { ”,” VariableInitializer } [”,”] ] ”}” Expression

A program consists of classes CompilationUnit: [ ”package” QualifiedIdentifier ”;” ] {ImportDeclaration } {ClassDeclaration } ImportDeclaration: ”import” Identifier { ”.” Identifier } [ ”.*” ] ”;” ClassDeclaration: {Modifier } ”class” Identifier [ ”extends” Type ] [ ”implements” TypeList ] ClassBody Modifier: ”public” | ”protected” | ”private” | ”static” | ”abstract” | ”final” | ”native” | ”synchronized” | ”transient” | ”volatile” | ”strictfp”

Syntax tree Expression: x x expression expression1 expression2 expression3 infixop ”+””+” expression3primaryprefixoppostfixop ”++”identifierprimary”++” identifier ”x””x” ”x””x”

Example class Point{ int x; int y; Point(int x,int y){ this.x=x; this.y=y; } public String toString(){ return "("+x+","+y+")"; } } class Point3D extends Point{ int z; Point3D(int x,int y,int z){ super(x,y); this.z=z; } public String toString(){ return "("+x+","+y+","+z+")"; } }

Example Point a=new Point(2,4); System.out.println(a.toString()); Point3D b=new Point3D(7,9,13); System.out.println(b.toString()); A Point3D ”is-a” Point object and may be used as a Point object

Basics: class A{ int f(){return 2;} } class B extends A{ int f(){return 3;} }.. A a1=new A(); int i=a1.f(); // ok, i=2 A a2=new B(); int i=a2.f(); // ok, i=3 B b1=new A(); // not ok B b2=new B(); int i=b2.f(); // ok, i=3

Cast an instanceof class A{.. } class B extends A{..void g().. } A a= new B(); // ok a.g(); // not ok If(a instanceof B){ B b= (B) a; // cast b.g(); // ok }

Access control Java has four levels of controlling access to fields, methods, and classes: public access –Can be accessed by methods of all classes private access –Can be accessed only by the methods of their own class protected access –Access from subclasses package access –The default, when no access modifier is given –Can be accessed by all classes in the same package –Good default for classes, but extremely unfortunate for field

Subtyping and generics Tst1 t1=new Tst2(); t1.f(1); t1.f(1.0); // Double 1.0 Double 1.0 Tst2 t2=new Tst2(); t2.f(1); t2.f(1.0); // Int 1.0 Double 1.0 class Tst1{ void f(double d){System.out.println("double "+d);} } class Tst2 extends Tst1{ void f(double d){System.out.println("Double "+d);} void f(int i){System.out.println("Int "+i);} }

Avoid finally int i=0; try{ i=Integer.parseInt( JOptionPane.showInputDialog("input")); }catch(NumberFormatException e){ System.out.println("bad"); i=Integer.parseInt( JOptionPane.showInputDialog("input")); } finally{ System.out.println("typed: "+i); } System.out.println("typed: "+i);

More features Arrays and ArrayList Inheritance Methods, recursion Nested loops Libraries: util, io, awt, swing