Download presentation
Presentation is loading. Please wait.
Published byAbner Beasley Modified over 9 years ago
1
Basic Java Syntax Comments Basic data types Operators and assignment
2
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...
3
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 =.333333333333
4
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 (?)
5
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’
6
Booleans Pseudocode –TRUE/FALSE Java –true/false
7
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!
8
Pseudocode Complex Built-in User defined Atomic Num Char Boolean Ptr n/a Strings “Records”
9
Java Whole numbers –byte –short –int –long Fractional Numbers –float –double Characters –char Booleans –boolean 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!!!In case you’re curious, the next slide shows the technical details! Not important in CS 1311!!! Watch capitalization!!!
10
Data Type Ranges TypeSizeMinDefault booleanfalse Max 1false*true* char'\u0000' (null)16 byte(byte) 08-128127 short(short) 016-32,76832,767 int032 -2,147,483,6482,147,483,647 long0L64 -9,223,372,036,854,775,8089,223,372,036,854,775,807 float0.0F32 Approx ±3.4E+38 with 7 significant digits double0.0D64 Approx ±1.7E+308 with 15 significant digits void * Not truly min and max.
11
Java Complex Built-in User defined Primitives byte short int long float double char boolean n/a Strings plus lots more! “Classes”
12
Java Actually Objects! Built-in User defined Primitives byte short int long float double char boolean n/a Strings plus lots more! “Classes”
13
The Classic Duality Programming languages have always wrestled with the difference between assigning a value and the equality relational operator Equality (Boolean Result) –BASIC A = B –Pascal A = B –FORTRAN A.EQ. B –C A == B –Pseudocode A = B –Java A == B Equality (Boolean Result) –BASIC A = B –Pascal A = B –FORTRAN A.EQ. B –C A == B –Pseudocode A = B –Java A == B Assignment –BASIC LET A = B –Pascal A := B –FORTRAN A = B –C A = B –Pseudocode A B –Java A = B Assignment –BASIC LET A = B –Pascal A := B –FORTRAN A = B –C A = B –Pseudocode A B –Java A = B
14
Variable Declarations Pseudocode: – isoftype – e.g. Count isoftype Num Java: – ; – e.g. int count; or (optional initialization at declaration) – = ; – e.g. int count = 100; Equivalent to – int count; – count = 100;
15
Declaration Examples int Counter; int NumStudents = 583; double GPA; double BatAvg =.406; char Gender; char Gender = ‘f’; boolean Safe; boolean Empty = true;
16
Assignment Statements Pseudocode: – –e.g. Percent <- 100 * fraction Java: – = ; –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 )... Note: In Java, we distinguish this use of ‘=‘ from the equality test by using ‘==‘ to test for equality e.g. if( Percent == 50 )...
17
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 maxGrade = 100d; // now holds ‘100.0’ double temp = 583d; // holds double precision 583 double temp = 583. // Note decimal point float fTemp = 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 maxGrade = 100F; // now holds ‘100.0’ long x = 583l; // holds 583, but looks like 5,381 long y = 583L; // Ah, much better!
18
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
19
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.0F; 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!
20
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...
21
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: && ||
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.