Variables and data types in Java

Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, string, etc.), we are storing. After it a fixed memory is assign to each variable. For example, normal integer is assign 4 bytes. We can not assign other type of values to defined variables. For example, if we have defined a variable as integer then we can not assign a text string to that variable. We can modify that variable with different value of same type (say another integer). In java, there are 8 types of primitive variables.

  1. Integer
  2. Byte
  3. Char
  4. Boolean
  5. Double
  6. short
  7. long
  8. float


  1. Run below examples by writing in main method. To print them in console use System.print.out("variable");
  2. We can give variables any name other than reserved keywords starting with letter, $ or underscore (_).


Integer is a 32-bit data type which stores integer values from range - 2,147,483,648 ( -2^31 ) to 2,147,483,647(inclusive) ( 2^31 -1 ).


  1. int a = 8;
  2. a = 10;


It is 8-bit data type used to store integer value from -128 to 127 (inclusive).


  1. byte a = 23;
  2. a = -24;


It is 16-bit unicode character which stores any character.


  1. char a = 'a';
  2. a = 'z';

NOTE: Character is written inside single colons '' but string is written in double colons "".


It is one bit boolean data type which has only two values: true or false.

  1. boolean a = true;
  2. a = false;


Double data type is a double-precision 64-bit IEEE 754 floating point. This should not be used for precise values such as currency.

  1. double a = 56;
  2. a = 45.98;


Short data type is a 16-bit signed two's complement integer. Its range is from -32,768 to 32,767 (inclusive).


Long data type is a 64-bit signed two's complement integer. Its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). It is used when we need wider range for integers.

  1. int a = 200000L;
  2. a = -300000L;


Float data type is a single-precision 32-bit IEEE 754 floating point. Float data type is also never used for precise values such as currency.

More types:


String is an array of characters. We will read about array in detail later. At this moment you may assume string is a collection of characters. It is simple text.

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250