Learn about Java Datatypes and how we use them

Learn about Java Datatypes and how we use them

A data type (or Java datatypes) is a category of potential values and a set of permitted actions in computer science and computer programming. The compiler or interpreter can identify the data types on how the programmer intends to use them.

Most programming languages allow the fundamental data types of characters, Booleans, floating-point numbers, and integer numbers (of various sizes). The multiple values that an expression, such as a variable or a function, might take are limited by the data type.

The meaning of the data, the operations that can be carried out on it, and the techniques for storing values of that type are all determined by the data type.

What are Variables?

Identifiers with associated values are called variables. The variable’s type and name are written in the declaration, and it is optional to initialize the variable by assigning a value in the same sentence. Using a comma as a delimiter, many variables of the same type can be declared and initialized in a single sentence. Before, using a variable you need to declare the variable as it is necessary. 

Rules for writing Variables

  • Every identifier must begin with the letter A to Z or a to z currency character $ or an _ (underscore).
  • Variables should be named on the terms of the subject area, so the purpose should be clear by reading the variable name.
  • Remove the spaces between the words to create variable names. All words in the name should be capitalized, even prepositions and pronouns with just one letter. Eg: variable DirectoryProcessingDialog; variable PackInBoxQuantity; FirstProgramme etc.
  • Never start a variable with an underscore.
  • Always write a variable that contains more than a single character, only loop counters are allowed to use a single character.
  • After the state that corresponds to the “true” value, name the variables that denote binary states (“true” or “false”).

Few Examples

Examples of variables are (“java type” and “ variable name”) we will learn about Java Data Types after this topic below:

int MyNum = 5 ;
float MyFloatNum = 5.99f ;
char MyLetter = ‘D’ ;
boolean MyBool = true ;
String MyText = “Hello” ;

Also read, Why Java is so popular among Developers!

What are data types?

A set of values and a set of operations that are defined on those values make up a data type. Java has huge reference type libraries that are designed for a wide range of applications to supplement the primitive data types you have been using.

They are used to support the variable as they store value or data as being named “Data Types” eg int, float, String, etc.

Types of data types

The datatypes of Java are divided into two categories “primitive datatypes” and “non-primitive datatypes”. The primitive datatypes store byte, short, int, long, float, double, boolean, and char. The non-primitive datatypes contain values such as Strings, Arrays, and classes. I will be explaining it all on this page and you will get a basic idea of data types.

Primitive Data Types

Primitive data types contain the variable and the size of the value and have no additional methods there are 8 types of primitive data types; as follows:-

  1. byte – byte has a size of byte and stores a value of whole numbers from -128 up to 127.
  2. short – It has a size of 2 bytes and stores values of whole numbers from -32,768 to 32,767.
  3. int – The int stores has size of 4 bytes and has value stored from -2,147,483,648 to 2,147,483,647. 
  4. long – The long datatypes has a size of 8 bytes and stores value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  5. float – float is used for decimal values and can store values up to 6 – 7 decimal digits and the size is 4 bytes.
  6. double – double is used for storing large decimal values as it is sufficient enough to store values up to 15 decimal digits.
  7. boolean – boolean stores only true and false values and the size is 1 byte.
  8. char – The char data type only stores ASCII values or single characters/letters.

Also read, Understanding Array, String, and Object Data Structure with JavaScript

Non-Primitive Data Types

As non-primitive data types refer to objects they are also known as reference types.

  • Primitive types cannot invoke methods to carry out specific actions, whereas non-primitive types can.
  • In Java, primitive types are already specified or predefined. The non-primitive type is not specified in Java, instead, the programmers develop it.
  • In contrast to non-primitive types, which can be null, primitive types always have a value.
  • While non-primitive types begin with an uppercase letter, primitive types begin with a lowercase letter.
  • While non-primitive types all have the same size, the size of a primitive type varies depending on the data type.
  • Strings, Arrays, Classes, Interfaces, and other non-primitive types are examples.

Different types of Primitive Data Types

The primitive types have been divided into two parts: integer types and floating point types.

The integer types

It doesn’t contain decimal values and only stores whole numbers, whether they are positive or negative (such as 123 or -456). byte, short, int, and long are all acceptable types. The appropriate kind depends on the numerical value.

Floating Types

The floating point types represent fractional parts of a number that contain one or more decimals; float( eg:5.75f ), and double( eg:19.99d ).

Note: As there are a large number of numeric data types in Java, int for whole numbers and double for decimal numbers they are the most used numbers.

Also read, Implement Star Rating Widget using HTML, CSS and JavaScript | The DOM Challenge

The Integer Types

Byte 

Whole numbers between -128 and 127 can be stored using the byte data type. When you know the number will be between -128 and 127, use this instead of int or another integer type to conserve memory.

class Sample {
    public static void main(String[] args) {
        byte MyNum = 100;
        System.out.println(MyNum);
    }
}

Short

From -32768 to 32767, the short data type can store full numbers.

class Sample {
    public static void main(String[] args) {
        short MyNum = 100;
        System.out.println(MyNum);
    }
}

Int

