1 / 22

Basic Java Syntax

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

trinity
Download Presentation

Basic Java Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Atomic Complex Built-in Num Char Boolean Ptr Strings User defined n/a “Records”

  9. 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!!! Java Watch capitalization!!!

  10. Data Type Ranges Type Size Min Max Default boolean 1 false* true* false char 16 '\u0000' (null) 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 0L float 32 Approx ±3.4E+38 with 7 significant digits 0.0F double 64 Approx ±1.7E+308 with 15 significant digits 0.0D void * Not truly min and max.

  11. Java Primitives Complex Built-in byte short int long float double char boolean Strings plus lots more! User defined n/a “Classes”

  12. Java Primitives Actually Objects! Built-in byte short int long float double char boolean Strings plus lots more! User defined n/a “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 • Assignment • BASIC LET A = B • Pascal A := B • FORTRAN A = B • C A = B • Pseudocode A  B • Java A = B

  14. 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;

  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: • <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 ) ...

  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: && ||

More Related