20-Jun-15 Additional Java Syntax. 2 Odd corners We have already covered all of the commonly used Java syntax Some Java features are seldom used, because:

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Access to Names Namespaces, Scopes, Access privileges.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
28-Jun-15 Additional Java Syntax. 2 Odd corners We have already covered all of the commonly used Java syntax Some Java features are seldom used, because:
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:
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Copyright Curt Hill Variables What are they? Why do we need them?
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter 4 Assignment Statement
Week 3 - Friday CS222.
Lecture 2: Data Types, Variables, Operators, and Expressions
CSE 190D, Winter 2013 Building Java Programs Chapter 1
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Starting JavaProgramming
null, true, and false are also reserved.
Additional Java Syntax
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Additional Java Syntax
Namespaces, Scopes, Access privileges
C Programming Getting started Variables Basic C operators Conditionals
Additional Java Syntax
Focus of the Course Object-Oriented Software Development
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

20-Jun-15 Additional Java Syntax

2 Odd corners We have already covered all of the commonly used Java syntax Some Java features are seldom used, because: They are needed in only a few specialized situations, or It’s just as easy to do without them, or Few people know about them You should be at least aware of these features, because: You may encounter them in someone else’s code They may do something you will need someday There are also a few useful features that we just didn’t get around to, or didn’t discuss in sufficient detail

3 Reserved words A reserved word, or keyword, is one that has a special meaning, and you may not use it as a name Examples: if, while, class, package There are also important keywords that we have not talked about: native, strictfp, transient, volatile And there are keywords that are not used (but you still can’t use them as names): const, goto

4 Unused keywords const is used in some languages to declare constants Java has this keyword, but used final instead A final variable does not have to be given a value when it is assigned, but it can be given a value only once: final int lesser; if (x < y) lesser = x; else lesser = y; // legal! The keyword final is also used to indicate that: A class cannot be subclassed A method cannot be overridden It does not indicate that a variable cannot be shadowed Some languages allow you to put a label on a statement, and to goto (jump to) that statement from elsewhere in the program Java reserves the goto keyword but does not use it

5 native Sometimes you may wish to use a method that is defined in some other language (such as C or C++) The method modifier native tells Java to link to this “native” method To do this, you need to look into the JNI (Java Native Interface) API Drawbacks: You lose platform independence For security reasons, you cannot use native code in an applet

6 strictfp While Java is extremely platform independent, it isn’t perfect Due to differing machine implementations of floating-point arithmetic, floating-point results may be off by a couple of the least significant bits There is a standard, IEEE 754, that specifies exactly how floating-point arithmetic should be carried out It is computationally much less efficient to meet this standard if the hardware does not already meet it If this degree of platform independence is required, you can use strictfp in front of any class, interface, or method declaration A constructor cannot be declared FP-strict; to do this, you should designate the entire class as FP-strict A method in an interface cannot be declared FP-strict, because that’s implementation information, not an interface property native methods cannot be declared FP-strict Every compile-time constant expression is automatically FP-strict

7 transient You can serialize an object (turn it into a linear sequence of bytes) to write it to an output stream, then de-serialize it to read it in again Every field in the object must itself be Serializable If an object has fields that are not Serializable, and you don’t care that much about them anyway, you can declare them as transient transient variables don’t get serialized or de-serialized

8 volatile When the compiler optimizes your program, it may keep a variable in a register for several steps in a computation If another Thread might modify that variable, and you want to always get the current value (as set by some other Thread) of that variable, you can mark it as volatile Local variables never need to be marked volatile

9 The for loop The form of a for loop is: for ( initialization ; condition ; step ) body The initialization may be a declaration or expression, or a comma-separated list of expressions Recall that = is an operator, so var = expr is an expression The step may be an expression or a comma-separated list of expressions The initialization, condition, and step may be empty--but the semicolons may not be omitted An empty condition is equivalent to true Hence, for(;;); is an infinite loop Example: for (i = 0, j = a.length - 1; j > i; i++, j--) swap(a, i, j);

10 synchronized You need synchronized whenever you may have multiple Thread s accessing the same object at the same time A Thread gets a lock on a synchronized object before it uses it, and releases the lock (so another Thread can use the data) when it is done You can synchronize various ways: synchronized( object ) statement ; synchronized instanceMethod ( parameters ) { body } is equivalent to instanceMethod ( parameters ) {synchronized(this) { body }} synchronized staticMethod ( parameters ) { body } is equivalent to staticMethod ( parameters ) {synchronized( class ) { body }} Constructors and initializers cannot be synchronized Non-synchronized methods can execute at the same time as synchronized methods, on the same object

11 Labeled statements A labeled statement has the syntax label : statement The label has the same syntax as a variable name The scope of the label is the statement A break statement is only legal within a loop or switch statement and has one of the forms break; // exit innermost enclosing loop or switch break label ; // exit labeled enclosing loop or switch A continue statement is only legal within a loop, and has one of the forms: continue; // resume from test of innermost loop continue label ; // resume from test of labeled loop Any statement may be labeled, but it only makes sense on a loop or switch statement

