Tutorials Hut

Java Numbers Class and Characters

In previous articles of data types we already saw different data types, primitive data types like int, float, double etc., in this article we will discuss java numbers and java characters which are wrapper classes and non-primitive, they can hold numeric values and also methods to do various operations on them.

Example of primitive data types:

int a = 20;

float b = 2.3f;

double c = 2.45d;

Examples of wrapper class based numbers:

Integer a = 20;

Float b = 2.3f;

Double c = 2.45d;

If we look at both primitive and wrapper class based numbers look the same, but there is a huge difference, primitive numbers are just numbers and stored in memory but wrappers hold value of number and other methods as well with class.

Different Java Number Classes (Wrapper Classes)

  • Byte – Holds byte number and useful methods
  • Double – Holds double number and useful methods
  • Float – Holds float number and useful methods
  • Integer – Holds int number and useful methods
  • Short – Holds short and useful methods
  • Long – Holds long and useful methods
  • AtomicInteger – These are similar to integer but available in concurrency package of java and used in multi threaded on concurrent programs.
  • AtomicLong – Similar to long but used in concurrent or multi threaded programs.
  • BigDecimal – It is used to hold huge or big numbers, takes more memory.
  • BigInteger – Similar to Integer but holds big integer numbers, takes more memory.

Examples of Java Number Classes

public class NumberClasses {
public static void main(String[] args) {
    Integer a = 20;
    Float b = 20.23f;
    Double c = 23.44d;
   Byte d = 2;
    System.out.println("Integer a ="+a);
    System.out.println("Float b ="+b);
    System.out.println("Double c ="+c);
    System.out.println("Byte d ="+d);
}
}

Output:

Integer a =20
Float b =20.23
Double c =23.44
Byte d =2

Methods in Wrapper Class Numbers

There are different methods available in number classes which we can use for different purposes, below are few methods listed.

1. toString() – converts to string

2. byteValue() – returns bytes

3. compareTo(some number)– compares

4. doubleValue() – /returns double

5. equals(some number) –  checks if the numbers are equal or not

6. floatValue() – returns float value

7. longValue() – returns long value

8. intValue() –  returns int

9. shortValue() –  returns short

Example of Java Number Class methods:

public class NumberClasses {
public static void main(String[] args) {
    Integer a = 20;
    a.toString(); //converts to string (java int to String)
    a.byteValue(); // returns bytes
    a.compareTo(10); //compares
  a.doubleValue(); //returns double
    a.equals(20); // checks if the numbers are equal or not
    a.floatValue(); // returns float value
    a.longValue(); // returns long value
    a.intValue(); // returns int
    a.shortValue(); // returns short
Integer.parseInt("10") //java string to int
}
}

Java Autoboxing and Unboxing

Auto boxing  – It is a way to automatically convert primitive data type to wrapper class or non-primitive data types.

Example –

Integer a = 10; //primitive int 10 is auto boxed to wrapper class Integer a

Unboxing –It is reverse of auto boxing where a non-primitive data types is converted to primitive or wrapper class.

Example –

Integer a = 10;
int b = a; // a wrapper class converted to non-primitive, unboxing

Java Characters

In previous chapters we saw a little bit about java characters, here we will see more details, java characters are used to store any single letter or symbol.

Syntax:

char  = ‘value of variable’;

Example:

public class Characters {
public static void main(String[] args) {
    char a = 'x'; //Holding a letter x in form of char
    char b = '1'; // Holding a number 1 in form of char
    char c = '='; //Holding equals = symbol in form of char
    System.out.println("char a: "+a+" b : "+b+" c: "+c);
}
}

Output:

char a: x b : 1 c: =

References

Oracle’s article on Java Numbers.

Next Article To Read

You may be interested to read our next articles:














  • Leave a Reply

    Your email address will not be published. Required fields are marked *