Object Oriented Programming From Datatypes
Object Oriented Programming From Datatypes
PROGRAMMING
FROM DATATYPES
DATA TYPES
Data types specify the different sizes and
values that can be stored in the variable.
There are two types of data types in Java:
{
// arithmetic using integers
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
}}
Output:
Integer Arithmetic a = 2
b=6
c=1
d = -1
e=1
Floating Point Arithmetic da = 2.0
db = 6
dc = 1.5
dd = -0.5
de = 0.5
Modulus Operator
x mod 10 = 2
y mod 10 = 2.25
Arithmetic Compound Assignment Operators
shown here: a += 4;
Syntax:
var op= expression;
Example:
// Demonstrate several assignment operators. class OpEquals
{
public static void main(String args[])
{
int a = 1; int b = 2; int c = 3; a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a); System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
Output:
a = 6
b = 8
c = 3
Increment and Decrement Operators
The ++ and the – – are Java’s increment and decrement operators. The increment
operator increases its operand by one. The decrement operator decreases its operand
by one.
Example:
// Demonstrate ++. class IncDec
{
public static void main(String args[])
{
int a = 1; int b = 2; int c;
int d;
c = ++b; d = a++; c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
Output:
a = 2
b = 3
c = 4
d = 1
Bitwise Operators