Java Data Types (Primitive)
Java data types
As the name suggests, data types in Java specify the type of data that can be stored in Java variables.
Java is a static type language. This means that all variables must be declared before they can be used.
int speed;
Here the speed is a variable and the data type of the variable is int.
The int data type determines that the speed variable can only contain integers.
There are 8 predefined data types in the Java programming language, called primitive data types.
Note: Besides the primitive data types, there are also referenced types (object type).
8 primitive data types
1.boolean type
- The Boolean data type has two possible values, true or false.
- Default: false.
- They are generally used for true / false conditions.
Example 1: Java Boolean Data Type
class Main {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag); // prints true
}
}
2.byte type
- The byte data type can have values between -128 and 127 (8-bit signed two's complement integer).
- If it is certain that a variable's value will be between -128 and 127, then it is used in place of int to save memory.
- Default: 0
Example 2: Java Byte Data Type
class Main {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range); // prints 124
}
}
3.short type
- The short data type in Java can have values between -32768 and 32767 (16-bit signed two's complement integer).
- If it is certain that a variable's value will be between -32768 and 32767, then it is used instead of other integer data types (int, long).
- Default: 0
Example 3: Java short data type
class Main {
public static void main(String[] args) {
short temperature;
temperature = -200;
System.out.println(temperature); // prints -200
}
}
4.type int
- The int data type can have values between -231 and 231-1 (32-bit signed two's complement integer).
- If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 232-1. To learn more, see How to use unsigned integer in java 8?
- Default: 0
Example 4: Java int data type
class Main {
public static void main(String[] args) {
int range = -4250000;
System.out.println(range); // print -4250000
}
}
5.long type
- The long data type can have values between -263 and 263-1 (64-bit signed two's complement integer).
- If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264-1.
- Default: 0
Example 5: long java data type
class LongExample {
public static void main(String[] args) {
long range = -42332200000L;
System.out.println(range); // prints -42332200000
}
}
Notice the use of L at the end of -42332200000. This means that it is an integral literal of type long. You will learn more about full literals later in this article.
6.dual type
- The double data type is a 64-bit double-precision floating point.
- It should never be used for specific values such as currency.
- Default: 0.0 (0.0d)
Example 6: Double Java data type
class Main {
public static void main(String[] args) {
double number = -42.3;
System.out.println(number); // prints -42.3
}
}
7.float type
- The float data type is a 32-bit single-precision floating point. Learn more about single precision and double precision floating point if you are interested.
- It should never be used for specific values such as currency.
- Default value: 0.0 (0.0f)
Example 7: Java float data type
class Main {
public static void main(String[] args) {
float number = -42.3f;
System.out.println(number); // prints -42.3
}
}
Note that we used -42.3f instead of -42.3 in the above program. This is because -42.3 is a double literal.
To tell the compiler to treat -42.3 as a float rather than a double, you need to use f or F.
If you want to know more about single precision and double precision
8.character type
- It is a 16-bit Unicode character.
- The minimum value of the char data type is '\u0000' (0) and the maximum value of is'\uffff'.
- Default:'\u0000'
Example 8: Java char data type
class Main {
public static void main(String[] args) {
char letter = '\u0051';
System.out.println(letter); // prints Q
}
}
Here the Unicode value of Q is \u0051. Therefore, we get Q as the output.
Here is another example:
class Main {
public static void main(String[] args) {
char letter1 = '9';
System.out.println(letter1); // prints 9
char letter2 = 65;
System.out.println(letter2); // prints A
}
}
Here, we have assigned 9 as a character (specified by single quotes) to the variable letter1. However, the variable letter2 is assigned to 65 as an integer (without single quotes).
Therefore, A is printed on the output. This is because Java treats characters as integral types and the ASCII value of A is 65. Learn more about ASCII
String type
Java also supports strings through the java.lang.String class. Strings in Java are not primitive types. Instead, they are objects. For example,
String myString = "Java Programming";
Here, myString is an object of the String class. To learn more
Comments
Post a Comment