Basic Java Syntax Comments Basic data types Operators
Basic Java Syntax Comments Basic data types Operators and assignment
Quick Comments // Works just like in pseudocode // From the double slash to the end of the // line becomes a comment (no semicolon) Also works like: a = b; // This is okay // more later. . .
Pseudocode • Num – Could hold all types of numbers – Absolutely no rules • No limits 0 to • No concerns about precision 3. 14159265358979323 • No concerns about accuracy 1/3 =. 333333
Java • Two types of numbers – Whole numbers • Generically known as integer type numbers – Fractional numbers • Typically known as floating point numbers • Why? – Whole numbers • • Exact Compact Fast Limited size – Fractional numbers • Used for “real” world applications • Much larger range • Imprecise (? )
Characters • Pseudocode – char was used to store a single character: ‘A’ – Could be used for sorting: ‘a’ < ‘b’ – No compatibility with numbers • Java – char still holds a single character: ‘A’ – Can be used for sorting: ‘a’ < ‘b’ – Internally is actually stored as a number • e. g. ‘a’ actually stored as 97 – Can do arithmetic! ‘a’ + 1 equals ‘b’
Booleans • Pseudocode – TRUE/FALSE • Java – true/false
Strings • Pseudocode – – – World’s most fabulous built-in string type! Very untypical Historically was linked list of characters! Too much work for too little benefit Remember: Pseudocode can do anything!!! • Java – We’ll defer Strings since they are implemented as Objects!
Pseudocode Atomic Complex Built-in Num Char Boolean Ptr Strings User defined n/a “Records”
Java • Whole numbers – – byte short int long • Fractional Numbers – float – double • Characters – char • Booleans • Relax!!! • We won’t use all of these Probably just. . . • In case you’re curious, the next slide shows the technical details! Not important in CS 1311!!! – boolean Watch capitalization!!!
Data Type Ranges Type Size Min false* Max Default true* false boolean 1 char 16 byte 8 -128 127 (byte) 0 short 16 -32, 768 32, 767 (short) 0 int 32 -2, 147, 483, 648 2, 147, 483, 647 0 long 64 -9, 223, 372, 036, 854, 775, 808 9, 223, 372, 036, 854, 775, 807 0 L float 32 Approx ± 3. 4 E+38 with 7 significant digits 0. 0 F double 64 Approx ± 1. 7 E+308 with 15 significant digits 0. 0 D 'u 0000' (null) void * Not truly min and max.
Java Primitives Complex Built-in byte short int long float double char boolean Strings plus lots more! User defined n/a “Classes”
Java Primitives Actually Objects! Built-in byte short int long float double char boolean Strings plus lots more! User defined n/a “Classes”
The Classic Duality • Programming languages have always wrestled with the difference between assigning a value and the equality relational operator • Equality (Boolean Result) – – – BASIC Pascal FORTRAN C Pseudocode Java A A A = B. EQ. B == B • Assignment – – – BASIC Pascal FORTRAN C Pseudocode Java LET A = B A : = B A = B
Variable Declarations • Pseudocode: – – <identifier> isoftype <data type> e. g. Count isoftype Num • Java: – – <datatype> <identifier>; e. g. int count; • or (optional initialization at declaration) – – <data type> <identifier> = <init value>; e. g. int count = 100; • Equivalent to – – int count; count = 100;
Declaration Examples int Counter; int Num. Students = 583; double GPA; double Bat. Avg =. 406; char Gender = ‘f’; boolean Safe; boolean Empty = true;
Assignment Statements • Pseudocode: – <recipient> <- <value> – e. g. Percent <- 100 * fraction • Java: – <recipient> = <value>; – e. g. Percent = 100 * fraction; Note: In Java, we distinguish this use of ‘=‘ from the equality test by using ‘==‘ to test for equality e. g. if( Percent == 50 ). . .
Assignment Examples • Note that whole integers appearing in your source code are taken to be ‘ints’. So, you might wish to flag them when assigning to non-ints: double max. Grade = 100 d; // now holds ‘ 100. 0’ double temp = 583 d; // holds double precision 583 double temp = 583. // Note decimal point float f. Temp = 5. 5; // ERROR! // Java thinks 5. 5 is a double • Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but always use L): float max. Grade = 100 F; // now holds ‘ 100. 0’ long x = 583 l; // holds 583, but looks like 5, 381 long y = 583 L; // Ah, much better!
Casting • In Pseudocode we had a simple rule • NO TYPE MISMATCHING!!! • In other words: • A <- B • Required that A and B be the same type – Num, Char, Boolean, String, Ptr • Most (if not all) real languages realize that it is often necessary to convert from one type to another. Two ways. . . – Do it automatically – Enforce typing rules
Casting • In Java two things can happen when you type mismatch – The compiler will realize that you may be losing information and give you an error – The compiler may know how to make the conversion with no problem • Example float f = 123. 0 F; double d = 123. 0; f = d; // ERROR d = f; // No problem It is possible to override this behavior! f = (float)d; // Note: d is unchanged. Casting!
Casting • It gets trickier! • Suppose we want to divide two ints int a = 5; int b = 2; float f; f = a/b; f = ? • But what if I want the decimals? • One solution float fa = a; float fb = b; f = fa/fb; f = ? • Or we could do: f = (float)a/(float)b; • Note: f = (float)(a/b); • // doesn’t solve problem. . .
Operators • Arithmetic: +, -, *, /, % (mod), etc. Example: x = (a + b)/(c + d); • Relational: >, <, >=, <=, != instead of <> == instead of = Example: boolean quit = (index == 100); • Boolean: AND becomes && OR becomes || NOT becomes ! if( !quit || (index < 5) ) { // do something } Note: these must be double: && ||
- Slides: 22