Adding example for Primitives

This commit is contained in:
hitanshu310 2026-01-03 16:56:46 +05:30
parent ff26c81b98
commit 52ee8d1b90

View File

@ -0,0 +1,85 @@
package com.hithomelabs.dsa.Primitives;
import java.util.stream.IntStream;
public class PrimitiveExample {
public static void main(String[] args) {
// * * byte, short, int and long are the integer types in Java
byte a = 24;
short b = 24;
int c = 24;
long d = 24;
// * * Minimum and maximum values are given by wrapper fields
System.out.printf("Byte has min value %d, max value %d and occupies %d bits \n",Byte.MIN_VALUE, Byte.MAX_VALUE, Byte.SIZE);
System.out.printf("Short has min value %d, max value %d and occupies %d bits \n",Short.MIN_VALUE, Short.MAX_VALUE, Short.SIZE);
System.out.printf("Integer has min value %d, max value %d and occupies %d bits \n",Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.SIZE);
System.out.printf("Long has min value %d, max value %d and occupies %d bits \n",Long.MIN_VALUE, Long.MAX_VALUE, Long.SIZE);
// * * In Java, integer literals are int by default.
// * * If too large to fit in an `int` (32-bit), you must explicitly mark it as a `long`
long l = 2147483649l; // * * trying to assign a value greater than Integer.MAX_VALUE
// * * Widening convention, no explicit casting needed when converting values to a bigger type
b = a;
c = b;
d = c;
// * * Narrowing convention, when storing a bigger value to a smaller type (lossy conversion)
// * * Explicit casting is required
c = (int) l;
System.out.println("The integer value is: " + c);
/*
* * The most significant 4 bytes get discarded
* * binary representation of l: 10000000 00000000 00000000 00000001
* * binary representation of c: c
* * where MSB (sign) bit is set so c is in 2's complement form
* * value of c is -ve 2's complement of: 10000000 00000000 00000000 00000001
* * which is : 01111111 11111111 11111111 11111111
*/
b = (short) c;
System.out.println("the short value is :" + b);
a = (byte) b;
System.out.println("the byte value is :" + a);
// * * Float and Double are data dtypes for decimals
System.out.printf("Float has min value %f, max value %f, occupies %d bits \n",Float.MIN_VALUE, Float.MAX_VALUE, Float.SIZE);
System.out.printf("Double has min value %f, max value %f, occupies %d bits \n",Double.MIN_VALUE, Double.MAX_VALUE, Double.SIZE);
// * * In Java, decimal literals are double by default.
// * * saving value to a float, f needs to be marked to the end
float dbl = 12.56565667f;
// * * trying to assign large integers to floating point types results in loss of precision but does not need to be casted
float fl = Integer.MAX_VALUE;
System.out.println("Float value of integer is :" + fl);
// * * Trying to assign largest float value to integer leads to overflow and needs to be casted.
int in = (int) Float.MAX_VALUE;
System.out.println("Integer value of float is :" + in);
// * * Characters are 2 bytes
System.out.println("Character size in bits: " + Character.SIZE);
System.out.printf("Characters range from %d to %d \n", (int) Character.MIN_VALUE, (int) Character.MAX_VALUE);
// * * First 128 characters are ASCII values
IntStream.rangeClosed(0,128).mapToObj(i -> (char) i).forEach(System.out::println);
}
}