The range of whole numbers that the int data type may hold is -2147483648 to 2147483647. The int data type is the ideal data type when creating variables with a numeric value in general and in our course.

class Sample {
    public static void main(String[] args) {
        int MyNum = 10000;
        System.out.println(MyNum);
    }
}

Long

The range of whole numbers that the int data type may hold is -2147483648 to 2147483647. The int data type is the ideal data type when creating variables with a numeric value in general and in our course.

class Sample {
    public static void main(String[] args) {
        long MyNum = 150000000L;
        System.out.println(MyNum);
    }

Also read, Learning to Think Like a Computer

Floating Point Types

Anytime you require a number with a decimal, such as 9.99 or 3.14515, you should use a floating point type.

Fractional numbers are able to be stored in the float and double data types. Keep in mind that the value should end with an “f” for floats and a “d” for doubles:

class Sample {
    public static void main(String[] args) {
        float MyNum1 = 12.5f;
        System.out.println(MyNum1);
        double MyNum2 = 19.99d;
        System.out.println(MyNum2);
    }
}

Use “float” or “double” The number of digits a floating point value can have after the decimal point is determined by its precision. While double variables have a precision of roughly 15 digits, float variables only have six or seven decimal digits of precision. Therefore, for the majority of calculations, double is safer.

Scientific Numbers

A scientific number with an “e” to denote the power of 10 can also be a floating point number:

class Sample {
    public static void main(String[] args) {
        float  f1 = 35e2f;
        double d1 = 12E4d;
        System.out.println(f1); // 3500.0
        System.out.println(d1); // 120000.0
    }
}

Also read, How to make money through coding as a teenager?

Boolean Types

When the boolean keyword is used to declare a data type, it can only accept the values true or false.

The main application of boolean values is conditional testing, which is covered in greater detail in a later chapter.

class Sample {
    public static void main(String[] args) {
        boolean IsJavaFun = true;
        boolean IsFishTasty = false; 
        System.out.println(IsJavaFun);
        System.out.println(IsFishTasty);
    }
}

Java Characters

A single character is stored using the char data type. A or c must be used as single quotations around the character.

class Sample {
    public static void main(String[] args) {
        char MyGrade = 'B';
        System.out.println(MyGrade);
    }
}

As an alternative, you can show some characters using ASCII values if you are familiar with them.

class Sample {
    public static void main(String[] args) {
        char MyVar1 = 65, MyVar2 = 66, MyVar3 = 67;
        System.out.println(MyVar1);
        System.out.println(MyVar2);
        System.out.println(MyVar3);
    }
}

Strings

A series of characters are stored using the String data type (text). Double quotes must be used to enclose string values:

Java’s String type is so prevalent and well-integrated that some refer to it as “the special ninth type.”

Because it refers to an object, a String in Java is actually a non-primitive data type. There are methods available on the String object that are used to manipulate strings in various ways. If you’re still not sure what an “object” is, don’t worry. In a subsequent chapter, we will learn more about objects and strings.

class Sample {
    public static void main(String[] args) {
        String greeting = “Hello World”;
        System.out.println(greeting);
    }
}

Data types specifications

Data Types Default Size Range
boolean false 1 bit true, false
byte 0 8 bit -128 to 127
short 0 16 bits -32768 to 32767
char \u0000 16 bits Character representations of ASCCI values (0 to 255)
int 0 32 bit -2147483648 to 2147483647
long 0 64 bit -9223372036854775808 to 9223372036854775807
float 0.0 32 bit up to 7 decimal digits
double 0.0 64 bit up to 16 decimal digits

Also read, An Easy Explanation of JavaScript Closure

Data Type Casting

When you assign a value from one primitive data type to another type, this is known as type casting.

There are two varieties of casting in Java:

Widening Casting

Widening Converting (automatically) from smaller type to larger type size is known as casting.

byte —> short —> char —> int —> long —> float —> double

When moving from a smaller size type to a larger size type, widening casting occurs automatically. When moving from a smaller size type to a larger size type, widening casting is automatically performed: size category.

class Sample {
    public static void main(String[] args) {
        int MyInt = 9;
        double MyDouble = MyInt; // Automatic casting: int to double
        System.out.println(MyInt); // 9
        System.out.println(MyDouble); // 9.0
    }
}

Narrowing Casting

Converting a larger type to a smaller size type using a manual process known as narrowing casting.

double —> float —> long —> int —> char —> short —>  byte

By adding the type in parentheses in front of the value, narrowcasting must be done manually.

class Sample {
    public static void main(String[] args) {
        double MyDouble = 9.78d;
        int MyInt = (int)MyDouble; // Explicit casting: double to int
        System.out.println(MyDouble); // 9.78
        System.out.println(MyInt); // 9
    }
}

Also read, Writing Your First Program in Java

Final Words

The variables are referred to as the nouns of computer language and data types hold on to the values in it. You will be mostly using int, String, and float/double (using double would be a better choice as it stores large decimal values).

The primitive data types are the most used by programmers. This is all about variables and data types. I have explained each of the data types so that you could get a brief idea so that you can memorize them and we will be using and learning these in the next classes.