0% found this document useful (0 votes)
20 views

Variables, Data Types, and Conversion (Minggu 3)

The document discusses various Java data types including primitive types like integer, floating point, character, and boolean. It provides examples of declaring variables of different types and performing conversions between types. The key types are integer (byte, short, int, long), floating point (float, double), character, and boolean. Variables can be local, instance, or static. Widening conversion happens automatically between compatible types while narrowing requires explicit casting.

Uploaded by

Markus Silitonga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Variables, Data Types, and Conversion (Minggu 3)

The document discusses various Java data types including primitive types like integer, floating point, character, and boolean. It provides examples of declaring variables of different types and performing conversions between types. The key types are integer (byte, short, int, long), floating point (float, double), character, and boolean. Variables can be local, instance, or static. Widening conversion happens automatically between compatible types while narrowing requires explicit casting.

Uploaded by

Markus Silitonga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DATA TYPE, VARIABLE

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

{ seconds = days * 24 * 60 * 60; // convert to


seconds
public static void main(String args[])
distance = lightspeed * seconds; // compute
{ distance

int lightspeed; System.out.print("In " + days);

long days; System.out.print(" days light will travel


about ");
long seconds;
System.out.println(distance + " miles.");
long distance;
}
lightspeed = 186000;
}
4
5
FLOATING POINT TYPES
This is used for numbers with a decimal part. There
are two floating-point types: float (32 bits, single-
precision) and double (64bits, double-precision).

6
class Area

public static void main(String args[])

double pi, r, a;

r = 10.8; // radius of circle

pi = 3.1416; // pi, approximately

a = pi * r * r; // compute area

System.out.println("Area of circle is " + a);

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

public static void main(String args[])

char ch1, ch2;

ch1 = 88; // code for X

ch2 = 'Y';

System.out.print("ch1 and ch2: ");

System.out.println(ch1 + " " + ch2);

9
class CharDemo2

public static void main(String args[])

char ch1;

ch1 = 'X';

System.out.println("ch1 contains " + ch1);

ch1++; // increment ch1

System.out.println("ch1 is now " + ch1);

10
BOOLEAN DATA TYPE
The boolean type can have one of two values, true or false.

class BoolTest { if(b)


public static void main(String args[]) System.out.println("This is executed.");
{
b = false;
boolean b;
if(b)
b = false;
System.out.println("This is not executed.");
System.out.println("b is " + b);
System.out.println("10 > 9 is " + (10 > 9));
b = true;
}
System.out.println("b is " + b); 11
}
12
VARIABLE
A variable provides us with named storage that our
programs can manipulate. Each variable in Java has a
specific type, which determines the size and layout of
the variable's memory; the range of values that can be
stored within that memory; and the set of operations
that can be applied to the variable.

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:

• The two data types are compatible.


• When we assign a value of a smaller data type to a
bigger data type.
For Example, in java, the numeric data types are compatible with each other but no
automatic conversion is supported from numeric type to char or boolean. Also, char
and boolean are not compatible with each other.

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 {

// Main driver method

public static void main(String[] args)

// Double datatype

double d = 100.04;

// Explicit type casting by forcefully getting

// data from long datatype to integer type

long l = (long)d;

// Explicit type casting

int i = (int)l;

27
// Print statements

System.out.println("Double value " + d);

// While printing we will see that

// fractional part lost

System.out.println("Long value " + l);

// While printing we will see that

// fractional part lost

System.out.println("Int value " + i);

28

You might also like