Java Theory Tokens and Data Types
Java Theory Tokens and Data Types
1
Can consist of uppercase, lowercase, digits, $ sign and underscore (_) character (NO
SPACE)
Begins with letter, $, or _ (NO NUMBER)
Is case sensitive
Cannot be a keyword
Can be of any length
2
; (semicolon- used as statement terminator),
, (comma- used to separate list of variables),
. (dot or decimal- used make fraction value, include package, referencing
object).
STATIC Initialization: Variable is initialized during its declaration with defined constant.
E.g. int i = 10;
DYNAMIC Initialization: Variable is initialized at run time, i.e. during execution.
e.g. i = a + b;
2. How much space do the following take – bit, terabyte, nibble, kilobyte?
Ans.
Name Size
Bit 1 bit
Nibble 4 bits
Byte 8 bits
Kilobyte 1,024 bytes
Megabyte 1,024 kilobytes
Gigabyte 1,024 megabytes
Terabyte 1,024 gigabytes
Petabyte 1,024 terabytes
Exabyte 1,024 petabytes
Zettabyte 1,024 exabytes
Yottabyte 1,024 zettabytes
3
1. What is Data type?
Ans.
Data type refers to the type of data that can be stored in a variable. Java has two types
Primitive and Reference Data Types.
4
i. Explicit Type Casting
ii. Implicit Type Casting
Implicit type casting occurs if you assign any of these data types to higher data types
byte –> short –> int –> long –> float –> double
e.g
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
Explicit Typecasting
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size.
This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed
by the programmer. The higher size is narrowed to lower size.
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value
and may also lose precision and range.
e.g.
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
int y = (int)x; // ok. Explicit Typecasting