Java - Data Types

Java Data Types

Java is statically typed and also a strongly typed language because, in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the Java data types.

Java has two data types,

    1)  Primitive data types.

    2)  Non-Primitive data types.

Primitive data types.

Primitive data are only single values and have no special capabilities. There are 8 primitive data types.

    1. boolean

  • The boolean data type represents a logical value that can be either true or false.

    2. byte

  • The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.

    3. short

  • The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.

    4. int

  • It is a 32-bit signed two’s complement integer.

    5. long

  • The range of a long is quite large. The long data type is a 64-bit two’s complement integer and is useful for those occasions where an int type is not large enough to hold the desired value.

    6. float

  • The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.

    7. double

  • The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays. (ex: 5.5)

    8. char

  • The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.


Non-primitive data types.

    1. String

  • Strings are defined as an array of characters. The difference between a character array and a string in Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char-type entities.

    2. Class

  • A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.

    Ex: Modifires, Class name, Superclass, Interfaces, Body{}

    3. Object

  • An Object is a basic unit of Object-Oriented Programming and represents real-life entities.

    Ex: State, Behavior, Identity

    4. Interface

  • Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

    5. Array

  • An Array is a group of like-typed variables that are referred to by a common name.


Summary 

Understanding Java’s data types is fundamental to efficient programming. Each data type has specific use cases and constraints, making it essential to choose the right type for the task at hand. This ensures optimal memory usage and program performance while leveraging Java’s strong typing system to catch errors early in the development process.




Comments

Popular posts from this blog

Java Language - Object Generating & Objects / Java classes

Introduction of Java Programming Language

Java Standards