Core Java || Primitive Data Type
3 min readJul 15, 2023
Size of Primitive Data Types
Wrapper class & its helpful static final field: -
- 8 bits = 0000 0000 = 1 byte (values -128 to 127 & MSB is sign bit)
- SIZE: data type occupies how many bits of memory
- BYTES: data type occupies how many bytes of memory (BYTES = SIZE / BYTE.SIZE)
System.out.println("Data Type\t| Size (in bytes)\t| Size (in bits)");
System.out.println("----------------------------------------------");
System.out.println("byte\t\t| " + Byte.BYTES + "\t\t\t| " + (Byte.SIZE));
System.out.println("short\t\t| " + Short.BYTES + "\t\t\t| " + (Short.SIZE));
System.out.println("int\t\t\t| " + Integer.BYTES + "\t\t\t| " + (Integer.SIZE));
System.out.println("long\t\t| " + Long.BYTES + "\t\t\t| " + (Long.SIZE));
System.out.println("----------------------------------------------");
int
- By default values are given in Decimal Number System
- If the value starts with 0 then its Octal re-presentation (Range: 0–7)
- If the value starts with 0x/ 0X then its Hexa-Decimal re-presentation (Range: 0-F)
- If you provide the value from out of range, it will give a compile-time error
// Octal representation
int a = 010;
System.out.println(a); //8
// Hexa-Decimal representation
int b = 0x10;
System.out.println(b); //16
int c = 0xF;
System.out.println(c); //15
float & double
float f = 34.5f;
System.out.println(f);
double d = 34.455;
System.out.println(d);
char
char c = 'a';
System.out.println(c);
System.out.println(Character.isAlphabetic(c)); // true
System.out.println(Character.isLetter(c)); // true
System.out.println(Character.isDigit(c)); // false
System.out.println(Character.isLetterOrDigit(c));
System.out.println(Character.isLowerCase(c)); // true
System.out.println(Character.isUpperCase(c)); // false
char space = ' ';
char tab = '\t';
char newline = '\n';
System.out.println(Character.isSpaceChar(space)); // true
System.out.println(Character.isSpaceChar(tab)); // false
System.out.println(Character.isSpaceChar(newline)); // false
System.out.println(Character.isWhitespace(space)); // true
System.out.println(Character.isWhitespace(tab)); // true
System.out.println(Character.isWhitespace(newline)); // true
Type Casting: -
- Type Casting is the process of converting a value from one data type to another data type in a programming language
- There are two types of type casting: Implicit (automatic) casting & Explicit (manual) casting.
Explicit Type Casting: -
- As we know, a byte can only number from -128 to 127 as its size is 8 bytes
- byte b = 128; // error
- What happens if you assign the value 128 to the byte??? It will give an error java: incompatible types: possible lossy conversion from int to byte
- when assigning a larger data type, such as
int
, to a smaller data type, such asbyte
then requires an explicit casting - byte b= (byte)128; // -127
Binary Re-presentation of Number: -
Positive Number: —
5: 0000 0101
Negative Number: —
(-5): one’s complement of 5 + 1
= (one’s complement of 0000 0101) + 1
= (1111 1010) + 1
= 1111 1011
Decimal to Binary: -
- 0000 0101: MSB is 0 means the number is positive -> 5
- 1111 1011: MSB is 1 means the number is negative, magnitude = (one’s complement + 1) = (negative sign) 0000 0101 = (-5)
int to byte Conversion: -