1. This example shows default values of 8 primitive types in Java.
class PrimitiveTypeDcoder {
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
static char c;
static boolean bl;
public static void main(String[] args) {
System.out.println("There are 8 primitive types in JAVA:");
System.out.println("Byte :" + b);
System.out.println("Short :" + s);
System.out.println("Int :" + i);
System.out.println("Long :" + l);
System.out.println("Float :" + f);
System.out.println("Double :" + d);
System.out.println("Char :" + c);
System.out.println("Boolean :" + bl);
}
}
Output:
There are 8 primitive types in JAVA:
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char :_
Boolean :false
2. This Java Example shows how to declare and use Java primitive byte
variable inside a java class.
public class JavaByte {
public static void main(String[] args) {
/* * byte is smallest Java integer type. * byte is 8 bit signed type ranges
from –128 to 127. * byte is mostly used when dealing with raw data like reading a *
binary file. * * Declare byte varibale as below * * byte = ; * * here assigning
default value is optional. */
byte b1 = 100;
byte b2 = 20;
System.out.println("Value of byte variable b1 is :" + b1);
System.out.println("Value of byte variable b1 is :" + b2);
}
}
Output:
Value of byte variable b1 is: 100
Value of byte variable b1 is: 20
3. This Java Example shows how to declare and use Java primitive
boolean variable inside a java class.
public class JavaBoolean {
public static void main(String[] args) {
/* * boolean is simple Java type which can have only of two values; true or
false. * All rational expressions retrun this type of value. * * Declare boolean
varibale as below * * boolean = ; * * here assigning default value is optional. */
boolean b1 = true;
boolean b2 = false;
boolean b3 = (10 > 2) ? true: false;
System.out.println("Value of boolean variable b1 is :" + b1);
System.out.println("Value of boolean variable b2 is :" + b2);
System.out.println("Value of boolean variable b3 is :" + b3);
}
}
Output:
Value of boolean variable b1 is: true
Value of boolean variable b2 is: false
Value of boolean variable b3 is: true
4. This Java Example shows how to declare and use Java primitive int
variable inside a java class.
public class JavaInt {
public static void main(String[] args) {
/* * int is 32 bit signed type ranges from –2,147,483,648 * to 2,147,483,647.
int is also most commonly used integer * type in Java. * Declare int varibale as
below * * int = ; * * here assigning default value is optional. */
int i = 0;
int j = 100;
System.out.println("Value of int variable i is :" + i);
System.out.println("Value of int variable j is :" + j);
}
}
Output:
Value of int variable i is: 0
Value of int variable j is: 100
5. This Java Example shows how to declare and use Java primitive char
variable inside a java class.
public class JavaChar {
public static void main(String[] args) {
/* * char is 16 bit type and used to represent Unicode characters. * Range of
char is 0 to 65,536. * * Declare char varibale as below * * char = ; * * here
assigning default value is optional. */
char ch1 = 'a';
char ch2 = 65;
/* ASCII code of 'A'*/
System.out.println("Value of char variable ch1 is :" + ch1);
System.out.println("Value of char variable ch2 is :" + ch2);
}
}
Output:
Value of char variable ch1 is: a
Value of char variable ch2 is: A
6. This Java Example shows how to declare and use Java primitive float
variable inside a java class.
import java.util. * ;
public class JavaFloatExample {
public static void main(String[] args) {
/* * float is 32 bit single precision type and used when fractional precision *
calculation is required. * * Declare float varibale as below * * float = ; * * here
assigning default value is optional. */
float f = 10.4f;
System.out.println("Value of float variable f is :" + f);
}
}
Output:
Value of float variable f is: 10.4
7. This Java Example shows how to declare and use Java primitive double
variable inside a java class.
public class JavaDouble {
public static void main(String[] args) {
/* * double is 64 bit double precision type and used when fractional precision
* calculation is required. * * Declare double varibale as below * * double = ; * *
here assigning default value is optional. */
double d = 1232.44;
System.out.println("Value of double variable d is :" + d);
}
}
Output:
Value of double variable f is: 1232.44
8. This Java Example shows how to declare and use Java primitive long
variable inside a java class.
import java.util. * ;
public class JavaLong {
public static void main(String[] args) {
/* * long is 64 bit signed type and used when int is not large * enough to hold
the value. * * Declare long varibale as below * * long = ; * * here assigning default
value is optional. */
long timeInMilliseconds = new Date().getTime();
System.out.println("Time in milliseconds is : " + timeInMilliseconds);
}
}
Output:
Time in milliseconds is: 1226836372234
9. This Java Example shows how to declare and use Java primitive short
variable inside a java class.
public class JavaShort {
public static void main(String[] args) {
/* * short is 16 bit signed type ranges from –32,768 to 32,767. * * Declare
short varibale as below * * short = ; * * here assigning default value is optional.
*/
short s1 = 50;
short s2 = 42;
System.out.println("Value of short variable b1 is :" + s1);
System.out.println("Value of short variable b1 is :" + s2);
}
}
Output:
Value of short variable b1 is: 50
Value of short variable b1 is: 42
10. This int to String java example shows how to convert int to String in
Java.
public class IntToString {
public static void main(String args[]) { //int variable
int i = 11;
/* * To convert int to String, use
* toString(int i)
* method of Integer wrapper class. */
String str = Integer.toString(i);
System.out.println("int to String : " + i);
}
}
Output:
int to String : 11