A Concept More Primitive Than The Cavemen by@jennifer.greenberg

A Concept More Primitive Than The Cavemen

image
Jennifer Greenberg HackerNoon profile picture

Jennifer Greenberg

image

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.

image

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

Image from tutorialspoint.com

image

Image from tutorialspoint.com

image

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 falseboolean t = true;// declaring characterchar a = ‘G’;// declaring bytebyte b = 4;// declaring shortshort s = 56;






// declaring intint i=89;//declaring a longlong l = 244843984;// declaring a float — for float use ‘f’ as suffixfloat f = 4.7333434f;


// declaring a double — default fraction value is double in javadouble 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);}}

react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

L O A D I N G
. . . comments & more!