Primitive Data Types
The Primitive Data Types are used to define variables that can store data values. Primitive data types
are built into the Java language. They are NOT objects created from a class. You do not use the keyword new
when defining primitive variables. The keyword new is only used to create an object from some class.
Default values are for instance variables only. If you define a local variable (inside a method), you
must assign a value to it yourself before using the variable in an expression.
Type Bits Default Range (inclusive)
byte 8 0 -128 to 127
char 16 0 0 to 65,535 (unsigned)
short 16 0 -32,768 to 32,767
int 32 0 -2,147,483,648 to 2,147,483,647
float 32 0.0f 32 bit floating point number (decimals)
long 64 0L -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807
double 64 0.0 64 bit floating point number (decimals)
boolean 8 false false or true
You can think of the bits as representing the size of the storage box.
Examples:
int a,b,c;
b = 13;
c = 5;
a = b + c; // a would receive the value 18
a = b / c; // a would receive the value 2 (decimal part truncated)
a = b % c; // a would receive the value 3 (the remainder)
char someChar = 'A'; // same as someChar = 65;
int d = 26;
int e = 0x1a; (0x indicates the number has been written in hexadecimal (base 16)
int f = 0b11010; (ob indicates the number has been written in binary)
double x = 323.4;
double y = 3.234e2; // same as 323.4
float z = 3.14159f; // must have the f, otherwise 3.14159 would be considered a double
Some General Assignment Rules (well not without a type cast or doing a manual conversion):
1) You can't store what's in a bigger storage box in a smaller storage box (the number of bits (size) matters).
someChar = d; would be illegal since someChar (16 bits) cannot hold the 32 bits used by variable d
2) You can't store a decimal number in a non decimal storage box.
a = x; would be illegal since a can only hold integer numbers.
3) You can't store a value that might be negative in an unsigned storage box.
someChar = a; would be illegal since a might hold a negative number and someChar is unsigned.
import java.util.Scanner;
public class Sample2 {
public Sample2() {
}
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("Semester Grade");
System.out.println();
System.out.println();
System.out.print("Enter your semester 1 grade ");
double s1 = scan.nextDouble();
System.out.println();
System.out.print("Enter your semester 2 grade ");
double s2 = scan.nextDouble();
System.out.println();
System.out.print("Enter your final exam grade ");
double exam = scan.nextDouble();
System.out.println();
System.out.println();
double grade = (s1 + s2) / 2 * .8 + exam * .2;
System.out.println("Your semester grade is " + grade );
System.out.println();
System.out.println();
}
}
import java.util.Scanner;
public class Sample4 {
public Sample4() {
}
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("Fahrenheit to Celsius");
System.out.println();
System.out.println();
System.out.print("Enter the Fahrenheit Temperature ");
double f = scan.nextDouble();
double c = (5.0/9.0)*(f-32);
System.out.println();
System.out.println("The Celsius Temperature is " + c);
System.out.println();
System.out.println();
}
}
import java.util.Scanner;
public class Sample3 {
public Sample3() {
}
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("Slope of a Line");
System.out.println();
System.out.println();
System.out.print("Enter x1 ");
double x1 = scan.nextDouble();
System.out.print("Enter y1 ");
double y1 = scan.nextDouble();
System.out.println();
System.out.print("Enter x2 ");
double x2 = scan.nextDouble();
System.out.print("Enter y2 ");
double y2 = scan.nextDouble();
System.out.println();
System.out.println();
if (x2-x1==0)
{
System.out.println("The slope of the line is undefined");
}
else
{
double slope = (y2-y1)/(x2-x1);
System.out.println("The slope of the line is " + slope);
}
System.out.println();
System.out.println();
}
}
More on Primitive Data Types