How to use and store data using variable in java programming

Hi! in this lesson you'll learn how program can store data in variables. Check out the codes below it declares an "integer" variable (a) with the value 24 and prints the value of this variables.

public class Main(){
public static void main(String[] args){
int a = 24;
System.out.println(a);
int b = 59;
System.out.println(b);
int c = a+b;
System.out.println(c);
//variable d added
int d = a*b;
System.out.println(d);
}
}


As you can see variables can hold values that can be accessed later, you can try changing the value of (a) for your test, make it print another number instead. Change it from 24 to 59 then print the result. once you have done that let us now to proceed to the other characteristic of variable check out the code I've added above, I've added variable a and b the value of c is computed from the sum of "a" and "b" have a look at the codes above, The program prints the result of the additions of "a" and "b". Now declare a variable  "d" and initialize with the product of "a" and "b" and print the result, this is for your enhancement activities or otherwise skip this for now and look the example above instead.


Variable Modification


in the previous discussion, we'll learn how powerful the variable is and how it can store data. Now let's try to move on to that and proceed to the next topic or the Variable Modification, within this lesson you'll learn how to manipulate data in variables. Check out the java program below it changes the value of a variable (a) during its execution.

public class Main{
public static void main(String[] args){
int a = 5;
System.out.println(a);
a= 9;
System.out.println(a);
a=a+1;
System.out.println(a);
a=a+1;
System.out.println(a);

}
}

in this case, the println statements print the recent actual value of "a" at the time they are executed. Now we change the value of variable "a" to 6 after the existing code and print it as well check out the added codes above which increase the value of "a" by 1 by using addition. the statement a=a+1 above first total "a+a" and then assigns the result back to "a" Now we can increase the value of "a" the same way again and print it. try changing the initial value of "a" so that the program prints 11,12,13 The output should print 11,12,13 in three lines, since increasing a variable by 1 is required often there is a shortcut for it. Check out the program I've added above it uses the (++) operator to increase "a" by 1, then after that try to run the program. the (++) operator increases the value of the variable there's also a (--) which means decrement the value of variable, try changing the ++ operator to -- operator and see what happens. The (--) operator decreases the variable value.



Variable types


in continuation of the variable topics, we are now going to learn how the declared type of a variable determines the type of data it can store. This way it is possible to find errors in programs even without running them. Check out the java program below which declares Variable a; The int here specifies that the types of the variable is an integer and can only hold integer numbers.

public class Main{
public static void main(String[] args){
int a = 11;
System.out.println(a);
int a =11.4 // Fix this error
System.out.println(a);
double pi = 3.1415;
System.out.println(pi);// double variable added
String s ="Hello"; //String statement added
System.out.println(s);

}
}

To store other values type like Strings or floating-point numbers you just need to declare a different variable type, try changing the type of "a" from integer to double and see what happen The int variable "a" could not hold a floating number point for example 11.4. Now let us declare another double variable "pi" which will hold the floating pint number (3.1415) and see what happens when we print it. Now let's explore some other types of variable, we can also add a string variable to print a string value, look at the code above we've added a string variable see how it will be printed from your IDE.

Post a Comment

0 Comments