Data type types in Java

Data type types in Java

Hello, everyone, this is my first blog post and I thought I would write about data types in the Java Programming language. I was kind of confused when I first started learning Java in terms of the different data types available so this article is for beginners and experts if you want to refresh your memory. I will start by defining what a data type is and then go into detail from there. I hope you find this article useful.

What is a data type?

A data type is a classification that identifies the kind of value a variable has and the kinds of operations that can be performed on it without producing an error.

Java data types are separated into two categories:

  1. Primitive data types
  2. Non-primitive data types (reference types)

Primitive data types

Primitive data types are already predefined in Java and always have a value. A character data type, a boolean data type, two subsets of floating point numbers, and four subsets of integers are the primitive data types found in Java. The following table lists Java's primitive data types and their properties.

TypeStorageMin ValueMax Value
byte8 bits-128127
short16-32,76832,767
int32 bits-2,147,483,6482,147,483,647
long64 bits-9,223,372,036,854,775,8089,223,372,036,854,775,807
float32 bitsAproximately -3.4E+38 with 7 significant digitsApproximately 3.4E+38 with 7 significant digits
double64 bitsApproximately -1.7E+308 with 15 significant digitsApproximately -1.7E+308 with 15 significant digits

Floating points and Integers

Floating points and integers, which both have a fractional component, are the two fundamental types of numeric values in Java. There are two floating point data types (float and double) and four integer data types (byte, short, int, and long). The range of values that can be represented depends on how much memory is required to store a value of that type, which varies for each of the numeric kinds. All the numeric values can store positive and negative values.

A variable’s data type needs to be chosen accordingly to the appropriate size so that memory space is not wasted. This happens when memory space is severely constrained, such as when software is running on an embedded device. On the other hand, one should leave a decent—or even generous—amount of space when it's unclear what the range of a specific variable will be.

A literal is an explicit data value used in a program. Unless an L or l is added to the end of the number to indicate that it should be regarded as a literal of type long, such as 60L, Java assumes that all integer literals are of type int. Numeric literals of type double can be followed by a D or d if desired. Similarly, Java takes the position that all floating point literals are of type double. If a floating point literal must be treated as a float, an F or f is added to the end of the value, as in 4.758F or 100.45F. The following are examples of numeric variable declarations in Java:

int quantity = 42;
byte number1, number2;
long countedStars = 86827263927L;
float ratio = 0.1373F;
double delta = 453.523311903;

Character

A single character is represented by the data type char. Java programs use single quote marks to indicate character literals, such as "c," "f," or ";". A character set, which is simply a list of characters in a specific order, establishes the characters that can be managed. A common option is the ASCII (American Standard Code for Information Interchange) character set.

Here are a few instances of Java character variable declarations:

char topGrade = 'A';
char symbol1, symbol2, symbol3;
char terminator = ';', separator = ' ';

Boolean

A boolean variable can be used to describe any scenario that has two states, such as a light being on or off, as well as to show if a specific condition is true. Only true and false are acceptable values for boolean values, which are defined in Java using the reserved word boolean. Java reserves the use of the words true and false as boolean literals, and they are not permitted in other contexts. A boolean value cannot be changed into another data type, and vice versa for any other data type.

Here are some instances of Java declarations for boolean variables:

boolean flag = true;
boolean tooHigh, tooSmall, tooRough;
boolean done = false;

Non-primitive data types

Because they make references to objects, non-primitive data types are sometimes known as reference types. Java does not specify non-primitive types; instead, they are developed by the programmer (except for String). They can be utilized to invoke methods to carry out particular activities. Strings, Arrays, Classes, Interfaces, etc are non-primitive types examples. The image below depicts some Java non-primitive data types.

Non-primitive type.png

String

Text is stored using strings. The characters in a String variable are enclosed in double quotations.

String name = "Seho";

A String object cannot have its value increased or decreased, nor can any of its characters be changed once it has been created. As a result, we refer to a String object as immutable. However, a number of the String class's methods produce new String objects that have had the value of the original string modified. A Java string object is created using the java.lang.String class.

Array

A list of values is kept in an object called an array. As opposed to defining distinct variables for each value, arrays are used to hold numerous values in a single variable. Arrays offer indexed access to hold one or more values of a certain data type. The index of an element in an array is used to access that element. The following code shows how to declare an array.

Define the variable type with square brackets to declare an array:

String[] fruit;

The kind of values that an array can store is referred to as the element type. A specific array's values are all of the same type, or at the very least, compatible types. As a result, an array may have an array of integers, boolean values, and so on.

In the next example, a string array-containing variable is declared. When adding values to it, use an array literal and enclose the values in curly brackets with commas:

String[] fruit = {"Banana", "Orange", "Apple"};

Class

Java is an object-oriented programming language. This indicates that objects are used as the main source to carry out what the code specifies. Implementing real-world concepts like inheritance, polymorphism, etc. in programming is the goal of object-oriented programming.

In Java, a class is a template for building objects and defining their data types and functions.

The following is an example of a class

public class Dog {
   String name;
   int age;
   String color;

   void meowing() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

The following variable types can be found in classes.

  • Local variables: These are variables that are defined within methods, constructors, or blocks.

  • Instance variables: These variables are declared in a class but not within constructors, methods, or blocks.

  • Class variables: A variable that is declared in a class but not within constructors, methods, or blocks.

Interface

An interface is a group of abstract methods that define a range of possible ways to interact with an object.

Use the interface keyword to declare an interface. It is employed to offer complete abstraction. By default, all fields in an interface are public, static, and final, and all methods in an interface are declared with an empty body. All of the methods declared in an interface must be implemented by a class that implements the interface. Use the keyword implements to implement an interface.

interface Maseho
{
    final int id = 10;
    int move();
}

An object reference variable may be declared using an interface name. Any object of any class that implements the specified interface may be referred to by an interface reference.

Conclusion

That's all for this article, I hope you found it helpful and gives you an understanding of data types in Java.