filmov
tv
#2.2 Java Tutorial | Working with Variables
Показать описание
In this lecture we are discussing
1) Why we we are using variable if we can use literal (data) directly ?
2) What is type safe or static type language ?
3) Method to declare variable
4) Way to store data in variable when error occur
#1
if we use directly literal and want to operation and print in console
then you can do only once. If you want to reuse in next operation or want to use
then you need to write this literal and remember this variable every time.
But if you can store in variable then you can use this value multiple time.
e.g
in every operation you need to remember the the literal 5 and 2
so they violate the DRY principle which is known as don't repeat yourself
So, for using multiple times we are declare the variable
int num1=5;
int num2=2;
#2
java is known as type safe or static type language. It means
data type declaration is necessary. Because during compilation type
is check if does not found give error
int a=5;
int num=6;// required to declare type of variable which we are using
#3
Method to declare variable
There are basically 3 steps to declare a variable in correct way:
step1: Remember We can use alphabets, digits and only two special symbol in variable declaration underscore(_) and dollar($).
step2: First letter can only start with alphabets , underscore and dollar sign but we cannot start first letter with digits.
step3: We cannot use reserved key word to declare the variable
e.g
//abstract , assert , boolean , break , byte , case , catch , char , class , const , continue , default ,
do , double , else , enum , extends , final , finally , float , for , goto , if , implements , import , instanceof ,
int , interface , long , native , new , package , private , protected , public , return , short , static , strictfp ,
super , switch , synchronized , this , throw , throws , transient , try , void , volatile , while
#4
Way to store data in variable
if you can declare
int i=5;
then , double d=i;
but it is incorrect to use int x=d;
because size of double is bigger than integer
by default 5.5 is double
then if you want to intialize 5.5 in float
then float x=5.5f;
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
1) Why we we are using variable if we can use literal (data) directly ?
2) What is type safe or static type language ?
3) Method to declare variable
4) Way to store data in variable when error occur
#1
if we use directly literal and want to operation and print in console
then you can do only once. If you want to reuse in next operation or want to use
then you need to write this literal and remember this variable every time.
But if you can store in variable then you can use this value multiple time.
e.g
in every operation you need to remember the the literal 5 and 2
so they violate the DRY principle which is known as don't repeat yourself
So, for using multiple times we are declare the variable
int num1=5;
int num2=2;
#2
java is known as type safe or static type language. It means
data type declaration is necessary. Because during compilation type
is check if does not found give error
int a=5;
int num=6;// required to declare type of variable which we are using
#3
Method to declare variable
There are basically 3 steps to declare a variable in correct way:
step1: Remember We can use alphabets, digits and only two special symbol in variable declaration underscore(_) and dollar($).
step2: First letter can only start with alphabets , underscore and dollar sign but we cannot start first letter with digits.
step3: We cannot use reserved key word to declare the variable
e.g
//abstract , assert , boolean , break , byte , case , catch , char , class , const , continue , default ,
do , double , else , enum , extends , final , finally , float , for , goto , if , implements , import , instanceof ,
int , interface , long , native , new , package , private , protected , public , return , short , static , strictfp ,
super , switch , synchronized , this , throw , throws , transient , try , void , volatile , while
#4
Way to store data in variable
if you can declare
int i=5;
then , double d=i;
but it is incorrect to use int x=d;
because size of double is bigger than integer
by default 5.5 is double
then if you want to intialize 5.5 in float
then float x=5.5f;
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Комментарии