Primitive data types and wrapper classes

From Knowledge Kitchen
Jump to navigation Jump to search


Primitive data types

Primitive data types are not object-oriented. For this reason, Java is not a purely object-oriented programming language.

Read Oracle's overview of primitive data types

Numeric primitives

  • byte = 8 bit signed
  • short = 16 bit signed
  • int = 32 bit signed (default for integer literals)
  • long = 64 bit signed
  • float = 32 bit floating point
  • double = 64 bit floating point (default for decimal literals)

Floats and doubles are fundamentally inaccurate. Use java.math.BigDecimal for accuracy.

Textual primitives

  • char = 16 bit Unicode

String is not a primitive data type in Java

Logical primitives

boolean foo = true;

The implications of string not being a primitive data type

  • The == operator performs a comparison of the position in memory where two values are stored in memory, and not by the equivalence of the values stored there.
  • only primitive data types with the same value are guaranteed to be stored in the same spot in memory
  • String is not a primitive data type, it's an object-oriented class.
  • So two strings with the same value may be stored in different spots in memory
  • So use the .equals() method for Strings instead.

Literals

Integer literals

int decVal = 26; //decimal
int hexVal = 0x1a; //hexadecimal
int binVal = 0b11010; //binary

Decimal literals

double numb = 26.6; //double
float numb = 26.6F //float

Char literals

char letter = "A"; //alphabetic character
char letter = "\u0041"; //unicode character


Wrapper classes

A.k.a. utility or convenience classes. These are classes that offer useful methods for manipulating the types of data usually stored as primitive data types. They are called "wrappers" because they offer additional functionality built around primitive data types. These are fully-objected-oriented equivalents of the primitive data types.

  • Integer
  • Long
  • Short
  • Float
  • Double
  • Byte
  • Boolean
  • Character
  • Arrays

There is also a StringBuilder class, which is a wrapper class for the String class. Even though String is actually fully object-oriented, Strings are immutable. The StringBuilder class is a convenient mutable equivalent of the String class.

Converting data types

Double to int

Double double = new Double(5.0);
int integer = double.intValue();

Integer to String

String str = Integer.toString(5);
//or
String str = ""  +  5;

Double to String

String str = Double.toString(5.0);

Long to String

String str = Long.toString(50L);

Float to String

String str = Float.toString(5.0F);

String to Integer

int i = Integer.valueOf("5").intValue();
//or
int i = Integer.parseInt("5");

String to Double

Double d = Double.valueOf(str).doubleValue();
//or
Double d = Double.parseDouble(str);

String to Long

Long l = Long.valueOf("5").longValue();
//or
Long l = Long.parseLong("5");

String to Float

Float f = Float.valueOf("5").floatValue();

Decimal to Binary

String bin = Integer.toBinaryString(50);

Decimal to Hexadecimal

String hexstr = Integer.toHexString(50);
//or
String hexstr = Integer.toString(50, 16);

Hexadecimal to Decimal

int i = Integer.valueOf("B8DA3", 16).intValue();
//or
int i = Integer.parseInt("B8DA3", 16);

Char to String

String s = String.valueOf('c');

Integer to ASCII code

int i  = (int) "A";

Catching conversion exceptions

Use exception handling to handle any problems encountered while attempting to convert one data type to another.

try{
	i = Integer.parseInt(aString); 
} 
catch(NumberFormatException e) {   
	… 
}


What links here