Docsity

First Look at Java - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download First Look at Java - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

A First Look At Java

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Outline

 13.2 Thinking about objects  13.3 Simple expressions and statements  13.4 Class definitions  13.5 About references and pointers  13.6 Getting started with a Java language system

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Chapter Thirteen Modern Programming Languages, 2nd ed. 4

My x: 5 My y: 20 My color: black Things I can do: move report x report y

My x: 20 My y: 10 My color: dark g rey Things I can do: move report x report y

My x: 17 My y: 25 My color: light g rey Things I can do: move report x report y

Docsity.com

Java Terminology

 Each point is an object  Each includes three fields  Each has three methods  Each is an instance of the same class

Chapter Thirteen Modern Programming Languages, 2nd ed. 5

My x: 10 My y: 50 My color: black Things I can do: move report x report y

Docsity.com

Java Class Definitions: A Peek

Chapter Thirteen Modern Programming Languages, 2nd ed. 7

public class Point { private int x,y; private Color myColor; public int currentX() { return x; } public int currentY() { return y; } public void move(int newX, int newY) { x = newX; y = newY; } }

field definitions

method definitions

Docsity.com

Outline

 13.2 Thinking about objects  13.3 Simple expressions and statements  13.4 Class definitions  13.5 About references and pointers  13.6 Getting started with a Java language system

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Primitive Types We Won’t Use

byte : -2 7 ..2 7 -  short : -2 15 ..2 15 -  long : -2 63 ..2 63 -1, written with trailing Lfloat : IEEE 32-bit standard, written with trailing F ( 1.2e-5 , 1e3 )

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 10

Constructed Types

 Constructed types are all reference types: they are references to objects

  • Any class name, like Point
  • Any interface name (Chapter 15)
  • Any array type, like Point[] or int[] (Chapter 14)

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 11

A String Object

Chapter Thirteen Modern Programming Languages, 2nd ed. 13

My data: Hello there My length: 11 Things I can do: report my length report my ith char make an uppercase version of myself etc.

"Hello there"

Docsity.com

Numeric Operators

int : + , - , ***** , / , % , unary –

double : + , - , ***** , / , unary –

Chapter Thirteen Modern Programming Languages, 2nd ed. 14

Java Expression Value 1+23 7 15/7 2 15%7 1 -(55) -**

Java Expression Value 13.02.0 26. 15.0/7.0 2.*

Docsity.com

Comparisons

 The usual comparison operators < , <= , >= , and > , on numeric types  Equality == and inequality != on any type, including double (unlike ML)

Chapter Thirteen Modern Programming Languages, 2nd ed. 16

Java Expression Value 1<=2 true 1==2 false true!=false true

Docsity.com

Boolean Operators

&& and || , short-circuiting, like ML’s andalso and orelse! , like ML’s nota?b:c , like ML’s if a then b else c

Chapter Thirteen Modern Programming Languages, 2nd ed. 17

Java Expression Value 1<=2 && 2<=3 true 1<2 || 1>2 true 1<2? 3 : 4 3

Docsity.com

Assignment

a=b : changes a to make it equal to b  Assignment is an important part of what makes a language imperative

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 19

Rvalues and Lvalues

 Why does a=1 make sense, but not 1=a?  Expressions on the right must have a value: a , 1 , a+1 , f() (unless void ), etc.  Expressions on the left must have memory locations: a or d[2] , but not 1 or a+  These two attributes of an expression are sometimes called the rvalue and the lvalue

Chapter Thirteen Modern Programming Languages, 2nd ed. (^) Docsity.com 20