Data types in Java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, October 28, 2018

Data types in Java

Data types in Java

Java is strongly typed language. Let's see what this means. First, every variable has a type, every expression has a type, and every type is strictly defined. Second, all assignments, whether explicit or via parameter passing in method calls are checked for type compatibility. The Java compiler checks all expressions and parameters to ensure that the types are compatible. Any type mismatches are errors that must be corrected before the compiler will finish compiling the class.

Java has two type of data.

  1. Primitive data type
  2. Non-primitive data type

Primitive data type

Java defines eight primitive type of data: byte, short, int, long, char, float, double, and boolean. These can be put in four category:

  1. Integers: This group includes byte, short, int, and long. All of these are signed, positive and negative values.
  2. Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
  3. Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
  4. Boolean: This group includes boolean, which is a special type for representing true/false values.
Data Type Default Size Default Value
byte 1 byte 0
short 2 byte 0
int 4 byte 0
long 8 byte 0L
float 4 byte 0.0f
double 8 byte 0.0d
char 2 byte '\u0000'
boolean false

Integers

Java defines four integer types: byte, short, int and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers.

byte data type

The byte data type is a primitive data type and smallest integer type. This is a signed 8-bit type. It's value range from -128 to 127. The default value of the byte data type is 0.

This data type byte is especially useful when you’re working with a stream of data from a network or file. They are also useful when you’re working with raw binary data that may not be directly compatible with Java’s other built-in types.

Example: byte b = 10, c = 20;

short data type

The short data type is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used Java type. The default value of short data type is 0.

Example: short b = 10, c = 20;

int data type

This is the most commonly used integer type. It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. The default value of int data type is 0.

int is best choice when integer is needed. you might think that using a byte or short would be more efficient than using an int in situations in which the larger range of an int is not needed, this may not be the case. The reason is that when byte and short values are used in an expression, they are promoted to int when the expression is evaluated.

Example: int b = 10, c = 20;

long data type

long is a signed 64-bit type. It has a range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The default value of long data type is 0.

It is useful for those occasions where an int type is not large enough to hold the desired value. This makes it useful when big, whole numbers are needed.

Example: long b = 10, c = 20000000000000000L;

Floating-point numbers

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine, result in a value whose precision requires a floating- point type.

float data type

The float data type is a single-precision 32-bit IEEE-754 floating point. Its approximate range 1.4e–045 to 3.4e+038. The default value of float data type is 0.0f.

float are useful when you need a fractional component, but don’t require a large degree of precision.

Example: float f = 10.0f;

double data type

The double data type is a double-precision 64-bit IEEE-754 floating point. Its approximate range 4.9e–324 to 1.8e+308. The default value of float data type is 0.0d.

All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.

Example: double d = 10.0d, e=10.01;

Characters

In Java, the data type used to store characters is char. However, C/C++ programmers beware: char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent characters.

char data type

The char data type is 16-bit type unicode character. The range of a char is 0('\u0000') to 65,536( '\uffff'). There are no negative chars.

Example: char x = 'A';

Boolean

Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false.

boolean data type

The boolean data type is used to store only two value: true and false. The default value of boolean data type is false.

Example: boolean flag = true;

Non-primitive data type

The non-primitive data types are String, Array etc.

Examples

Here we will discuss about some of the example that uses the primitive data types.

Example of integers data type

public class IntegersDataTypeDemo {
  public static void main(String[] args) {
    // example of byte data type
    byte x = 127;
    // byte is 8 bit value
    System.out.println("byte data type:: x=" + x);

    // It overflow here because
    // range of byte is -128 to 127
    x++;
    System.out.println("byte data type:: After overflow x=" + x);
    System.out.println();

    // example of the short data type

    short y = 32767;
    // short is 16 bit value
    System.out.println("short data type:: y=" + y);

    // It overflow here because
    // range of short is –32,768 to 32,767
    y++;
    System.out.println("short data type:: After overflow y=" + y);
    System.out.println();

    // example of int data type
    int i = 6888909;
    System.out.println("int data type:: i=" + i);
    System.out.println();

    // example of long data type
    int lightspeed;
    long days;
    long seconds;
    long distance;
    // approximate speed of light in miles per second
    lightspeed = 186000;
    days = 1000; // specify number of days here
    seconds = days * 24 * 60 * 60; // convert to seconds
    distance = lightspeed * seconds; // compute distance
    System.out.print("In " + days);
    System.out.print(" days light will travel about ");
    System.out.println(distance + " miles.");
  }
}

Output of the program:

byte data type:: x=127
byte data type:: After overflow x=-128

short data type:: y=32767
short data type:: After overflow y=-32768

int data type:: i=6888909

In 1000 days light will travel about 16070400000000 miles.

Example of Floating-point numbers

public class FloatingPointDemo {
  public static void main(String[] args) {
    // example of flat data type
    float a = 10.0f;
    float b = 20.01f;
    // multiplication of a and b
    // precedence of * is grater than +
    System.out.println("float data type:: a*b=" + a * b);
    System.out.println();

    // example of double data type
    double pi, r, area;
    r = 10.8; // radius of circle
    pi = 3.1416; // pi, approximately
    area = pi * r * r; // compute area
    System.out.println("double data type:: Area of circle is " + area);
  }
}

Output of the program:

float data type:: a*b=200.1

double data type:: Area of circle is 366.436224

Example of Character

public class CharacterDemo {
  public static void main(String[] args) {
    char ch1, ch2;
    ch1 = 88; // code for X
    ch2 = 'Y';
    System.out.print("ch1 and ch2: ");
    System.out.println(ch1 + " " + ch2);
  }
}

Output of the program:

ch1 and ch2: X Y

Example of Boolean

public class BooleanDemo {
  public static void main(String[] args) {
    boolean b;
    b = false;
    System.out.println("b is " + b);
    b = true;
    System.out.println("b is " + b);
    // a boolean value can control the if statement
    if (b) System.out.println("This is executed.");

    b = false;

    if (b) System.out.println("This is not executed.");
    // outcome of a relational operator is a boolean value
    System.out.println("10 > 9 is " + (10 > 9));
  }
}

Output of the program:

b is false
b is true
This is executed.
10 > 9 is true

No comments :

Post a Comment