0% found this document useful (0 votes)
125 views12 pages

Java Variable Declaration and Initialization

1. The numbers 1, 2, and 3 in the code sample represent variable declaration, initialization, and dynamic initialization respectively. 2. Variable declaration creates and reserves memory for a variable. Initialization assigns an initial value to a variable when it is declared. Dynamic initialization assigns a value to a variable during runtime based on operations or values of other variables. 3. The value of variable c would be found after execution, not before, because it is dynamically initialized based on the values of variables a and b, which are divided at runtime.

Uploaded by

Sumedha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views12 pages

Java Variable Declaration and Initialization

1. The numbers 1, 2, and 3 in the code sample represent variable declaration, initialization, and dynamic initialization respectively. 2. Variable declaration creates and reserves memory for a variable. Initialization assigns an initial value to a variable when it is declared. Dynamic initialization assigns a value to a variable during runtime based on operations or values of other variables. 3. The value of variable c would be found after execution, not before, because it is dynamically initialized based on the values of variables a and b, which are divided at runtime.

Uploaded by

Sumedha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Declaration, Initialization and Dynamic

Initialization
Concept Revisitation
Data types.
What is 1, 2, 3, in these lines of code depict?

public class Fifthmultiple


{
public static void main()
{
int a, five_m; //1
a = 22; //2
five_m = 5*a; //3
[Link](“The fifth multiple of 22 is :” + five_m);
}
}

Replacing 1, 2 and 3 with


Int a=22;
Int five_m=5*a;
Discussion.
1. Variable declaration.
2. Initialization
3. Dynamic Initialization
Declaration of a variable:

Creating a variable is also called declaring a variable. When you create a primitive variable
Java will set aside enough bits in memory for that primitive type and associate that memory
location with the name that you [Link] declare (create) a variable, you will specify the type,
leave at least one space, then the name for the variable and end the line with a semicolon (;).
Java uses the keyword int for integer, double for a floating point number (a double precision
number), and boolean for a Boolean value (true or false).

Eg: int score;

int a;
Initializing variables/ Static Initialization

Java allows us to initialize a variable on the same statement that declares the
[Link] a variable is both declared and assigned a value in a single statement, it is
called initialization of a [Link] variable is initialized/ memory allocated at the start or

.
time of declaration or compile time Static initialization is one where the value of the
variable is a constant that the compiler knows before execution.

Syntax:

datatype variable_name = constant;

int x = 5, y = 10;

int x = 0;

String lastName = "Lowe";

double radius = 15.4;


Dynamic Initialization/ Direct Initialization
When a variable gets initialized at run time ,i.e during the execution of a program
logic is termed as Dynamic Initialization. This makes it hard for the compiler to
allocate memory when the program has already started executing. Dynamic
initialization, the variable is initialized only during run time as it is assigned a value
which is an outcome that uses other variables as input.

Eg: int i=9;// initialization of i;

double y= [Link](9); // Dynamic initialization of y;


Look at this.
public static void main()
{
float a = 5.8;
float b = 3.3;
float c = a/b;
:
:
}

● What is the value of c?


● When would the value of c be found; Before execution/After execution?
Why?
Differences between Static and Dynamic
Static Dynamic

1. Declaring and assigning value in the same 1. Declaring assigning and giving an
statement. operation to perform.
2. The memory allocation is done at compile 2. The memory allocation is done only at
time before execution. run-time or after execution
3. This type of memory allocation is easier 3. Here compiler takes some time to allocate
for the compiler to work. and process the operation. Seemingly
4. Once the memory has been allocated, it hard
cannot be reused. 4. Since the memory is dynamically
5. Wastage of unused memory. allocated, efficient usage of memory.
5. No wastage of unused memory.
Pop Quiz
1. What data type of a variable is needed to store if a student has failed or passed
an exam?
2. What data type of variable is needed to store ℼ?
3. What data type of variable is needed to store the grade of a student?
4. Will the statement to calculate average marks be considered Static Initialization?
Why/Why not?

Video link

You might also like