Java Datatypes

JAVA DATATYPES

                                   Java, one of the most widely used and versatile programming languages, is known for its robust features and performance. One of the foundational elements of Java programming involves understanding its data types.

                                      In computer programming, a data type is an indication of the kind of value a variable can hold. It tells the compiler or interpreter how to interpret the bits that are associated with that variable. Understanding data types is crucial as they dictate what operations can be performed on variables, the amount of memory allocated, and how data is stored and manipulated.

Types of Data Types in Java:

                     Java categorizes its data types into two main categories: 

1)Primitive Data Types 

2)Reference Data Types or Non-Primitive Datatypes.

 


1)Primitive Datatypes:

              Primitive data types are the simplest forms of data types. They include a predefined set of values and are stored directly in memory.

             Java has eight primitive data types:

byte: 

Size: 8 bits (1 byte)

Range: -128 to 127

Used for saving memory in large arrays, primarily in place of integers.

Ex:- byte age = 25;

short: 

Size: 16 bits (2 bytes)

Range: -32,768 to 32,767

Suitable for saving memory in large arrays, especially when the memory savings matter.

Ex:- short population = 15000;

int: 

Size: 32 bits (4 bytes)

Range: -2,147,483,648 to 2,147,483,647

The most commonly used data type for integers.

Ex:- int salary = 50000;

long: 

Size: 64 bits (8 bytes)

Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Suitable when dealing with very large integers or when more precision is needed.

Ex:-  long d = 100000L;

float: 

Size: 32 bits (4 bytes)

Range: Approximately ±3.40282347E+38

 Used for single-precision floating-point calculations where a reasonable amount of precision is required.

Ex:- float e = 5.5f;

double: 

Size: 64 bits (8 bytes)

Range: Approximately ±1.79769313486231570E+308

 Preferred for double-precision floating-point calculations providing more accuracy.

Ex:- double f = 10.99;

char: 

Size: 16 bits (2 bytes)

Range: 0 to 65,535 (Unicode characters)

 Used for storing single 16-bit Unicode characters.

Ex:- char g = 'A';

boolean: 

Size: 1 bit (not precisely defined)

Values: true or false

 Used for simple flags that track true/false conditions.

Ex:- boolean h = true;

2) Reference Data Types:

               Reference data types, in contrast to primitive data types, refer to objects. They do not store the actual data but rather the reference, or memory address, where the data is located. 

              The main characteristics of reference data types include:

Object:

An object is an instance of a class that contains data (variables) and behavior (methods).

*  Syntax:-
ClassName obj = new ClassName();
*  Example:-
class Student {
int id;
}

Student s1 = new Student();
s1.id = 101;

Array:

An array is a collection of elements of the same data type stored in contiguous memory locations.

*  Syntax:-
int arr[] = new int[5];
*  Example:-
int arr[] = {10, 20, 30, 40};
System.out.println(arr[0]); // 10

Strings:

A String is a sequence of characters used to represent text,

*  Syntax:-
String s = "Hello";
*  Example:-
String name = "Bhavya";
System.out.println(name.length()); // 6

 User-Defined Classes:

  A user-defined class is a class created by the programmer to define custom data types.

*  Syntax:-
class Student {
int id;
String name;
}
*  Example:-
class Student {
int id;
String name;
}

Student s = new Student();

s.id = 1;
s.name = "Bhavya";

Example program for Primitive datatypes :















Example program for Reference Datatype:































Comments

Popular posts from this blog

Java Introduction