Java Data Types

This article aims to make understanding Java data types easier by evaluating them and providing examples.

JAVA Data Types

Java Data Types

In Java, a variable must be a certain data type, as was discussed in the previous chapter:

Example: 

public class Main { public static void main(String[] args) { int my_num = 25; // integer (whole number) float my_Float_num = 5.99f; // floating point number char my_Letter = 'D'; // character boolean my_Bool = true; // boolean String my_Words = "Hello am learning Java Data Types"; // String System.out.println(my_num); System.out.println(my_Float_num); System.out.println(my_Letter); System.out.println(my_Bool); System.out.println(my_Words); } }

Example: 

public class Main { public static void main(String[] args) {String mrx_string="This is a String data type"; float ample_PI_value=3.142f; // This is a float data type int mrx=45; // this represents an integer boolean isJavaEasy=true; // The boolean value is represented here char mrx_start='J'; // Shows the character data typeSystem.out.println(mrx_string); System.out.println(ample_PI_value); System.out.println(mrx); System.out.println(isJavaEasy); System.out.println("The word Java starts with : "+mrx_start); } }

There are two categories of data types when discussing Java data types:

  1. Primitive data types – includes byte, short, int, long, float, double, boolean and char
  2. Non-primitive data types – such as String, Arrays and Classes (Upcoming chapters will provide more information on these non-primitive data types.)


Primitive Data Types

Primitive data type only describes the size and kind of variable values. It lacks any extra functions.

Java supports the following eight primitive data types:

Data TypeSizeOverview
byte1 byteStores whole numbers having a range between -128 and 127.
short2 bytesSupports the entire range of whole numbers between -32,768 and 32,767.
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647.
long8 bytesContains the whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
float4 bytesFractional numbers are stored. Enough to hold six to seven decimal digits
double8 bytesFractional numbers are stored. Enough to store 15 decimal digits
boolean1 bitHolds values for true or false.
char2 bytesA single character, letter, or ASCII values are stored.

Numbers

As far as Java data types are concerned, primitive number types can be divided into two main categories:

Unlike decimal types, integer types store whole numbers (such as 786 or -987). There are four valid types: byte, short, integer, and long.

Depending on the numeric value, you should use the appropriate type.

A floating point value represents numbers containing fractional and decimal parts. The type of data is either float or double.

Java’s most commonly used numerical types are int (for whole numbers) and double (for floating point numbers). As you read on, we will describe them all.


Integer Types

 

Type Byte:

Bytes can store whole numbers between -128 and 127. If you are certain that the value will be between -128 and 127, a byte can be used instead of an int.

Example: 

public class Main { public static void main(String[] args) { byte my_number = 110; System.out.println(my_number); } }

Example: 

public class Main { public static void main(String[] args) { byte my_number = 126; // Byte ranges from -128 to 127 System.out.println("Byte value= "+my_number); } // Output : Byte value = 126 }

Type Int:

It is possible to store whole numbers between -2147483648 and 2147483647 using the int data type. When creating variables with numeric values, the int data type is the preferred data type.

Example: 

public class Main { public static void main(String[] args) { int my_Number = 120000; System.out.println(my_Number); } }

Example: 

public class Main { public static void main(String[] args) { int my_Number = 32423000; System.out.println(my_Number); } }

Short Type:

Short data types can store whole numbers between -32768 and 32767:

Example: 

public class Main { public static void main(String[] args) { short my_ short_num = 6000; System.out.println(my_short_num); } }

Example: 

public class Main { public static void main(String[] args) { short my_short_num = 32767; // Displays the biggest possible number from the short data type range System.out.println(my_short_num); } }

Long Type:

Long data types can store whole numbers from -9223372036854775808 to 9223372036854775807. The value is stored here when int is not large enough to contain it. In Java data types, you should end the value with an “L”:

Example: 

public class Main { public static void main(String[] args) { long my_long_num = 17000000000L; System.out.println(my_long_num); } }

Example: 

public class Main { public static void main(String[] args) { long ample_long_num = 3120400000000L; System.out.println(ample_long_num); } }

Floating Point Types

In Java data types, a floating point type best suits numbers with decimals, such as 9.99 or 3.14515.

Float:

In the float data type, fractional numbers can range from 3.4e−038 to 3.4e+038. The value should be terminated with an “f“:

Example: 

public class Main { public static void main(String[] args) { float my_float_num = 7.85f; System.out.println(my_float_num); } }

Example: 

public class Main { public static void main(String[] args) { float mrx_float_num = 46.1241f; System.out.println(mrx_float_num); } }

Double:

Double data types can store fractional numbers from 1.7e−308 to 1.7e+308. In Java, you should end the value with a “d”:

Example: 

public class Main { public static void main(String[] args) { double my_Double_num = 20.29d; System.out.println(my_Double_num); } }

Example: 

public class Main { public static void main(String[] args) { double ample_Double_num = 14.5234123112312d; System.out.println(ample_Double_num); } }

Is it better to use floats or doubles?

Floating point precision refers to how many digits the value can have after the decimal point.

A float variable has a precision of only six or seven decimal digits, while double variables have a precision of about 15 digits.

Therefore, most calculations are safer when done with doubles.

Scientific Numbers:

Floating point numbers can also be scientific numbers indicating the power of 10 with an “e“:

Example: 

public class Main { public static void main(String[] args) { float float_Num = 85e4f; double double_Num = 32E5d; System.out.println(float_Num); System.out.println(double_Num); } }

Example: 

public class Main { public static void main(String[] args) { float float_mrx = 126e3f; double double_ample = 62E5d; System.out.println(float_mrx); System.out.println(double_ample); } }

Booleans

Booleans can only take true or false values and are declared with the boolean keyword:

Example: 

public class Main { public static void main(String[] args) { boolean isJavaEasy = true; boolean isJavaHard = false; System.out.println(isJavaEasy); System.out.println(isJavaHard); } }

Example: 

public class Main { public static void main(String[] args) { boolean isMatch_Tricky = true; boolean isMath_Fun =true; boolean maths_Is_My_Favourite=false; System.out.println(isMatch_Tricky); System.out.println(isMath_Fun); System.out.println(maths_Is_My_Favourite); } }

The main use of boolean values is conditional testing, which you will discover in the following chapter.


Characters

A char data type stores a single character. When it comes to Java data types, the character must be surrounded by single quotes, such as ‘A’ or ‘c’:

Example: 

public class Main { public static void main(String[] args) { char my_Final_Grade = 'A'; System.out.println(my_Final_Grade); } }

Example: 

public class Main { public static void main(String[] args) { char first_Alphabet_Of_Java = 'J'; System.out.println("First letter of word Java is: "+first_Alphabet_Of_Java); } }

You can also display certain characters using ASCII values:

Example: 

public class Main { public static void main(String[] args) { char my_char1 = 67, my_char2 = 66, my_char3 = 65; System.out.println(my_char1); System.out.println(my_char2); System.out.println(my_char3); } }

Example: 

public class Main { public static void main(String[] args) { char my_char1 = 74, my_char2 = 65, my_char3 = 86 , my_char4= 65 ; System.out.println(my_char1); System.out.println(my_char2); System.out.println(my_char3); System.out.println(my_char4); } // Output : // J // A // V // A }

 


Strings

The String data type is used to store a sequence of characters (text) in Java. In order to use string values, double quotes must be used:

Example: 

public class Main { public static void main(String[] args) { String introduction = "We are learning about java data types in this chapter"; System.out.println(introduction); } }

Example: 

public class Main { public static void main(String[] args) { String java_Intro = "Java Programming Language was founded by James Gosling at 23rd May, 1995"; System.out.println(java_Intro); } }

Some refer to String as “the special ninth type” in Java because it is so widely used.

Strings are actually non-primitive Java data types, since they refer to objects.

There are methods on the String object that can be used to perform certain operations on strings.

You don’t have to worry if you don’t understand what “object” means.

In a later chapter, we will learn more about strings and objects.


Non-Primitive Data Types

Because they refer to objects, non-primitive data types are called reference types.

Primitive and non-primitive data types differ in the following ways:

  • In Java, primitive types are predefined (already defined).
  • The non-primitive types are created by the programmer and are not defined by Java (except for Strings).
  • In contrast to primitive types, non-primitive types can call methods to perform certain operations.
  • Primitive types always have a value, while non-primitive types can be null.
  • Primitive types begin with a lowercase letter, while non-primitive types begin with an uppercase letter.
  • A primitive type’s size depends on its data type, while a non-primitive type’s size is the same.

You now know in detail what are Java data types. Strings, Arrays, Classes, Interfaces, etc., are examples of non-primitive types. A later chapter will discuss these topics in more detail.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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