Java Variables

Java variable is the name of memory locations where information is stored, simply we can say “ variable is the name of memory location”.

Java Variables

Since computer memory (RAM) is divided in small – small memory units and these memory units are called as cell. Each cell is marked with a unique number and this number is called as address of cell. Information is stored in cell or groups of cell for example if a cell size is 1 byte and information size is 15 byte then to store this information a group of 15 cell is required.

Variable Syntax

data_type variable_name = value;

Example

int age = 30;

Variable Data Type

It tells which kind of value a variable can store, for example int, char, float, string, double etc. We will learn about data type in detail in next chapter.

Variable Name

It is user defined name, its totally depends on developer what he want to name a variable. There are few naming convention rules just need to follow them while naming a variable. The rules are listed below.

  • A java keyword cannot be used as variable name.
  • A variable name can contain numbers, alphabets, and two symbols _ and $.
  • A variable name cannot start from a number.
  • A variable name can starts from alphabets.
  • A variable name can be starts from symbols _ and $.
  • Space is not allowed in variable name.
  • Variable name is case sensitive for example age and Age both are different.

Variable Value

It is the value which a variable will store, Type of this value must be match with the data type of variable. For example if data type is int then value must be and integer number like 20, 30, 40, 33 etc.

If variable value and variable data type are not in same category then compiler will generate an error.

Variable Declaration is Must

Variable declaration is must before to use it, we cannot use a variable without it’s declaration.

Variable Declaration Style

In java variable can be declare and initialize in many ways, let’s see all possible ways of declaring variables in java.

1. Declaration and Initialization together

You have already seen this type of declaration and initialization together, in this declaration and initialization of variable is done together.

2. Declaration first and Initialization latter

In this method, variable declaration is done first and initialization is done latter, see below example.

3. Multiple Variable declarations together

Java allows us to declare multiple variable together which have similar data type, we can declare and initialize together or we can declare together and initialize them latter. Let’s take some example.

Java variables Type

Java variables can be divided in two categories based on scope of variable and data type of variable

  1. Types of variable based on scope
  2. Types of variable based on data type

Types of variable based on scope

It defines the area ( scope ) where a variable can be accessed. Since we have not study yet about class and function so it’s difficult to understand variable scope, we will discuss it latter.

Types of variable based on data type

On the basis of data type a variable can be many types, here we are going to discuss some most used variables type based on their data type.

  • string: "string" type of variable stores text value, for example “ Hello Java” . This type values are surrounded by double quotes.
  • int: "int" type of variable is used to store integer value ( whole number ), without decimal point, for example 98, -22, 121 etc.
  • flaot: "float" type of variable is used to store floating point number, with decimal point, for example 22.5, -82.03, 134.52 etc.
  • char: "char" type of variable is used to store a single character, for example ‘c’, ’A’, ‘m’ etc., This type of values are surrounded by single quotes.
  • boolean: "boolean" type of variable can store two types of value, either true or false.

Program to print variables value

Example

Output

Hello! Jhon

String concatenation

If there are two values in which one is String and other is any data type value and if we are putting + symbol between these two value then these value will concatenation ( combine ) to each other and generate a new string value.

Example

"Hello" + "Tod" = Hello Tod
"Hello" + "Tod" = HelloTod
"Hello" + 10 = Hello 10
"Hello" + "10" = Hello 10
                                    

Note: One value must should be String for string concatenation, we will learn more in depth in String chapter.

Example

Output

Employee: Jhon
Employee ID: 101
Employee Age: 23
Employee Salary: 18000

println: It prints the value and breaks line

print: It prints the value but does not break line

Example

Output

300

Example

Output

Joy Smith

Example

Output

Hello my name is Joy and my age is 23 year!

Related Topics

Average salary in Mauritius

6 Ways to Modernize Your Manufacturing Business

Neha Bagaria Net Worth 2024, Business, Salary, Income Source, Car, House

What is the macro economics?

Ankur Aggarwal Net Worth 2024, Business, Salary, Income Source, Car, House

Pranav Goel Net Worth 2024, Business, Salary, Income Source, Car, House

Dilip Shanghvi Net Worth 2024, Business, Salary, Income Source, Car, House

What is the micro economics?

What are the basic accounting concepts?

Top 10 richest people in the world in 2023

What is an informal organization?

Khrisha Shah Net Worth 2024, Business, Salary, Income Source, Car, House

Average salary in Bahamas

Average salary in New Zealand

How to Find a Sponsor for Your Series 7 Examination

What do a supply schedule and supply curve show?

Average salary in Algeria

Anant Goel Net Worth 2024, Business, Salary, Income Source, Car, House

Elon Musk Net Worth 2024, Business, Salary, Income Source, Car, House

Akash Ambani Net Worth 2024, Business, Salary, Income Source, Car, House

Building Stronger Connections: A New Age of Customer Outreach

Ankit Bhati Net Worth 2024, Business, Salary, Income Source, Car, House

Navigating the Crypto Investment Landscape: Choosing the Right Platform for Your Portfolio

What is the GDP of West Bengal?

What is a Digital Marketing Company?

Average salary in Israel

Mohit Tandon Net Worth 2024, Business, Salary, Income Source, Car, House

Larry Ellison Net Worth 2024, Business, Salary, Income Source, Car, House

How to Choose a Digital Marketing Company?

Average salary in Burundi

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)