Array Declarations in Programming Languages | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Sussman; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-mla-3
koofers-user-mla-3 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433, Alan Sussman, U. Maryland (via
Bill Pugh) 1
CMSC433, Spring 2002
Programming Language
Technology and Paradigms
Basic Java, continued
Alan Sussman
February 5, 2002
CMCS 433, Spring 2002 -Alan Sussman 2
Administrivia
Quiz returned today
Revised version of project posted
clarify myCounter behavior
specify wire/socket format for timestamps
sample code for creating a scrolling text
window
Account issues?
change those passwords!
CMCS 433, Spring 2002 -Alan Sussman 3
Last time
Java basics
statements, expressions, operators
values primitive types and references to
objects
object operations assignment, equals(),
toString(), clone(), getClass(), instanceof, cast
arrays
CMCS 433, Spring 2002 -Alan Sussman 4
Array declarations
int[] A and int A[] have identical semantics
declares Ato be a variable containing a reference to an
array of ints
int[] A[], B;
A is a ref to an array of refs to arrays of ints
Bis a ref to an array of ints
None of these allocate an array
A = new int [10]; allocates an array of 10 ints,
and makes Abe a reference to it
CMCS 433, Spring 2002 -Alan Sussman 5
Array example
int[] array1 = {1, 3, 5};
int[][] a = new int[10][3];
// a.length == 10
// a[7].length == 3
a[5][2] = 42;
a[9] = array1;
// a[9][2] == 5
// use of array initializers
int[][] twoD = {{1, 2, 3}, {4, 5}, {6}};
Object [] args = {“one”, “two”, a};
main(new String [] {“a”, “b”, “c”});
CMCS 433, Spring 2002 -Alan Sussman 6
String
A class for representing non-mutable strings
string constants converted to String
+does string concatenation
In some contexts objects automatically
converted to String type
Example:
public static void printArray(Object [] a) {
for (int i = 0; i < a.length; i++)
System.out.println(“a[“ + i + “] = “ + a[i]);
}
pf3

Partial preview of the text

Download Array Declarations in Programming Languages | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland (via

CMSC433, Spring 2002

Programming Language

Technology and Paradigms

Basic Java, continued

Alan Sussman

February 5, 2002

CMCS 433, Spring 2002 - Alan Sussman 2

Administrivia

• Quiz returned today

• Revised version of project posted

  • clarify myCounter behavior
  • specify wire/socket format for timestamps
  • sample code for creating a scrolling text window

• Account issues?

  • change those passwords!

CMCS 433, Spring 2002 - Alan Sussman 3

Last time

• Java basics

  • statements, expressions, operators
  • values – primitive types and references to objects
  • object operations – assignment, equals(), toString(), clone(), getClass(), instanceof, cast
  • arrays

CMCS 433, Spring 2002 - Alan Sussman 4

Array declarations

  • int[] A and int A[] have identical semantics
    • declares A to be a variable containing a reference to an array of ints
  • int[] A[], B;
    • A is a ref to an array of refs to arrays of ints
    • B is a ref to an array of ints
  • None of these allocate an array
  • A = new int [10]; allocates an array of 10 ints, and makes A be a reference to it

CMCS 433, Spring 2002 - Alan Sussman 5

Array example

int[] array1 = {1, 3, 5}; int[][] a = new int[10][3]; // a.length == 10 // a[7].length == 3

a[5][2] = 42; a[9] = array1; // a[9][2] == 5

// use of array initializers int[][] twoD = {{1, 2, 3}, {4, 5}, {6}}; Object [] args = {“one”, “two”, a}; main(new String [] {“a”, “b”, “c”}); CMCS 433, Spring 2002 - Alan Sussman 6

String

• A class for representing non-mutable strings

• string constants converted to String

• + does string concatenation

• In some contexts objects automatically

converted to String type

• Example:

public static void printArray(Object [] a) { for (int i = 0; i < a.length; i++) System.out.println(“a[“ + i + “] = “ + a[i]); }

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Spring 2002 - Alan Sussman 7

Object/memory allocation

  • Only way/time an object gets allocated is:
    • by executing new
      • one object per invocation of new
    • by having an array constant (e.g., {5, -5, 42})
    • by having a string constant (e.g.,“Hello world”)
  • Declaring a reference variable does not allocate an object
  • Allocating an array does not automatically allocate the contents of the array CMCS 433, Spring 2002 - Alan Sussman 8

Allocation (cont.)

• Multi-dimensional array allocation

  • int [][] a = new int [10][10];
  • equivalent to, but faster than: int [][] a = new int [10][]; for(int i=0; i<10; i++) a[i] = new int[10];

• No explicit deallocate required, nor allowed

CMCS 433, Spring 2002 - Alan Sussman 9

Garbage collection

• Java uses garbage collection to find objects

that cannot be referenced

  • i.e. do not have any pointers to them

• GC not a major performance bottleneck

  • fast garbage collectors have been implemented
  • on many commercial systems, GC runs on a single processor of a multiprocessor

CMCS 433, Spring 2002 - Alan Sussman 10

Other Java notes

  • Forward references resolved automatically
    • can refer to method/variable defined later
  • Integer division by zero raises an exception
  • Integer overflow drops extra bits
  • Floating point errors create special values
    • NaN, POSITIVE_INFINITY, …
  • Separate name spaces for methods, classes, variables, … - can produce confusing error messages

CMCS 433, Spring 2002 - Alan Sussman 11

What’s missing

  • preprocessor (#include, #define, …)
  • structs and unions
  • enumerated types
  • bit-fields
  • variable-length argument lists
  • multiple inheritance (of implementation)
  • operator overloading
  • templates/ parameterized types
    • GJ (generic Java)

Object-oriented programming in

Java