ECSE 250
FUNDAMENTALS OF SOFTWARE
DEVELOPMENT
Lecture 2 – Java Syntax and Primitive Data Types
Lili Wei
Material by Giulia Alberini and Michael Langer
WHAT ARE WE GOING TO DO TODAY?
▪ Scope of a variable
▪ Primitive Datatype
▪ Characters
▪ Type Casting
SCOPE OF A
VARIABLE
SCOPE OF A VARIABLE
▪ A (local) variable only exists inside the block in which it is declared.
▪ When inside a block, a variable starts to exist when it is declared, and
▪ It ends to exist when the block (in which it was declared) ends.
▪ We will introduce more types of variables when introducing OOD.
EXAMPLE 1
int x = 5;
if (x > 0) {
int y = 0;
} else {
int y = x;
}
System.out.println(y);
int x = 5;
int y;
if (x > 0) {
y = 0;
} else {
y = x;
}
System.out.println(y);
EXAMPLE 2
int x = 2;
int y = 3;
if (x < y) {
x = x + y;
int z = 5;
y = z*x;
}
System.out.println(x + " " + y + " " + z);
int x = 2;
int y = 3;
int z = 0;
if (x < y) {
x = x + y;
z = 5;
y = z*x;
}
System.out.println(x + " " + y + " " + z);
EXAMPLE 3
for (int i= 0; i < 5; i++) {
System.out.println(i);
}
System.out.println(i);
int i;
for (i= 0; i < 5; i++) {
System.out.println(i);
}
System.out.println(i);
PRIMITIVE DATA
TYPES
PRIMITIVE TYPES
byte
▪ A primitive type is
short
▪ predefined by the language, and Integer values
int
▪ named by a reserved keyword
long
float
▪ Java supports 8 primitive data types. Real numbers
double
boolean true or false
char One character
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
WHY DIFFERENT TYPES?
▪ The difference between the types: number of bits reserved for those values.
Description Keyword Size Range
[−128, 127]
Very Small Integer byte 8 bits
Small Integer short 16 bits [−215 , 215 −1]
Integer int 32 bits [−231, 231 − 1]
Big Integer long 64 bits [−263, 263 − 1]
Low Precision Reals float 32 bits ~[-3.4*1038, 3.4*1038]
High Precision Reals double 64 bits ~[-1.8*10308,1.8*10308]
True/False boolean 1 bit [true, false]
One Character char 16 bits -
OVERFLOW AND UNDERFLOW
▪ Variables of type int store values between 231 − 1 and −231.
▪ 231 − 1 = 2147483647 (Integer.MAX_VALUE)
▪ −231 = −2147483648 (Integer.MIN_VALUE)
▪ What happens if:
int x = 2147483647; int y = -2147483648;
System.out.println(x+1); System.out.println(y-1);
Output:-2147483648 Output 2147483647
Choose the right datatype depending on your problem!
A NOT-SO-HARMFUL EXAMPLE
A NOT-SO-HARMFUL EXAMPLE
FLOATING POINT
▪ In Java, the default floating point type is double.
▪ All standard arithmetic operations can be done on floating point.
▪ NOTE: Java distinguishes between 1 and 1.0.
▪ If you write .0 after an integer, it will be considered a double
int x = 3.0;
int x = 3;
double x = 3.0;
BE CAREFUL!
▪ Java automatically converts one type to the other (e.g. int to double) if need be AND
if no loss of information would occur.
double x = 1; // legal, but bad style!
▪ If the mathematical operators are used with at least one operand of type double,
then java will convert the other operands to double and it will output a values of type
double.
▪ If all the operands are integers, the output of the operator will also be an integer!!
int x = 1.0/2; // compiler error!
double y = 1/4; // no compiler error, but is it correct?
CHAR
and
UNICODE
CHAR DATA TYPE
char is one of the primitive data types that we have in Java.
▪ We can declare and initialize a variable of type char as follows:
char letter = 'a';
▪ Character literals appears in single quotes
▪ Character literals can only contain a single character
ASCII VS UNICODE
▪ How are characters stored?
▪ An ordered list of characters, where each character corresponds to a
unique number.
▪ ASCII
▪ 7 bits ---> represent 128 characters.
▪ UNICODE
▪ 16 bits ---> 65536 characters.
▪ It is a superset of ASCII: the numbers 0-127 map to the same
characters both in ASCII and Unicode.
ASCII TABLE
JAVA USES UNICODE
▪ Java uses Unicode to represent characters.
▪ Variables of type char have 16 bits reserved in the memory to store a
value.
▪ Each character is represented by an integer.
Note: not every integer represent a character!
CHARACTER ARITHMETIC
▪ Since every character is practically an integer, we can
perform arithmetic operations on variables of type char.
char first = 'a';
char second = (char) (first + 1);
▪ What is the value of second?
▪ Note the typecasting!
▪ first evaluates to 97
▪ first + 1 evaluates to 98
▪ (char) convert it back to char
WHAT IS THE OUTPUT?
char letter = 'g';
if( letter == 'a') {
System.out.println("first letter of the alphabet");
}
else if (letter == 'z') {
System.out.println("Last letter of the alphabet");
}
else if (letter > 'a' && letter < 'z') {
System.out.println("Another letter of the alphabet");
}
else {
System.out.println("Not a lower case letter");
}
ESCAPE SEQUENCES
▪ Escape sequence: a sequence of characters that represents a special
character.
▪ Examples:
▪ \n represents the character newline
▪ \" or \' represent quotation marks
▪ \t represents a tab.
▪ Escape sequences are legal characters because they represent a single
character
char nl = '\n';
TYPE CASTING
TYPECASTING
▪ Typecasting: convert back and forth between variables of different types. (or
casting, for short)
int x = 3; // x is an integer
double y = 4.56; // y is a double
int n = (int) y; // convert y to integer
double m = (double) x; // convert x to double
▪ What are the values of n, and m?
➢ n = 4, m = 3.0
PRIMITIVE TYPE CONVERSION – INT & DOUBLE
▪ When going from int to double, an explicit cast is NOT necessary
because double is "wider" than int
int x = 3; // x is an integer
double m = (double) x; // convert x to double
double n = x; // it's ok to skip (double)
▪ When going from double to int, you will get a compile-time
error if you don't have an explicit cast.
PRIMITIVE TYPE CONVERSION – IN GENERAL
type number of bits
double 64
float 32
long 64
wider narrower
int 32
Here, wider usually
(but not always)
char 16
means more bytes. short 16
byte 8
Widening and narrowing change the bit representation (Details in ECSE 222 and
324)
NOTE: char is "special"... see the following slides.
EXAMPLES
int i = 3;
double d = 4.2;
d = i; // widening (implicit casting)
d = 5.3 * i; // widening (by "promotion")
i = (int)d; // narrowing (by casting)
float f = (float) d; // narrowing (by casting)
• For narrowing, you get a compilation error if you don't cast.
EXAMPLES WITH CHAR
char c = 'q';
int x = c // widening
c = (char) x; // narrowing
short y = 12;
c = y; // compile time error! (need explicit casting)
y = c; // compile time error! Narrowing need explicit
casting
A HARMFUL EXAMPLE
▪ The Ariane 5 Failure (1996)
▪ Self-destructed 37 seconds after launch.
▪ Lost: Over US$1 billion
A HARMFUL EXAMPLE
▪ Conversion from 64-bit floating point value larger than 215 −1 to 16-bit
signed integer value caused an overflow.
▪ Program crashed -> rocket crashed
MORE EXAMPLES ABOUT CASTING
▪ What if we add int and long together?
MORE EXAMPLES ABOUT CASTING – FLOAT VS DOUBLE
▪ What about floats and doubles?
SUMMARY
▪ Today ▪ Next Lecture
▪ Scope of a variable ▪ Strings
▪ Primitive Datatype ▪ Arrays
▪ Characters
▪ Typecasting
▪ Your To-do list
▪ Tutorials start this week!
▪ First weekly assignment –
submit Hello World to GitHub