Tuesday, October 26, 2021

Data Types in Java

Data type defines the values that a variable can take. Java is a statically typed language. A language is statically typed, if the data type of a variable is known at compile time. This means that you must
specify the type of the variable (Declare the variable) before you can use it.

Data types are divided into two groups:

  • Primitive data types - includes byte, short, int, long, float, double, boolean and char
  • Non-primitive data types - such as String, Arrays and Classes.

I. Primitive data types

§  byte, short, int and long data types are used for storing whole numbers.

§  float and double are used for fractional numbers.

§  char is used for storing characters(letters).

§  boolean data type is used for variables that holds either true or false.

byte:

This can hold whole number between -128 and 127.

Default size of this data type: 1 byte.

Default value: 0

short:

This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.

Default size of this data type: 2 byte

Int:

The int data type can store whole numbers from -2147483648 to 2147483647.

long:

Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

size: 8 bytes

Default value: 0

Float:

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f".

Ex: float myNum = 5.75f;

double:

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d".

Ex: double myNum = 19.99d;

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

char:

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

II. Non-Primitive Data Types

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

Java Strings

The String data type is used to store a sequence or array of characters (text). But in Java, a string is an object that represents an array or sequence of characters. The java.lang.String is the class is used for creating a string object.

String literals should be enclosed within double-quotes. The difference between a character array and a string is that in the string a special character ‘\0’ is present.

Basic Syntax for declaring a string in Java:

String <String_variable_name> = “<sequence_Of_Strings>” ;

Java Arrays

An Array in Java is a single object which can store multiple values of the same data type. Arrays are homogeneous data structures that store one or more values of a specific data type and provide indexes to access them. A particular element in an array can be accessed by its index.

Java Classes

A class is a collection of objects of the same type. It is a user-defined blueprint or prototype which defines the behavior or state of objects. Class and objects are the basic elements of Object-Oriented Programming that represent the real-world entities.

A class consists of a set of properties (fields or variables) or methods/operations to define the behavior of an object. 

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top