12 Array initialization and literals Array initialization and array literals are very convenient, and are less well-known than they ought to be Initialization examples: int primes[ ] = {2, 3, 5, 7, 11, 13, 19}; String languages[ ] = { "Java", "C", "C++" }; int game[ ][ ] = { {3, 7}, {5, 4} }; Examples of array literals: myPrintArray(new int[] {2, 3, 5, 7, 11}); int foo[ ]; foo = new int[ ]{42, 83}; Person people[ ] = { new Person("Alice"), new Person("Bob"), new Person("Carla"), new Person("Don") };

13 Initialization blocks The fields of an object may be initialized when they are declared, or in a constructor, or both class Example { int x = 5; // done before constructor int y; Example(int n) { x = y = n; } } They may also be declared in an initialization block, which can contain statements as well as declarations class Example { int f = 1; { for (int i = 2; i <= 10; i++) f = f * i; } } “Instance” initialization blocks are executed when an object is created They are executed in the order in which they occur

14 Static initialization blocks static initialization blocks are like ordinary initialization blocks, except that they are executed once only, when the class is first loaded Example: class UsesRandomNumbers { static double[ ] values = new double[100]; static { for (int i = 0; i < values.length; i++) { values[i] = Math.random(); } }... }

15 Expression statements An expression statement is an expression followed by a semicolon Only certain expressions may be used this way: Assignment expressions, such as x = 0; Increment and decrement expressions, such as x++; Method call expressions You can call a method that returns a value and just ignore that value Object creation expressions As with a method, you can ignore the returned Object Other expression statements are flagged by Java as an error

16 Method call expressions A method call (which is an expression) must have one of these five forms: method ( parameters ) super. method ( parameters ) Class. method ( parameters ) Class.super. method ( parameters ) object. method ( parameters )

17 Escape sequences An escape sequence is used to specify a particular character (in a char literal or String literal) You probably already know a few escape sequences, such as \n for newline and \" for double quote Escape sequences can also be written as three-digit octal numbers \ ddd (ASCII) or four-digit hexadecimal numbers \u dddd (Unicode)

18 Interfaces An interface declaration has the form modifier interface interfaceName extends-clause { field-descriptions; method-descriptions; class-declarations; interface-declaration; } At the top level, the modifier can be public or absent (but public is always understood) Within a class or interface, the modifier can be static or absent (but static is always understood) and can be public, protected, or private An interface can extend a class or another interface A field-description is always static, final, and public, and must have an initial value method-descriptions are always abstract and public class-declarations are always static and public

19 Static import facility import static org.iso.Physics.*; class Guacamole { public static void main(String[] args) { double molecules = AVOGADROS_NUMBER * moles;... } } You no longer have to say Physics.AVOGADROS_NUMBER Are you tired of typing System.out.println( something ); ? Do this instead: import static java.lang.System.out; out.println( something );

20 varargs You can create methods and constructors that take a variable number of arguments public void foo(int count, String... cards) { body } The “... ” means zero or more arguments (here, zero or more String s) Call with foo(13, "ace", "deuce", "trey"); Only the last argument can be a vararg To iterate over the variable arguments, use the new for loop: for (String card : cards) { loop body }

21 java.util.Scanner Java finally has a fairly simple way to read input Scanner sc = Scanner.create(System.in); boolean b = sc.nextBoolean(); byte by = sc.nextByte(); short sh = sc.nextShort(); int i = sc.nextInt(); long l = sc.nextLong(); float f = sc.nextFloat(); double d = sc.nextDouble(); String s = sc.nextLine(); By default, whitespace acts as a delimiter, but you can define other delimiters with regular expressions

22 Formatted output Java 5 has a printf method, similar to that of C Each format code is % width code The width is the number of characters that are output (with blank fill) By default, output is right-justified A negative width means to left-justify the output Some values for the code are s for strings, d for integers, f for floating point numbers, b for booleans For floating point numbers, the width has the form total.right, where total is the total width and right is the number of digits to the right of the decimal point There are a huge number of options for formatting dates, which we won’t cover

23 Annotations Annotations allow you to mark methods as overridden, or deprecated, or to turn off compiler warnings for a method Example (in the Item public boolean equals(Item other) {... } Gives a syntax error because the signature is wrong Other provided annotations type ) You can create other kinds of annotations

24 Things that aren’t always obvious You can shadow a field... class Foo {int i;... {int i;...} }...but you can’t shadow a local variable or parameter: class Foo {...{int i;... {int i;...} } } // illegal In a conditional expression, e1 ? e2 : e3, e2 and e3 do not have to have the same type e2 and e3 must both be numeric, or be compatible reference types When creating an anonymous inner class from an interface, new Interface() { class-body }, the argument list must be empty Other odds and ends...

25 The End “Perfection is achieved not when you have nothing more to add, but when you have nothing left to take away.” --Antoine de Saint-Exupery