A Concept More Primitive Than The Cavemen

Written by jennifer.greenberg | Published 2018/05/06
Tech Story Tags: programming | java | data | code | stack

TLDRvia the TL;DR App

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 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);}}


Published by HackerNoon on 2018/05/06