Basic Java Syntax - PowerPoint PPT Presentation

About This Presentation
Title:

Basic Java Syntax

Description:

From the double slash to the end of the // line becomes a ... In case you're curious, the next shows the technical details! Not important in CS 1311! ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 23
Provided by: BillL161
Category:
Tags: basic | curious | java | syntax

less

Transcript and Presenter's Notes

Title: Basic Java Syntax


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 lt b
  • No compatibility with numbers
  • Java
  • char still holds a single character A
  • Can be used for sorting a lt 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
  • Worlds 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
  • Well defer Strings since they are implemented as
    Objects!

8
Pseudocode
Complex
Atomic
Built-in
Num Char Boolean Ptr
Strings
User defined
n/a
Records
9
Java
  • Whole numbers
  • byte
  • short
  • int
  • long
  • Fractional Numbers
  • float
  • double
  • Characters
  • char
  • Booleans
  • boolean
  • Relax!!!
  • We wont use all of these
  • Probably just...
  • In case youre curious, the next slide shows the
    technical details! Not important in CS 1311!!!

Watch capitalization!!!
10
Data Type Ranges
Type
Size
Min
Default
Max
boolean
false
1
false
true
char
'\u0000' (null)
16
byte
(byte) 0
8
-128
127
short
(short) 0
16
-32,768
32,767
int
0
32
-2,147,483,648
2,147,483,647
long
0L
64
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
float
0.0F
32
Approx 3.4E38 with 7 significant digits
double
0.0D
64
Approx 1.7E308 with 15 significant digits
void
Not truly min and max.
11
Java
Complex
Primitives
Built-in
byte short int long float double char boolean
Strings plus lots more!
User defined
n/a
Classes
12
Java
Actually Objects!
Primitives
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
  • ltidentifiergt isoftype ltdata typegt
  • e.g. Count isoftype Num
  • Java
  • ltdatatypegt ltidentifiergt
  • e.g. int count
  • or (optional initialization at declaration)
  • ltdata typegt ltidentifiergt ltinit valuegt
  • 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
  • ltrecipientgt lt- ltvaluegt
  • e.g. Percent lt- 100 fraction
  • Java
  • ltrecipientgt ltvaluegt
  • 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 lt- 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)
  • // doesnt solve problem...

21
Operators
  • Arithmetic , -, , /, (mod), etc.
  • Example x (a b)/(c d)
  • Relational gt, lt, gt, lt,
  • ! instead of ltgt
  • instead of
  • Example boolean quit (index 100)
  • Boolean
  • AND becomes
  • OR becomes
  • NOT becomes !
  • if( !quit (index lt 5) )
  • // do something

Note these must be double
22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com