Java Data Types

As explained in the previous chapter, the exception to Java should be the type of data specified:


Example

Data types are divided into two groups:

  • Primitive data types - including byte, short, int, long, float, double, boolean and char
  • Non-classic data types - such as String, Arrays and Classes (you will learn more about this in the next chapter)

Primitive Data Types

Older data type specifies different size and type of values, and has no additional options.

There are eight types of older data in Java:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Numbers

The first number types are divided into two groups:

Integer types store whole, positive or negative numbers (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type to use, depends on the number of digits.

Floating point types represent numbers with a fraction, consisting of one or more decimal. There are two types: float and double.


Even though there are many types of numbers in Java, the most commonly used numbers are int (whole numbers) and double (floating point numbers). However, we will explain them all as you read.



Integer Types

Byte

Byte data type can store integers from 128 to 127. This can be used instead of int or other integer types to save memory if you are sure the value will be between 128 and 127:


Example
byte myNum = 100;
        System.out.println(myNum);
            

Short

The short data type can store whole numbers from 32768 to 32767:


Example
short myNum = 5000;
        System.out.println(myNum);
            

Int

The int data type can store absolute numbers from -2147483648 to 2147483647. Generally, and in our study, the int data type is the preferred data type when we create variables in numerical values.


Example
int myNum = 100000;
        System.out.println(myNum);
            

Long

Long data types can store complete numbers from -9223372036854775808 to 9223372036854775807. This is used when the int is not large enough to store the value. Note that you must complete the value in "L":


Example
long myNum = 15000000000L;
        System.out.println(myNum);
            


Floating Point Types

You should use a floating point type whenever you need a decimal number, such as 9.99 or 3.14515.

Float

Floating data type can store partial numbers from 3.4e − 038 to 3.4e + 038. Note that you must complete the value with "f":


Example
float myNum = 5.75f;
        System.out.println(myNum);
            

Double

Double data type can store partial numbers from 1.7e − 308 to 1.7e + 308. Note that you must complete a value with "d":


Example
double myNum = 19.99d;
        System.out.println(myNum);
            


Use to float or double?

The accuracy of the floating point value indicates how many digits the value can have after the decimal point. Float accuracy is only six or seven digits, while double variables have an accuracy of about 15 digits. It is therefore safe to use duplicates in most calculations.


Scientific Numbers

A floating point number can also be a scientific number with an "e" to indicate a power of 10:


Example
float f1 = 35e3f;
        double d1 = 12E4d;
        System.out.println(f1);
        System.out.println(d1);
            


Booleans

The boolean data type is declared by the boolean keyword and can only take values ​​true or false:


Example
boolean isJavaFun = true;
        boolean isFishTasty = false;
        System.out.println(isJavaFun);     // Outputs true
        System.out.println(isFishTasty);   // Outputs false
            

The rational values ​​are used extensively in conditional testing, which you will learn more about in the next chapter.


Characters

The char data type is used to store a single character. The character should be surrounded by single quotes, such as 'A' or 'c':


Example
char myGrade = 'B';
        System.out.println(myGrade);
            

Alternatively, you can use ASCII values ​​to display specific characters:


Example
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
        System.out.println(myVar1);
        System.out.println(myVar2);
        System.out.println(myVar3);
            


Strings

String data type is used to keep the alphabetical order (text). Character unit values ​​should be surrounded by two quotes:


Example
String greeting = "Hello World";
        System.out.println(greeting);
            

String type is widely used and integrated in Java, so much so that some call it the "special ninth type".

String in Java is a type of data that is not old-fashioned, because it refers to an object. The String object has methods that are used to perform certain functions on the strings. Don't worry if you don't understand the word "object" yet. We will learn more about this unit of characters and objects in the next chapter.



Non-Primitive Data Types

Non-classical data types are called reference types because they refer to objects.

The main differences between primitive and non-primitive types of data are:

  • Older versions are already defined (already defined) in Java. Non-classic versions created by editor and not defined by Java (other than String).
  • Non-traditional types can be used to call for ways of doing certain tasks, while older types can be null.
  • The older version remains valuable, while the older versions may not work.
  • The older type starts with a lower case letter, while the non-older type starts with a lower case letter.
  • Older size depends on the type of data, while non-classic types have the same size.