Scanner class Methods in Java

Java- Datatypes

Data Types

Data Types are used to determine how much memory is allocated to store data and what operations can be performed on it. It is used to allocate the memory location of a particular variable
Data Types are keywords that inform compiler about the type of data we are using.
For example,  We want to print age on screen then for age we will use “int” datatype. Here ,“int” is for storing integer value and will occupy 4 bytes of memory.

Data Types are divided into following categories:

DataTypes in Java

  1. Primitive Types:-  Primitive types are also known as Built-in datatypes. These datatypes allocate memory to actual variable. The primitive type variable contains the actual value of the variable. For example, "int a=5;" It means a variable which is integer type contains the actual value of variable as 5.

       Primitive datatypes are divided into two broad categories: 1)Numeric Types 2)Boolean Types

Numeric Types: Numeric types are the datatype which is used to allocate memory to numerical data.It can allocate memory to  integer, character, floating point numbers. Integers and character types are grouped together in Integral types and Floating point types is the other group. You will understand them in below.These are further divide into two types: 1)Integral  types 2)Floating-point Types
  • Integral Types:- Integral types consists of integer values and character values.Lets understand the integer type first then character.
1.Integer Type:- Integer types are used to store whole numbers without fractional parts.It can store both positive and negative numbers,unlike C++, Java does not support unsigned integers. Integers are of four types as below:  
  • byte:- It has the smallest size.It uses  8-bits(1 byte) to store integer data.Sometime, we need small datatypes to store small values.It can store the integers ranging from -128 to +127.It can be declared as  ,  byte num; //num is variable name 
  • short:-It is bigger then byte but still small in size.It uses 16-bits(2 bytes) to store integer data.Sometime, we need small datatypes to store small values.It can store the integers ranging from -32768 to +32767.It can be declared as , short num;
  • int:- It is the most common data type to store integer value. It uses 32-bits(4 bytes) to store data. It is standard datatype for integers. It can store integer values ranging from -2,147,483,468  to +2,147,483,467.It can be declared as , int num;
  • long:- It is used to store biggest value of integers . It used 64-bits (8 byes) to store the integer value.It ranges from  -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.It can be declared as , long num;

2.Character Type:- Character types are used to store Unicode characters. It can store letter,alphabets or whatever we want to write as a character.We can write up to 65536 different characters in Java.It has size of 16-bits (2 bytes)Character is written as follow:

  • char:- char uses 16-bits (2 bytes) to store a character data.It stores single Unicode character.It can store up-to 65536 different Unicode characters. For example, we want to store "a" in a char type variable. We will use following code: -    char choice='a';
3.Floating-Point Types:- Floating-Point types are used to store decimal numbers.It stores  numbers with fraction parts.For example,3.12,4.00,5.0,6.5 etc. Floating pint types are further divided into two types: 
  • float:- It is use to store floating-point values ranging from -3.4 E 38 (E is for exponential) to +3.4 E 38 .Its size is 32-bit (4 bytes). It can store 7 precision values. Precision values means the digits after the decimal point. Example: float fnum;
  • double:-  It is use to store floating-point values ranging from -1.7 E 308 (E is for exponential) to +1.4 E 308 .Its size is 64-bit (8 bytes). It can store 15 precision values. Example: double dnum;
Boolean Types:- Boolean type is used to store boolean values. It is used to represent true or false value.It got its name from George Boole, an English Mathematician who invented the concept of Boolean Algebra.It has only one type as follows: 
  • boolean:- boolean is datatype used to store only one value. It can have any of value form true or false.It is mainly used to check the conditions.Like, if the number is 2 return true else return false.It has the size of 1-bit only. Example:  boolean flag;
       2.Non-Primitive Types:- Non-primitive data types are also known as Reference type or derived type datatypes. These data types are derived from the primitive datatypes. In non-primitive datatypes the memory is allocated to the reference or address of the actual value instead of actual value itself. It means when we store a value in reference type , it will store the address of the variable instead of its value. For example, if we want to store 10 values we can create array which store 10 integer values by allocating them contagious memory location. The array name stores the address of each value. We use single variable to store 10 value.So, it will allocate some address to the n sized array. Like,  int a[10] is an integer array ,it will store 10 values at contagious memory locations , we can access each value using index from 0-9.

a)Array:- Array is a homogeneous group of data.It can store various different values in contagious memory locations.Array can be of any type, to store integer values we need integer type array or to store floating point values we need float array. Example, declare array to store integer values, int[] a;

b)String:- String is new datatype to create string of characters. It have methods that can perform various operations on strings.We'll discuss it later.

c)class:- class is the main part of java program.Without class we can not create java program.class is a datatype keyword,otherwise compiler will not understand that its a class.So we must declare class using class keyword. To understand classes,click here. 

Non-primitive datatypes require a dedicated post to understand them. We will discuss about non-primitive datatypes in future posts. You must have knowledge of these data types to become a good programmer.You must know how and when to use a datatype and which datatype will be efficient to use.Stay tuned!

Comments