Variables, Data Types, and Conversion (Minggu 3)
Variables, Data Types, and Conversion (Minggu 3)
AND CONVERSION
By: Kadri Yusuf, ST., M.Kom 1
DATA TYPE
Java defines eight primitive types of data:
1. byte,
2. short,
3. int,
4. long,
5. char,
6. float,
7. double,
8. boolean.
2
INTEGER TYPES
3
class Light days = 1000; // specify number of days here
6
class Area
double pi, r, a;
a = pi * r * r; // compute area
7
CHAR DATA TYPE
The char type is used for individual characters.
Because Java uses the Unicode character set, the char
type has 16 bits of precision, unsigned.
8
class CharDemo
ch2 = 'Y';
9
class CharDemo2
char ch1;
ch1 = 'X';
10
BOOLEAN DATA TYPE
The boolean type can have one of two values, true or false.
13
To declare more than one variable of the specified
type, you can use a comma-separated list.
Following are valid examples of variable declaration
and initialization in Java − Example
14
3 KINDS OF VARIABLES
▪ Local Variable
▪ Instance Variable
▪ Classic / Static Variable
15
LOCAL VARIABLE
• Local variables are declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or block is
entered and the variable will be destroyed once it exits the method,
constructor, or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared method, constructor, or
block.
• Local variables are implemented at stack level internally.
• There is no default value for local variables, so local variables should be
declared and an initial value should be assigned before the first use.
16
▪ Method Definition: A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method. Methods are used to
perform certain actions, and they are also known as functions.
▪ Constructor : A constructor in Java is a special method that is used to initialize
objects. The constructor is called when an object of a class is created.
▪ Block : A block is a set of statements enclosed in set braces { }. Blocks can be nested.
17
EXAMPLE
18
FOLLOWING EXAMPLE USES AGE WITHOUT
INITIALIZING IT
19
CONVERSION
Type conversion is simply the process of converting data
of one data type into another. This process is known as
type conversion, typecasting, or even type coercion. The
Java programming language allows programmers to
convert both primitive as well as reference data types
Conversion divide 2 :
1. Widening or Automatic Type Conversion
2. Narrowing or Explicit Conversion
20
WIDENING OR AUTOMATIC TYPE
CONVERSION
Widening conversion takes place when two data types are automatically converted.
This happens when:
21
22
class GFG { // Print and display commands
// Main driver method System.out.println("Int value " + i);
System.out.println("Long value " + l);
public static void main(String[]
args) System.out.println("Float value " + f);
}
{
}
int i = 100;
// Automatic type conversion
// Integer to long type
long l = i;
// Automatic type conversion
// long to float type
float f = l; 23
NARROWING OR EXPLICIT
CONVERSION
If we want to assign a value of a larger data type to a
smaller data type we perform explicit type casting or
narrowing. This is useful for incompatible data types
where automatic conversion cannot be done. Here,
the target type specifies the desired type to convert
the specified value to.
24
public class GFG { // Declaringinteger variable
// Main driver method int num = 88;
public static void // Trying to insert integer to
main(String[] argv) character
{ ch = num;
// Declaring character }
variable }
char ch = 'c';
25
This error is generated as an integer variable takes 4 bytes while character datatype requires
2 bytes. We are trying to plot data from 4 bytes into 2 bytes which is not possible.
How to do Explicit Conversion?
26
public class GFG {
// Double datatype
double d = 100.04;
long l = (long)d;
int i = (int)l;
27
// Print statements
28