Variable Declaration

	Variable Initialization

In this tutorial, you will learn-

Variable Declaration Variable Initialization Types of variables Data Types in Java Type Conversion & Type Casting

Variable Declaration:

To declare a variable, you must specify the data type & give the variable a unique name.

Examples of other Valid Declarations are

Variable Initialization:

To initialize a variable, you must assign it a valid value.

Example of other Valid Initializations are

You can combine variable declaration and initialization.

Example :

Types of variables

In Java, there are three types of variables:

Local Variables Instance Variables Static Variables

1) Local Variables

Local Variables are a variable that are declared inside the body of a method.

2) Instance Variables

Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.

3) Static Variables

Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.

Example: Types of Variables in Java

Primitive Data Types :- which include integer, character, boolean, and float Non-primitive Data Types :- which include classes, arrays and interfaces.

Primitive Data Types

Primitive Data Types are predefined and available within the Java language. Primitive values do not share state with other primitive values. There are 8 primitive types: byte, short, int, long, char, float, double, and boolean Integer data types

Floating Data Type Textual Data Type Logical Points to Remember:

	All numeric data types are signed(+/-).

	The size of data types remain the same on all platforms (standardized)

	char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports internationalization. UNICODE is a character set which covers all known scripts and language in the world

Java Variable Type Conversion & Type Casting

A variable of one type can receive the value of another type. Here there are 2 cases – Case 1) Variable of smaller capacity is be assigned to another variable of bigger capacity.

This process is Automatic, and non-explicit is known as Conversion

Case 2) Variable of larger capacity is be assigned to another variable of smaller capacity

In such cases, you have to explicitly specify the type cast operator. This process is known as Type Casting.

In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses. Example: To Understand Type Casting Step 1) Copy the following code into an editor. Step 2) Save, Compile & Run the code. Output: