Working with Primitive Data Types - Java Language Basics

Java examples for Language Basics:Primitive Types

Introduction

Type Explanation
intA 32-bit (4-byte) integer value
short A 16-bit (2-byte) integer value
long A 64-bit (8-byte) integer value
byte An 8-bit (1-byte) integer value
float A 32-bit (4-byte) floating-point value
double A 64-bit (8-byte) floating-point value
char A 16-bit character using the Unicode encoding scheme
booleanA true or false value

Integer types

Java allows you to promote an integer type to a larger integer type.

For example, Java allows the following:

Demo Code

public class Main {
  public static void main(String[] args) {
    int xInt;//from ww w. ja v a2 s.c om
    long yLong;
    xInt = 32;
    yLong = xInt;

  }

}

Floating-point types

Floating-point numbers are numbers that have fractional parts.

When you use a floating-point literal, you should always include a decimal point, like this:

Demo Code

public class Main {
  public static void main(String[] args) {
    double period = 99.0;
    //from   w  w w. ja v a 2s .  c om
    System.out.println(period);
  }

}

If you omit the decimal point, the Java compiler treats the literal as an integer.

You can add an F or D suffix to a floating-point literal to indicate whether the literal itself is of type float or double.

For example:

Demo Code

public class Main {
  public static void main(String[] args) {
    float value1 = 199.33F;
    double value2 = 123456.995D;

  }/*from  w  ww  .  j a  va  2s . c  o  m*/

}

Related Tutorials