Initialization in Java:
After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the values of uninitialized variables. You assign to a previously declared variable using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.
int vacationDays; // this is a declaration
vacationDays = 12; // this is an assignment
Here's an example of an assignment to a character variable:
char yesChar;
yesChar = 'Y';
One nice feature of Java is the ability to both declare and initialize a variable on the same line.
For example:
int vacationDays = 12; // this is an initialization
Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:
double salary = 65000.0;
System.out.println(salary);
int vacationDays = 12; // ok to declare variable here
you cannot declare two variables with the same name in the same scope.
Constants:
In Java, you use the keyword final to denote a constant. For example, public class Constants
{
public static void main(String[] args)
{
final double CM_PER_INCH = 2.54;
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeter: "
paperWidth * CM_PER_INCH + " by "
paperHeight * CM_PER_INCH);
}
}
The keyword final indicates that you can assign to the variable once, then its value is set once and for all. It is customary to name constants in all upper case. It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant withthe keywords static final. Here is an example of using a class constant:
public class Constants2
{
public static final double CM_PER_INCH = 2.54;;
public static void main(String[] args)
{
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeter: "
+ paperWidth * CM_PER_INCH + " by "
+ paperHeight * CM_PER_INCH);
}
}
Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if the constant is Declared public, methods of other classes can also use the constant.
const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.
RELATED POST
JAVA PROGRAMMING ENVIRONMENT
After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the values of uninitialized variables. You assign to a previously declared variable using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.
int vacationDays; // this is a declaration
vacationDays = 12; // this is an assignment
Here's an example of an assignment to a character variable:
char yesChar;
yesChar = 'Y';
One nice feature of Java is the ability to both declare and initialize a variable on the same line.
For example:
int vacationDays = 12; // this is an initialization
Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:
double salary = 65000.0;
System.out.println(salary);
int vacationDays = 12; // ok to declare variable here
you cannot declare two variables with the same name in the same scope.
Constants:
In Java, you use the keyword final to denote a constant. For example, public class Constants
{
public static void main(String[] args)
{
final double CM_PER_INCH = 2.54;
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeter: "
paperWidth * CM_PER_INCH + " by "
paperHeight * CM_PER_INCH);
}
}
The keyword final indicates that you can assign to the variable once, then its value is set once and for all. It is customary to name constants in all upper case. It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant withthe keywords static final. Here is an example of using a class constant:
public class Constants2
{
public static final double CM_PER_INCH = 2.54;;
public static void main(String[] args)
{
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeter: "
+ paperWidth * CM_PER_INCH + " by "
+ paperHeight * CM_PER_INCH);
}
}
Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if the constant is Declared public, methods of other classes can also use the constant.
const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.
RELATED POST
JAVA PROGRAMMING ENVIRONMENT
No comments:
Post a Comment