Java Programming

Wednesday, May 29, 2019

Learn Variables and Data Types in Java

There are two data types in Java:
  • Primitive Data Type
  • Reference/Object Data Type

List of all primitive data types in Java:

  • byte (number, 1 byte)
  • short (number, 2 bytes)
  • int (number, 4 bytes)
  • long (number, 8 bytes)
  • float (float number, 4 bytes)
  • double (float number, 8 bytes)
  • char (a character, 2 bytes)
  • boolean (true or false, 1 byte)

Syntax

How to declare and assign a primitive data type in Java:
type vairable_name;
vairable_name = value;
Or
type vairable_name1, vairable_name2;
variable_name1 = value
variable_name2 = value; 
Or
type vairable_name = value;

byte

  • Byte data type is an 8-bit signed two’s complement integer.
  • Minimum value is -128
  • Maximum value is 127
  • Default value is 0
  • Byte data type is used to save memory in large arrays, mainly in place of integers because byte is four times smaller than an int.
How to declare and assign byte number in Java:
byte yourByte = 100;
An example with Eclipse:

short

  • Short data type is a 16-bit signed two's complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Default value is 0
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer.

How to declare and assign int number in Java:

short yourShort = 1000;

An example with Eclipse:
long
  • Long data type is a 64-bit signed two's complement integer
  • Minimum value is -9,223,372,036,854,775,808(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
  • Default value is 0L
  • This type is used when a wider range than int is needed

How to declare and assign long number in Java:
long yourLong = 1000L;

An example with Eclipse:

 int

  • Int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648 (-2^31)
  • Maximum value is 2,147,483,647(inclusive) (2^31 -1)
  • The default value is 0
  • Integer is generally used as the default data type for integral values unless there is a concern about memory.
How to declare and assign int number in Java:
int yourInt = 2;
An example with Eclipse:

float

  • Float data type is a single-precision 32-bit IEEE 754 floating point
  • Default value is 0.0f
  • Float is mainly used to save memory in large arrays of floating point numbers
How to declare and assign float number in Java:
float yourFloat = (float) 4.4;

Or
float yourFloat = 4.4f;

An example with Eclipse:

double

  • double data type is a double-precision 64-bit IEEE 754 floating point
  • Default value is 0.0d
  • This data type is generally used as the default data type for decimal values
How to declare and assign double number in Java:
double yourDouble = 3.3;

An example with Eclipse:

char

  • char data type is a single 16-bit Unicode character
  • Minimum value is '\u0000' (or 0)
  • Maximum value is '\uffff' (or 65,535 inclusive)
  • Char data type is used to store any character
How to declare and assign char or a character in Java:
char yourChar = 'c';
An example with Eclipse:

boolean

  • boolean data type represents one bit of information
  • There are only two possible values: true and false
  • This data type is used for simple flags that track true/false conditions
  • Default value is false
How to declare and assign boolean in Java:
boolean yourBoolean = false;
yourBoolean = true;
An example with Eclipse:

List of all object data types in Java:


No comments:

Post a Comment