












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Overview and intro to types
Learning about:
References in text and in JavaSummary type: A.14 slide 4 variable: A.13 slide 7 variable declaration: A.15 slide 7 Primitive types, A.16, back inside cover slide 5 Constants, A.17 slide 20 Assignment, A.18-A.20 slide 8 Casting, A.21 slide 6 Expressions: A.22-A. Precedences: A.24, back inside cover Unicode character codes, back inside cover
Matlab and Python are weakly typed : One variable can contain at different times a number, a string, an array, etc. One isn’t so concerned with types. Valid Python sequence: x= 100 ; x= ‘Hello World’; x= ( 1 , 2 , 3 , 4 , 5 ); Corresponding Java int x; x= 100 ; x= “Hello”; Java strongly typed : A variable must be declared before it is used and can contain only values of the type with which it is declared Declaration of x: x can contain only values of type int Illegal assignment: “Hello” is not an int
Weakly typed : Shorter programs, generally. Programmer has more freedom, language is more liberal in applying operations to values. S trongly typed : Programmer has to be more disciplined. Declarations provide a place for comments about variables. More errors caught at compile-time (e.g. it’s a syntax error to assign a string to an int variable). Note: weak and strong typing not well defined; literature has several definitions
int : values: – 2 31 .. 2 31
Integer types: byte short int long 1 byte 2 bytes 4 bytes 8 bytes Real types: float double – 22.51E 4 bytes 8 bytes 24. Use these to save space. Have an array of 1,000,000 integers in range 0..7? Use a byte array rather than an int array usual operators usual operators Don’t worry about this in next 7 - 8 weeks. Use int and double. Inside back cover, A- 6 .. 7
16 char is a number type: ( int ) 'V' ( char ) 86 Unicode repr. in decimal: 86 'V' Unicode: 16-bit char repr. Encodes chars in just about all languages. In java, use hexadecimal (base 16) char literals: '\u 0041 ' is 'A' '\u 0042 ' is 'B' '\u 0056 ' is 'V' '\u 0024 ' is ‘$' See www.unicode.org '\u 0950 ' is 'ॐ’ —Om, the sound of the universe '\u 5927 ' is '大' —大衛 is (I think) a transliteration '\u 885 b' is '衛' of David into Chinese (Da Wei)
17 Declaration : gives name of variable, type of value it can contain int x; (^) Declaration of x, can contain an int value area 20. double double area; Declaration of area, can contain a double value x 5 int int [] a; Declaration of a, can contain a pointer to an int array. We explain arrays much later a int []
19 Every expression has a type, which depends on its operators and the types of its operands in a natural way. Rule: In x= e; type of e has to be same as or narrower than type of x. Reason: To avoid possibly losing info without the programmer realizing it. The value of 5 + 1 is automatically cast from type int to type double. double y= 5 + 1; int x= ( int ) (75.5 + 1 ); You can cast to int explicitly. 76 will be stored in x. int x= 75.5 + 1 ; Illegal: The exp value is of type double.
20 function s = sum(a, b) % Return sum of a and b s= a + b; /** return sum of a and b */ public static double sum( double a, double b) { return a + b; } def sum(a, b): """ return sum of a and b""" return a + b Matlab Python Declarations of return type parameters a and b Specification: in comment before function