Jennifer Greenberg

@jennifer.greenberg

A Concept More Primitive Than The Cavemen

The title is a lie.

This concept, however, is a primitive concept in Java. Go ahead and grab a cup of Java, before we dive into the concept of primitive data types.

Boolean, char, byte, short, int, long, float, and double are all primitive data types in Java. These are all words that Java knows. These values are stored in the stack, as opposed to the heap. Variables that are on the stack are accessible directly from memory, and can run very fast. Objects are on the heap, and take more time to access.

Every primitive type in Java has a wrapper class.

  • short has Short
  • int has Integer
  • long has Long
  • boolean has Boolean
  • char has Character
  • float has Float
  • double has Double
  • byte has Byte

The wrappers have methods attached to them that the primitive types do not have. These include…

Image from tutorialspoint.com
Image from tutorialspoint.com
Image from tutorialspoint.com

Wrapper classes are primitives are also stored differently in memory. The wrapper classes are stored on the stack as a reference to an object on the heap.

class PrimitivePost
{
public static void main(String args[])
{
//declearing a boolean, this returns true or false
boolean t = true;
// declaring character
char a = ‘G’;
// declaring byte
byte b = 4;
// declaring short
short s = 56;
 // declaring int 
int i=89;
//declaring a long
long l = 244843984;
// declaring a float — for float use ‘f’ as suffix
float f = 4.7333434f;

// declaring a double — default fraction value is double in java
double d = 4.355453532;


System.out.println(“boolean: “ + t);
System.out.println(“char: “ + a);
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);
}
}

More by Jennifer Greenberg

Topics of interest

More Related Stories