What Is Data Types and Its Types In Java? - News4u95 - A Part of Your Everyday Life

Breaking

Sunday, June 5, 2022

What Is Data Types and Its Types In Java?

What Is Data Types and Its Types In Java?

Data Types In Java

Data types specify the different sizes and values that can be stored in the variable.

What Is Data Types and Its Types In Java?
What Is Data Types and Its Types In Java?
 

Types of Data Types In Java

a. Primitive Data Types
  1. byte
  2. short
  3. int 
  4. long
  5. float
  6. double
  7. boolean
  8. char
 
b. Non-Primitive Data Types
  1. String
  2. Array
  3. Class 
  4. Interface

 

a. Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java.

 

1.byte

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127.

Example: 

byte myNum = 100;

System.out.println(myNum);

 

2. short

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

Example:

short myNum = 5000;

System.out.println(myNum);

 

3. int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

Example:

int myNum = 100000;

System.out.println(myNum);

 

4. long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L".

Example

long myNum = 15000000000L;

System.out.println(myNum);

 

5. float

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

Example:

float myNum = 5.75f;

System.out.println(myNum);

 

6. double

The float and double data types can store fractional numbers. Note that you should end the value with an "f" for floats and "d" for doubles:

Example

double myNum = 19.99d;

System.out.println(myNum);

 

7. boolean

A boolean data type is declared with the boolean keyword and can only take the values true or false:

Example:

boolean isJavaFun = true;

boolean isFishTasty = false;

System.out.println(isJavaFun);     // Outputs true

System.out.println(isFishTasty);   // Outputs false

 

8. char

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

Example:

char myGrade = 'B';

System.out.println(myGrade);

 

b. Non-Primitive Data Types

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

 

1. String

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:

Example:

String greeting = "Hello World";

System.out.println(greeting);

 

2. Array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};

 

3. Class

Class is a collection of multiple statements.

Example:

public class Main {

  int x = 5;

}

 

4. Interface

Another way to achieve abstraction in Java, is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with empty bodies:

Example:

// interface

interface Animal {

  public void animalSound(); // interface method (does not have a body)

  public void run(); // interface method (does not have a body)

}

 

The main difference between primitive and non-primitive data types are:

Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).

Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.

A primitive type has always a value, while non-primitive types can be null.

A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

The size of a primitive type depends on the data type, while non-primitive types have all the same size.

 

 Read More Articles: What Is The Local Variable And Global Variable In Java?

No comments:

Post a Comment