Data Types
Data is an important part of a program. It's an ingredient of a program. All processing is done on data.
When a program is running in memory, a program should hold the data. The data is held temporarily in the main memory, not permanently in the hard-disk. Program should hold the data during the execution of a program.
Data is stored in variables. Variable are for storing data. In Java to store data, first declare the variable and then store the data. Variable will have a data type, which means the type of the data that is going to be store in that variable.
In Java, every variable and every expression has some type. Each and every data type is clearly defined, moreover, every assignment should be checked by the compiler for type compatibility (check that provided value and expected type are the same). Given all these reasons, we can conclude that Java is considered a strongly typed programming language (strongly typed means type checking is very important).
import java.lang.*;
class Test {
public static void main(String[] args) {
int x = 10.5; // INVALID
boolean b = 0; // INVALID
}
}
When compared with languages like C++, Java has a more object oriented nature. However Java is not considered as a pure object oriented programming language because several OOP features are not satisfied by Java (like operator overloading, multiple inheritance, etc..). Moreover Java depends on primitive data types, which are non objects.
There are some pre-defined data types available in Java.
Numeric Data Types are considered as signed data-types because we can represent both positive and negative values.
Integral: Integral data types can have any numerical value without a decimal point. Under integral, there is byte, short, int and long. All these are of same type, but different sizes.
byte: when using byte, positive numbers are represented directly in memory. Negative numbers are represented in two's complement form. To represent two's complement form, 7 bits are enough to represent -128. The most significant bit (msB) acts as sign bit, 0 means positive number, 1 means negative number. If a value is provided that is greater than the maximum value of the numerical data type (e.g. 128), a "possible loss of precision" compile-time error will be raised, with the "required" type being "byte" and "found" being "int" (because every Integral number is by default an "int" type). If a value is provided that is a floating-point type, then a "possible loss of precision" compile time error will also be raised, with the "required" type being "byte" and the "found" being "double". If a value is provided that is not a numeric type (e.g. true), then a "incompatible type" compile-time error will be raised, with "found" being "boolean" and "required" being "byte". If a String value is provided instead of byte then a "incompatible type" compile-time error will be raised, with found being "Java.lang.String" and "required" being "byte". The "byte" data type is best utilised where data is being handled in the form of "Streams", where you might want to write data to a file or might want to send data across the network. This is because the supported form for files and networks is "byte". There are 2 types of streams available character streams and byte streams.
import java.lang.*;
class Test {
public static void main(String[] args) {
byte a = 10; // VALID
byte b = 127; // VALID
byte c = 128; // INVALID: possible loss of precision compile-time error
byte d = 10.5; // INVALID: possible loss of precision compile-time error
byte e = true; // INVALID: incompatible types compile-time error
byte f = "string"; // INVALID: incompatible types compile-time error
}
}
short:: The least used data-type in Java is "short". If a value is provided that is greater than the maximum value of the numerical data type (e.g. 32768), a "possible loss of precision" compile-time error will be raised, with the "required" type being "short" and "found" being "int" (because every Integral number is by default an "int" type). If a value is provided that is a floating-point type, then a "possible loss of precision" compile time error will also be raised, with the "required" type being "short" and the "found" being "double". If a value is provided that is not a numeric type (e.g. true), then a "incompatible type" compile-time error will be raised, with "found" being "boolean" and "required" being "short". If a String value is provided instead of byte then a "incompatible type" compile-time error will be raised, with found being "Java.lang.String" and "required" being "short". When Java first came out in 1995, there were 8085 and 8086 microprocessor. 16-bit processors were very popular at that time. 16-bit processor means that the instruction length is 16-bits, and since short is 16-bit, it means read and write operations become very efficient. Therefore "short" data type is best suited for 16-bit processors like 8085/8086. Since 8085/8086 processors are now outdated, hence the corresponding data-type(short) for those processors is also rarely used.
import java.lang.*;
class Test {
public static void main(String[] args) {
short a = 32767; // VALID
short b = 32768; // INVALID: possible loss of precision compile-time error
short c = 10.5; // INVALID: possible loss of precision compile-time error
short d = true; // INVALID: incompatible types compile-time error
short e = "string"; // INVALID: incompatible types compile-time error
}
}
int: int is the most commonly used data-type in Java. If a value is provided that is greater than the maximum value of int data type (e.g. 2147483648), a "integer number too large". Unlike "byte" and "short" where a "loss of precision" compile-time error is raised, in this scenario, since every integral number is considered "int" by default, if a number i grater thatn "int" data-type max value, then a "integer number too large" compile-time error will be raised. If a "long" data type value is passed (e.g. 2147483648L), then a "possible loss of precision" compile-time error is raised, with "found" being "long" and "required" being "int". If a value is provided that is a floating-point type, then a "possible loss of precision" compile time error will also be raised, with the "required" type being "int" and the "found" being "double". If a value is provided that is not a numeric type (e.g. true), then a "incompatible type" compile-time error will be raised, with "found" being "boolean" and "required" being "int". If a String value is provided instead of byte then a "incompatible type" compile-time error will be raised, with found being "Java.lang.String" and "required" being "int".
import java.lang.*;
class Test {
public static void main(String[] args) {
int a = 2147483647; // VALID
int b = 2147483648; // INVALID: integer number too large compile-time error
int c = 2147483648L; // INVALID: possible loss of precision compile-time error
int d = true; // INVALID: incompatible types compile-time error
int e = "string"; // INVALID: incompatible types compile-time error
}
}
Although the int range is large, there are times where you'd need to store even larger numbers like factorial of 15 (15!). Antother instance is where the file.length() method is used get the number of characters in a file, to hold the number of characters in a large file, "int" data-type may not be enough as the number of characters may exceed int range. In these cases, we should use "long" data type, hence why the length() method available in "File" class return "long" data-type.
Floating point: Floating point data types can have any numerical value with a decimal point. Under floating point, there is float and double.
float | double |
---|---|
5 to 6 decimal points | 14 to 15 decimal points |
single precision | double precision |
If we want 5 to 6 decimal places of accuracy, then float should be used. If we want 14 to 15 decimal places of accuracy, then double should be used. float is single precision, double is double precision.
Non-Numeric Data Types are not considered as signed data-types because we cannot represent both positive and negative values.
char: char data types are for storing a character. Java supports other natural languages and their alphabets other than english, like hebrew, arabic, japanese, etc. In C or C++, only english is supported, hence why in C/C++ "char" size is 1 bytes. Whereas in Java char is 2 bytes, because for all other languages, unicode characters are used. For English language, ASCII codes are used. Java supports unicode and ASCII codes, which are a subset of unicode. Characters cannot be negative, so it only supports positive values from 0 to 65535.
boolean: boolean data types are for storing a true and false. In C/C++ 0 represents false and 1 represents true. In Java, only the keywords true or false are used represent true or false. The bytes (size) of boolean depends on the environment (JVM), so we cannot say exactly how many bits or bytes it takes, but the minimum is 1 bit. As for the range of boolean, it is not applicable, but the allowed values are true or false. If a value like 0 is provided to boolean an "incompatible types" error will be raised, with "found" being "int" (because all integral types are int by default" and "required" being boolean). If a value like "True" is provided, then a "cannot find symbol" compile-time error will be raised, with "symbol" being "variable True" and "location" being the class name (in this instance "class Test"). If a String value is provided instead of boolean then a "incompatible type" compile-time error will be raised, with "found" being "Java.lang.String" and "required" being "boolean".
import java.lang.*;
class Test {
public static void main(String[] args) {
boolean a = true; // VALID
boolean b = 0; // INVALID: incompatible types compile-time error
boolean c = True; // INVALID: cannot find symbol compile-time error
boolean d = "True"; // INVALID: incompatible types compile-time error
boolean d = null; // INVALID: incompatible types compile-time error
int x = 0;
if(x) { //Not valid in Java, however valid in C/C++; incompatible types compile-time error
System.out.println("Hello");
} else {
System.out.println("Hi");
}
while(1) { //Not valid in Java, however valid in C/C++; incompatible types compile-time error
System.out.println("Hi");
}
}
}
Type | Size (bytes) | Range | Default | Wrapper Class |
---|---|---|---|---|
byte | 1 | -128 to 127 | 0 | Byte |
short | 2 | -32,768 to 32,767 | 0 | Short |
int | 4 | -2,147,483,648 to 2,147,483,647 | 0 | Integer |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L | Long |
float | 4 | ±1.40129846432481707e-45 to ±3.40282346638528860e+38 | 0.0F | Float |
double | 8 | ±4.94065645841246544e-324d to ±1.79769313486231570e+308d | 0.0D | Double |
char | 2 | 0 to 65535 | \u0000 (space character) | Character |
boolean | ? | true/false | false | Boolean |
"null" is the default value for object reference and it cannot be applied to primitive data-types. If you try to use "null" for primitives, it will result in an "incompatible types compile-time error" with "found" being "<nulltype>" and "required" being the type of the primitive, if "char" then char, etc.