How To Initialize a Variable - Java Tutorial For Absolute Beginners

preview_player
Показать описание
Variable Initialization -- Java Tutorial For Absolute Beginners
Variable Initialization:
To initialize a variable, means to assign a valid value to a variable.
There are two ways to initialize a variable

Using the assignment statement
Using the input statement (assign the value by typing it on the keyboard)

Using the assignment statement

1) Combining variable declaration and initialization.

Java programming language allows to initialize a variable on the same statement that declares the variable. Here is the formula it follows

Datatype variable name = value;

For example, here we have
int age = 18;
double radius = 15.4;

Warning: certain variables are initialized differently from the others

String fistname= "Nicole";
char a=’v’;
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int [] numbers = {10, 20, 30, 40};
double do=20.22d;
float pi=3.14f;

Note: In Java, by default, floating-point numbers are considered to be of type double. Therefore, if you use the data type float to represent floating-point numbers in a program, you might get a warning or an error message, such as ‘‘truncation from double to float’’ or ‘‘possible loss of data.’’ To avoid such messages, you should use the double data type. However, if you need to explicitly use a float value in a program, specify that the decimal value is a float value using the letter "f" at the end of the number.

In each case, the variable is both declared and initialized in a single statement.
When you declare more than one variable in a single statement, each variable can have its own initializer. For example: int x = 18, y = 10;

WARNING: When you declare two class or instance variables in a single statement but use only one initializer, you can mistakenly think that the initializer applies to both variables. Consider this statement:
int x, y = 5;

Here you might think that both x and y would initialize to 5. But the initializer applies only to y, so x is initialized to its default value, 0. (If you make this mistake with a local variable, the compiler displays an error message for the first statement that uses the x variable because it isn’t properly initialized.)

2) Using an assign statement after the declaration of the variable

Another way to initialize a variable is to use an assignment statement following the variable declaration. Your code will follow this form

Datatype variable name;
Variable name = value;

Example of Valid Initializations
int age;
age = 18;

String name;
name= “Parker”;

char a;
a=’v’;

Boolean t;
t=true;

float pi;
pi=3.14f; // Dealing with decimal numbers

double do;
do=20.22d; // Dealing with decimal numbers

Note: In the examples above we have initialized the variables by assigning constant values but it is also possible to assign to a variable a Java expression or another.

The only condition is that the expression or the other variable must yield a value of the same data type as the variable you are initializing.

int a,b,c,d;
b=10; c=18;
d=a;
a=b+c;
Рекомендации по теме