Learning Objectives
After completing this session, you will be able to:
Write Java programs by declaring and initializing variables
Declaring and Initializing Variables:
Sample Program
1 public class VariableSamples {
2 public static void main( String[] args ){
3 //declare a data type with variable name
4 //result and boolean data type
5 boolean result;
6
7 //declare a data type with variable name
8 // option and char data type
9 char option;
10 option = 'C'; //assign 'C' to option
11
12 //declare a data type with variable name
13 //grade, double data type and initialized
14 //to 0.0
15 double grade = 0.0;
16 }
17 }
Declaring and Initializing Variables: Coding Guidelines
It is always good to initialize your variables as you declare them.
Apply descriptive names for your variables.
For example, for a student’s grade, the variable name can be given as “grade” or
“studentGrade” instead of giving any other names.
Declare one variable in each line of code.
For example, the variable declarations:
double exam = 0;
double quiz = 10;
double grade = 0;
is preferred over the declaration double exam=0, quiz=10, grade=0;
Try It Out
Problem Statement:
What will happen if you try to compile the code given in the next slide?
Code:
class Digit {
public void add() {
int k;
int s = k + 3;
}
}
How It Works:
The preceding code will not compile. You can declare k without a value, but as soon as you try to
use it, the compiler freaks out.
Tips and Tricks
Provide some important tips on conversion of primitives in Java
Solution:
The three types of conversion are namely assignment conversion, method call
conversion, and arithmetic promotion.
boolean may not be converted to or from any type that is not boolean.
byte and short, cannot be converted to char and char cannot be converted to byte and
short.
Arithmetic promotion:
Unary operators:
If the operand is byte, short or char {
convert it to int;
}
else {
do nothing; no conversion needed;
}
Binary operators:
If the operand is double {
all double; convert the other operand to double;
}
else if one operand is float {
all double; convert the other operand to
double;
}
else if one operand is float {
all double; convert the other operand to double;
}
else {
all int; convert all to int;
}
Summary
Local variables (method variables) live on the stack.
Objects and their instance variables live on the heap.
Scope of a variable refers to the lifetime of variable.
There are four basic scopes:
Static variables live basically as long as their class lives.
Instance variables live as long as their object lives.
Local variables live as long as their method is on the stack; however, if their method
invokes another method, they are temporarily unavailable.
Block variables (e.g., in a for or an if) live until the block completes.
After completing this session, you will be able to:
Write Java programs by declaring and initializing variables
Declaring and Initializing Variables:
Sample Program
1 public class VariableSamples {
2 public static void main( String[] args ){
3 //declare a data type with variable name
4 //result and boolean data type
5 boolean result;
6
7 //declare a data type with variable name
8 // option and char data type
9 char option;
10 option = 'C'; //assign 'C' to option
11
12 //declare a data type with variable name
13 //grade, double data type and initialized
14 //to 0.0
15 double grade = 0.0;
16 }
17 }
Declaring and Initializing Variables: Coding Guidelines
It is always good to initialize your variables as you declare them.
Apply descriptive names for your variables.
For example, for a student’s grade, the variable name can be given as “grade” or
“studentGrade” instead of giving any other names.
Declare one variable in each line of code.
For example, the variable declarations:
double exam = 0;
double quiz = 10;
double grade = 0;
is preferred over the declaration double exam=0, quiz=10, grade=0;
Try It Out
Problem Statement:
What will happen if you try to compile the code given in the next slide?
Code:
class Digit {
public void add() {
int k;
int s = k + 3;
}
}
How It Works:
The preceding code will not compile. You can declare k without a value, but as soon as you try to
use it, the compiler freaks out.
Tips and Tricks
Provide some important tips on conversion of primitives in Java
Solution:
The three types of conversion are namely assignment conversion, method call
conversion, and arithmetic promotion.
boolean may not be converted to or from any type that is not boolean.
byte and short, cannot be converted to char and char cannot be converted to byte and
short.
Arithmetic promotion:
Unary operators:
If the operand is byte, short or char {
convert it to int;
}
else {
do nothing; no conversion needed;
}
Binary operators:
If the operand is double {
all double; convert the other operand to double;
}
else if one operand is float {
all double; convert the other operand to
double;
}
else if one operand is float {
all double; convert the other operand to double;
}
else {
all int; convert all to int;
}
Summary
Local variables (method variables) live on the stack.
Objects and their instance variables live on the heap.
Scope of a variable refers to the lifetime of variable.
There are four basic scopes:
Static variables live basically as long as their class lives.
Instance variables live as long as their object lives.
Local variables live as long as their method is on the stack; however, if their method
invokes another method, they are temporarily unavailable.
Block variables (e.g., in a for or an if) live until the block completes.
No comments:
Post a Comment