Datatypes in java

Java is rich in data types which allows the programmer to select the appropriate type needed to store variables of an application.
Every variable in Java has a data type which tells the compiler what type of variable it as and what type of data it is going to store.
It specifies the size and type of values.
Information is stored in a computer memory with different data types.
Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold.

datatypes available in java

Primary Data Type

Java supports 8 primitive data types: byte, short, int, long, float, double, char and boolean.

Non-Primitive Data Types

Classes, Interface, Arrays etc.

Integer Types

It can hold whole numbers such as 196, -52, 4036 etc. Java supports four different types of integers These are:
TypeContainsDefaultSizeRange
byteSigned integer08 bit or
1 byte
-27 to 27-1 or
-128 to 127
shortSigned integer016 bit or
2 bytes
-215 to 215-1 or
-23,768 to 32767
intSigned integer032 bit or
4 bytes
-231 to 231-1 or
-2147,483,648 to 2147,483,647
longSigned integer064 bit or
8 bytes
-263 to 263-1 or
-9223,372,036,854,755,808 to 9223,372,036,854,755,807

Rational Numbers

It is used to hold whole numbers containing fractional part such as 36.74, or -23.95 (which are known as floating point constants). There are two types of floating point storage in java. These are:

TypeContainsDefaultSizeRange
floatIEEE 754 floating point
single-precision
0.0f32 bit or
4 bytes
±1.4E-45 to
±3.40282347E+38F
doubleIEEE 754 floating point
double-precision
0.064 bit or
8 bytes
±439E-324 to
±1.7976931348623157E+308

Characters

It is used to store character constants in memory. Java provides a character data type called char whose type consumes a size of two bytes but can hold only a single character.

TypeContainsDefaultSizeRange
charUnicode character
unsigned
\u000016 bits or 2 bytes0 to 216-1 or
\u0000 to \uFFFF

Conditional

Boolean type is used to test a particular condition during program execution. Boolean variables can take either true or false and is denoted by the keyword boolean and usually consumes one byte of storage.

TypeContainsDefaultSizeRange
booleantrue or falsefalse1 bittrue or false

Example:

class DataTypes{
public static void main(String args[]){
byte byteVar = 8;
short shortVar = 30;
int intVar = 40;
long longVar = 80;
float floatVar = 25.0;
double doubleVar = 30.123;
boolean booleanVar = true;
char charVar ='P';
System.out.println("Value Of byte Variable is " + byteVar);
System.out.println("Value Of short Variable is " + shortVar);
System.out.println("Value Of int Variable is " + intVar);
System.out.println("Value Of long Variable is " + longVar);
System.out.println("Value Of float Variable is " + floatVar);
System.out.println("Value Of double Variable is " + doubleVar);
System.out.println("Value Of boolean Variable is " + booleanVar);
System.out.println("Value Of char Variable is " + charVar);
   }
  } 

Output:

Value Of byte Variable is 8
Value Of short Variable is 30
Value Of int Variable is 40
Value Of long Variable is 80
Value Of float Variable is 25.0
Value Of double Variable is 30.123
Value Of boolean Variable is true
Value Of char Variable is P

operators in java

There are many types of operators available in Java such as:
Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators and Misc Operators.

Arithmetic Operators

OperatorDescription
+
Addition
Subtraction
*
Multiplication
/
Division
%
Modulus
++
Increment
−−
Decrement

Relational Operators

OperatorDescription
==
Is equal to
!=
Is not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to

Logical Operators

OperatorDescription
&&
And operator. Performs a logical conjunction on two expressions.
(if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False)
||
Or operator. Performs a logical disjunction on two expressions.
(if either or both expressions evaluate to True, result is True)
!
Not operator. Performs logical negation on an expression.

Bitwise Operators

OperatorDescription
<<
Binary Left Shift Operator
>>
Binary Right Shift Operator
>>>
Shift right zero fill operator
~
Binary Ones Complement Operator
&
Binary AND Operator
^
Binary XOR Operator
|
Binary OR Operator

Assignment Operators

OperatorDescription
=
Assign
+=
Increments, then assigns
-=
Decrements, then assigns
*=
Multiplies, then assigns
/=
Divides, then assigns
%=
Modulus, then assigns
<<=
Left shift and assigns
>>=
Right shift and assigns
&=
Bitwise AND assigns
^=
Bitwise exclusive OR and assigns
|=
Bitwise inclusive OR and assigns

Example:

import java.util.*;
import java.lang.*;
import java.io.*;

public class arithop {
 public static void main(String[] args) {
  int number1 = 6;
  int number2 = 8;
  int sum = number1 + number2;
  int dif = number1 - number2;
  int mul = number1 * number2;
  int quot = number1 / number2;
  int rem = number1 % number2;
  
  System.out.println("number1 is : " + number1);
  System.out.println("number2 is: " + number2);
  System.out.println("Sum is: " + sum);
  System.out.println("Difference is : " + dif);
  System.out.println("Multiplied value is : " + mul);
  System.out.println("Quotient is : " + quot);
  System.out.println("Remainder is : " + rem);
 }
}
OperatorDescription
Conditional(Ternary) Operator ( ? : )Operator is used to decide which value should be assigned to the variable.
instanceOf Operator
Object reference variables

Programs to Show How Conditional Operator Works

Example:

import java.util.*;
import java.lang.*;
import java.io.*;

public class optexp {
 public static void main(String args[]) {
  int a, b;
  a = 10;
  b = (a == 1) ? 40 : 60;
  System.out.println("Value of b is : " + b);
  b = (a == 10) ? 20 : 40;
  System.out.println("Value of b is : " + b);
 }
}