Variable Declaration and Initialization | Java Tutorial for Beginners
About this video:
Hello Everyone,
In this video,I have explained,
What is Variable?
How to Declare Variables?
How to Initialize Variables? in simple way with real life example
Links :
Free core Java tutorials for beginners 👇 • Java Basics
Java Inheritance:
• Java Inheritance | Jav...
Class and Object :
• #1 class and object in...
Java Method:
• #2 Method in Java | Ja...
Java constructor:
• #5 Java Constructor | ...
Java Instance Block:
• #6 Instanse Block in J...
Why do we need programming language?:
• Why we need Programmin...
Notes:
1. What is variable?
2. How to Declare Variables?
2.1 Syntax
3. How to Initialize Variables
3.1 Syntax
1. What is variable?
Variable is a container that stores the value and value of variable can be change...
2. How to Declare Variables?
To declare means to create
That means,
To declare a Variables means to create a Variables.
Now let's think,
If you have to perform any activity on the stage then before going to perform anything on the stage, first you need to introduce yourself on the stage sothat everyone will khow about you.
Similarly,In Java
If you have to use variables in a program then first you need to declare a variables so that compiler knows about variables and if you won't declare a variables and directly use the variables in a program then compiler will raised an error and program won't compile so basically,before using variables in a program we must be declared variables
2.1 Syntax:
Syntax is nothing but the set of rules that you have to follow to write the code.
Same as other languages
Such as english language
You know that where you have to put fullstop ,where you have to put comma n all.
So Basically each language has its own set of rules to write.
Similarly,
In java programming language, there is a syntax ,that we have to follow to write the code of a program.So you can use following syntax to declare a Variables.
access_ modifier data type var_name;
access - modifier :
Access - modifier controls the access level or scope of Variables.
You can use four types of access-modifiers for variables in Java.
public,private,protected,default
For example:
If you create your profile on social media and set your profile as a public then everyone can see your profile.That means,
Your profile will be vissible for everyone
Similarly,
If you declare variable as a public then variable is vissible / Accessible to all classes in a program and If you declared variable as private then variable is accessible only within the same class, where variable declared..
If variable is declared as protected then variable is accessible within the same package or outside the package.
Note: package is nothing but the group of classes.If you don't declared any access_modifier to the variable then it consider as a default and in this case, variable is accessible within the same package.
dataType :
Each variable has it's type and type is nothing but the datatype.
such as
Boolean,byte,char,short, int ,long,float, double,String.
For example :
You can write like this,
public int var1;
here,public is access- modifier,int is datatype and it's int and var is variable name and at the end you have to put semicolon.
Semicolon is important at the end of each statement in java because
it shows to the compiler that where one statement will ends, and from where the next statement will begins.
3. How to Initialize Variables?
To initialize means to assign / to store
that means,to initialize a variable means to assign value / data to the variable.So basically to give initial values to the variables is called as initialization of variables
3.1 Syntax :
So you can use following syntax
access- modifier data type var name = value ;
First you have to write access modifier and next to access modifier there will be a dataType and then there will be variable name and next to the variable name there will be = sign and equal sign followed by value or data that you have to assign to the variable.
For example :
So you can write like this,
public int var1= 2;
Or you can declare and initialize variables like this,
public int var1; // declaration
var1 = 2; // initialization
Either you can declare variable and initialize a variable in same statement or you can declare and initialize a variable separately.
Thank you for watching 🙏
#variablesdeclaration #variablesinitialization
#javatutorialforbeginners
#sgtutorial
#english
#variablesdeclarationandinitialization
15 сен 2021