After learning about Java JDK installation process and selecting our IDE for Java, Lets begin our journey for becoming a proficient Java Developer. In this Tutorial, We'll explore the most Fundamental concept of Java Programming, Variables. The most necessary basic Concept in every Programming Language.
What are Variables?
Variables are actually containers or blocks which hold a specific value assigned to them, during the execution of the Program. They hold the value until the program is terminated or until it has done all its work and printed the output. That's why, they are known as the basic unit of storage in a program. A variable is actually a name of the memory location, where we store the value.
Whenever a variable is declared, it is given a unique name in the computer memory, this unique name is known as an Identifier, They are assigned the memory address of the variable,where the original value is stored.
Identifiers
Literals
Declaring Variables
- int - stores Integer values without decimals
- char - stores characters in the form 'a', 'A', '+', '/' etc.(These single qoutes are necessory while initialising a char variable)
- float - stores floating point numbers with decimals
- double - they also store floating point numbers as float Data-type, but double has a larger Range than floats.
- String - stores text strings within double qoutes like "ProgrammerStudios", "Hello" etc.
- boolean - stores states - true or false
Naming Convention for Variables
- In Java, Variable names are case-sensitive, this means they can be any legal identifier having sequence of Unicode letters/numbers or special characters like underscore(_), or Dollar sign($), But it cannot have any special character other than $ and _(underscore), not even white spaces are allowed.
- Variable name can begin with any letter/digit or underscore/dollar sign but it is a bad practice to start the name with _ or $, avoid using these as the first character of the variable name.
- The variable name should not be a keyword or reserved word.
- By convention, the variable name should be a single word, in all small letters, but if more than one word is used in variable name, then Capitalize the first letter of the second word. For example: carSpeed, currentGear, gearRatio etc. This is the most widely used convention while naming variables, this is known as Camel-Case convention.
Types of Variables in Java
- Local variable
- Instance variable
- Static variable
Nice explanation on variables in java. Thanks for sharing.
ReplyDeletehttps://www.flowerbrackets.com/variables-in-java/
https://www.flowerbrackets.com/static-keyword-in-java/