Open swapnilmadavi opened 2 years ago
In Java, there are eight primitive data types:
byte: 8-bit signed integer. Range: -128 to 127. short: 16-bit signed integer. Range: -32,768 to 32,767. int: 32-bit signed integer. Range: -2^31 to 2^31 - 1. long: 64-bit signed integer. Range: -2^63 to 2^63 - 1. float: 32-bit floating point. Range: approximately ±3.40282347E+38F (6-7 significant decimal digits). double: 64-bit floating point. Range: approximately ±1.79769313486231570E+308 (15 significant decimal digits). char: 16-bit Unicode character. Represents a single character. Range: '\u0000' to '\uffff'. boolean: Represents true or false values.
byte: 8-bit signed integer (-128 to 127).
Saves memory in large arrays.
byte b = 100;
short: 16-bit signed integer (-32,768 to 32,767).
Also useful for memory savings in large arrays.
short s = 10000;
int: 32-bit signed integer (-2^31 to 2^31-1).
Can be used as an unsigned integer (0 to 2^32-1) in Java SE 8+.
int i = 100000;
long: 64-bit signed integer (-2^63 to 2^63-1).
Can be used as an unsigned integer (0 to 2^64-1) in Java SE 8+.
long l = 100000L;
float: 32-bit single-precision floating point.
Not for precise values like currency.
float f = 10.5f;
double: 64-bit double-precision floating point.
Default for decimal values, not for precise values.
double d = 10.5;
boolean: Represents true/false values.
boolean flag = true;
char: 16-bit Unicode character ('\u0000' to '\uffff').
char c = 'A';
Is a reference type. Strings are an object they are instances of the java.lang.String class. For string manipulation wide range of methods are provided which includes concatenation, comparison, and substring extraction. String objects are immutable, any modification results in the creation of a new String object.
Strings being an important DataType have been provided a special initialization technique:
String str1 = "Hello, World!";
however, java also support initialization using new keyword like reference types :
String str2 = new String("Hello, World!");
For the question Can you list 8 primitive types in java? in Objects and Primitives section,
String
is listed as an extra Primitive data type.According to the Java documentation (here),