Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Simple Types CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Java Simple Types CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Java Simple Types CSIS 3701: Advanced Object Oriented Programming

2 Basic Java Syntax Java syntax mostly same as C++ –Java developed by C++ programmers Examples –Lines/blocks: ; {} –Control structures: if else for while switch … –Operators: = + - * / ++ -- += … == != > = <= && || … –Comments: /* */ //

3 Simple Types Numeric types: –int 32 bits –short 16 bits –byte 8 bits –long 64 bits –float 32 bits, ~7 digits after decimal –double 64 bits, ~13 digits after decimal Defined standard in Java language

4 Simple Types Boolean type –keywords true and false –Example: boolean b = true; Char type –16-bit Unicode for international purposes –Examples: char c1 = ‘A’; char c2 = ‘\u3218’ –Numeric representation of non-ASCII character Tradeoff: memory vs. internationalization

5 Type Conversion int x = 3.7; –What does this do? Weak typing (C/C++) –Language attempts to convert one type to another –3.7 truncated to 3 and stored in x Strong typing (Java) –Must be no possible ambiguity or information loss –Otherwise syntax error

6 Type Conversion Other examples: –if (1) {…} Condition must be true or false –long y; int x; Possible information loss x = y; Must explicitly cast to less precise type –int x = (int)3.7; Tradeoff: safety vs. convenience

7 Constants and Types What type is the number 3 ? The number 3.7 ? –Number without decimal is int. –Number with decimal is double. Possible type conversion problems Example: float y = 3.7; –Syntax error – trying to store double as float –Java hack: Follow constant with f float y = 3.7f;


Download ppt "Java Simple Types CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google