Variables:
Variable Declaration: It is process of specifying what
type of data to be stored into the memory location.
Syntax: datatype <variable_name> OR datatype <var1>, <var2>,
<var3>
Ex:
int
age; (age is a variable name given to a
memory location where we can store integer type of data)
float
marks;
char
c;
boolean
status;
1. A Java
program can contain any number of variables.
2. In java language, the variable
must be first declare then use it. We cannot use the variable without declaring
it.
Rules in a variable Declaration:
·
A
java program can contain any number of variables
·
The
declaration of the variable is dynamic that means we can declare a variable
anywhere in a program.
·
The
variable in java language must be first declare and use. (java is strongly
typed language)
·
When
a variable is declared, the memory for that variable is allocated depending
upon the data type that is specified
·
Once
a variable is declared, the memory for that variable will be allocated and the
variable will be automatically initialized with the default value of that
corresponding data type.
Data
Type Default Value Data Type Default
Value
byte -> 0 float 0.0
short -> 0 double 0.0
int -> 0 char space
long -> 0 boolean false
The
default data type under integer category is “int”. The default data type under
floating point category is “double”.
If
a variable is declared and if we do not provide any value, then that variable
will be automatically initialized with the default value.
Declaring
and Initializing a Variable:
The variable
can be initialized with our own values at the time of declaration.
Syntax:
datatype
<variable_name> = <value>; (OR)
datatype
<variable_name1> = value1, <variable_name2> = value2 so on……
Ex: int a = 12; OR int a=10, b=20, c=30;
Initialization:
The process of providing a value to
the variable for the first time is called a initialization.
Assignment:
The process of providing a value to a variable second time onwards is called a
assignment.
The value of a variable can be changed during the execution
of a program that a variable can be assigned any number of times but variable
can be initialized only one time.
Ex:
int a=10; // Initializing
int a=20; // Assigning
Note: We can also call a value as a
literal.
int
a = 10; // 10 is integral literal
float
b = 12.2 // 12.2 is floating
point literal
char
c = ‘$’ // $ is character
literal
Method:
A method is a block of code that
perform some task or an action.
JVM executes a method only when we
call or invoke it.
We write a method once and use it
many times. We do not require to write code again and again.
A method must be declared within a
class.
A method can be called any number of
times.
Method Creation Syntax:
<access
specifier> <return type> <method name>(parameters list){
//Method
body
}
Example of Method Creation:
public void addition(int a, int b) {
System.out.println(a+b);
}
A method
contains Two parts. Method Header and Method Body. Method header contains 4
components those are access specifier, return type, method name and parameters.
0 comments:
Post a Comment