Lecture 2
Lecture 2
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
1 Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2 Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Short
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
• Default value is 0.
int
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there is a concern about memory.
• The default value is 0
• Example: int a = 100000, int b = -200000
Long
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed
• Default value is 0L
• Example: long a = 100000L, long b = -200000L
float
• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f
double
• double data type is a double-precision 64-bit IEEE 754 floating point
• This data type is generally used as the default data type for decimal values, generally the default choice
• Double data type should never be used for precise values such as currency
• Default value is 0.0d
• Example: double d1 = 123.4
boolean
• boolean data type represents one bit of information
• There are only two possible values: true and false
• This data type is used for simple flags that track true/false conditions
• Default value is false
• Example: boolean one = true
char
• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letterA = 'A'
Primitive Data Types
• Integers (int) e.g. -1, 0, 3, 1000, -2179, +123546.
• Floating point values (real numbers) (float, double) e.g. 98.6, 3.1419, -
3.4561e-5, 3, .4
Special Characters
These are characters which have a function they perform
Special Character Function
\b Back space
\f Form feed
\r Carriage return
\n New line
\t Tab
Initialization of Variables
• Initializing a variable means assigning a value to the variable for the first
time. A variable can be initialized when it is declared or after it has been
declared. Below is the syntax for the two possible ways to initialize a
variable.
• Data_type Identifier = Data_value;
• Data_type Identifier;
• Identifier = Data_value;
• Examples of variable declaration are as follows:
• int grade = 91; //91 is stored
• float price = 2; // 2.0 is stored
Operators in Java
Relational
Arithmetic e.g. +, -, /, Logic e.g. &&, ||,! e.g. >,<,==,!=
*, ++ , --, %
++ AND-- (UNARY INCREMENT AND DECREMENT OPERATORS)
. The operators ++ and -- are unary operators; they work with a single operand. They’re used to increment or decrement the
value of a variable by 1.
. Unary operators can also be used in prefix and postfix notation.
. In prefix notation, the operator appears before its operand:
int a = 10;
++a;
. In postfix notation, the operator appears after its operand:
int a = 10;
a++;
. When these operators are not part of an expression, the postfix and prefix notations
behave in exactly the same manner:
int a = 20;
int b = 10;
++a;
b++;
System.out.println(a);//prints 21
System.out.println(b);//prints 11
When a unary operator is used in an expression, its placement with respect to its operand decides whether its value
will increment or decrement before the evaluation of the expression or after the evaluation of the expression. See
the following code, where the operator ++ is used in prefix notation:
int a = 20;
int b = 10;
int c = a - ++b;
System.out.println(c); //prints 9
System.out.println(b);// prints 11
When ++ is used in postfix notation with an operand, its value increments after it
has been used in the expression:
int a = 20;
int b = 10;
int c = a - b++;
System.out.println(c); // print 10
System.out.println(b); // print 11
int a = 10;
a = a++ + a + a-- - a-- + ++a;// a = 10 + 11 + 11 - 10 + 10;
System.out.println(a);
The output of this code is 32.
Conditional Ternary Operator