Introduction For Object Oriented
We have another method for printing, it is the method printf, printf is used to print the data as an ordered argument list separated by commas.
The following symbols are used to print out the parameter list comma-separated arguments in ordered fashion:
%d -- Means decimal integer number
%f -- Means decimal fractional number
%o -- Means octal integer number
%s -- Means string constant
The syntax of using method printf is:
System.out.printf (parameter list);
The parameter list is a comma separated argument list. To understand the method of printf, consider the following example:
The output of the last program is:
Note that the first argument contains the symbols used to print the following arguments in the parameter list.
Where %s will search for the 1st string argument to print it, the %d will search for the 1st integer argument to print it, & the next %s will search for the next string argument to print it. \n is used to print a new line.
Datatypes in Java are shown in the following table:
In Java, as mentioned before, storing a value in a variable with a different datatype requires casting to avoid data loss.
As a special case in Java, initializing a float declared variable requires writing f after the value stored in the variable to ensure that the variable is initialized with a constant float.
See the following example which shows how to declare a float variable in Java:
To understand the principle of casting, see the following example which uses casting throughout the storing of values inside variables with different datatypes:
Note that you can store a small datatype value inside a big datatype variable with different datatypes if they are of the same type (integer numbers (int, long, short), decimal point numbers (float, double),.....). Such as storing short in long (They both are integers).
سامر المناصرة
We have another method for printing, it is the method printf, printf is used to print the data as an ordered argument list separated by commas.
The following symbols are used to print out the parameter list comma-separated arguments in ordered fashion:
%d -- Means decimal integer number
%f -- Means decimal fractional number
%o -- Means octal integer number
%s -- Means string constant
The syntax of using method printf is:
System.out.printf (parameter list);
The parameter list is a comma separated argument list. To understand the method of printf, consider the following example:
The output of the last program is:
Note that the first argument contains the symbols used to print the following arguments in the parameter list.
Where %s will search for the 1st string argument to print it, the %d will search for the 1st integer argument to print it, & the next %s will search for the next string argument to print it. \n is used to print a new line.
Datatypes in Java are shown in the following table:
In Java, as mentioned before, storing a value in a variable with a different datatype requires casting to avoid data loss.
As a special case in Java, initializing a float declared variable requires writing f after the value stored in the variable to ensure that the variable is initialized with a constant float.
See the following example which shows how to declare a float variable in Java:
To understand the principle of casting, see the following example which uses casting throughout the storing of values inside variables with different datatypes:
Note that you can store a small datatype value inside a big datatype variable with different datatypes if they are of the same type (integer numbers (int, long, short), decimal point numbers (float, double),.....). Such as storing short in long (They both are integers).
سامر المناصرة
ليست هناك تعليقات:
إرسال تعليق