1. Integer
public static void main(String[] args) {
int myFirstNumber = 5;
System.out.println(myFirstNumber);
}
Left of the =
sign have to be an expression.
Expression is construct that evaluates to a single value.
int myMinIntValue = Integer.MIN_VALUE;
Here Integer
is a wrapper class. Java uses the concept of a Wrapper class for all eight primitive types - In the case of an int, we can use Integer
, and by doing that it gives us ways to perform operations on an int.
In this case we're using the MIN_VALUE to get Java to tell us the minimum int that can be stored.
int myInt = 2147483648;
this is not human readable. It's human readable version is int myInt = 2_147_483_648;
2. Byte
byte myMinByteValue = Byte.MIN_VALUE;
byte myMabyteValueByteValue = Byte.MAX_VALUE;
System.out.println("Byte Minimum Value " + myMinByteValue);
System.out.println("Byte MabyteValueimum Value " + myMabyteValueByteValue);
3. Short
short myMinShortValue = Short.MIN_VALUE;
short myMabyteValueShortValue = Short.MAX_VALUE;
System.out.println("Short Minimum Value " + myMinShortValue);
System.out.println("Short MabyteValueimum Value " + myMabyteValueShortValue);
4. Long
long myLongValue = 100L;
long myMinLongValue = Long.MIN_VALUE;
long myMabyteValueLongValue = Long.MAX_VALUE;
System.out.println("Long Minimum Value " + myMinLongValue);
System.out.println("Long MabyteValueimum Value " + myMabyteValueLongValue);
long bigLongLiteralValue = 2_147_483_647_234L;
System.out.println("Big Long Literal Value " + bigLongLiteralValue);
5. Float
float myMinFloat = Float.MIN_VALUE;
float myMaxFloat = Float.MAX_VALUE;
System.out.println("Float Minimum Value: " + myMinFloat);
System.out.println("Float Maximum Value: " + myMaxFloat);
6. Double
Java by default takes a floating point number as double and whole number as int.
double is faster than float
double myMinDouble = Double.MIN_VALUE;
double myMaxDouble = Double.MAX_VALUE;
System.out.println("Double Minimum Value: " + myMinDouble);
System.out.println("Double Maximum Value: " + myMaxDouble);
int myIntValue = 5;
// float myFloatValue = (float) 5.25;
// float myFloatValue = 5.25f;
float myFloatValue = 5f / 3f;
double myDoubleValue = 5.00 / 3.00;
System.out.println("MyIntValue= " + myIntValue);
System.out.println("MyFloatValue= " + myFloatValue);
System.out.println("MyDoubleValue= " + myDoubleValue);
In case of double we can use the underscore notation also for example : double number = 2_000_000.4_567_891d;
- In general float and double are great for general floating point operations. But both are not great where precise calculations are required - this is due to a limitation with how floating point numbers are stored and not a Java problem such. Java has a class called BigDecimal that overcomes this.
7. Char
char myChar = 'D';
char myUnicodeChar = '\u0044';
System.out.println(myChar);
System.out.println(myUnicodeChar);
8. Boolean
boolean myTureBooleanValue = true;
boolean myFalseBooleanValue = false;
boolean isCustomerAdult = true;
Top comments (0)