You are on page 1of 9

Variables and Data Types in Java

Variable is a name of memory location. There are three types of variables in java:
local, instance and static.

Example:

int a=10;

int a=10.5;
boolean b=0;

valid in C but not in java.

Object Oriented Programming (CSEG2016)


Valid/Invalid??
byte b=10;
byte b =127;
byte b=128;
byte b= 10.5;
byte b=true;//Incompatible type,found boolean, required byte.

Note: In java, every variable and expression has some type, every assignment should
be checked by compiler for type compatibility, because of this reason Java language
is Strongly-Typed programming language.

Data Types in Java


Data types represent the different values to be stored in the variable. In java, there
are two types of data types:
 Primitive data types
 Non-primitive data types

Object Oriented Programming (CSEG2016)


Assignment Q.1:
Object Oriented Programming (CSEG2016)
What are the minimum and maximum range of primitive data types in java.

Operators in java
Operator in java is a symbol that is used to perform operations. For example: +, -,
*, / etc.
There are many types of operators in java which are given below:
 Unary Operator,
 Arithmetic Operator,
 shift Operator,
 Relational Operator,
 Bitwise Operator,
 Logical Operator,
 Ternary Operator and
 Assignment Operator.

Operator
Category Precedence
Type
postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||

Object Oriented Programming (CSEG2016)


Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Examples:
class Arith
{
public static void main(String args[])
{
int a=20,b=3;
System.out.println("Value of a is:"+a +"\nValue of b is:"+b);
System.out.println("Sum is:"+ (a+b));
System.out.println("Difference is:"+ (a-b));
System.out.println("Multiplication is:"+ (a*b));
System.out.println("Division is:"+ (a/b));
System.out.println("Remainder is:"+ (a%b));
}
}

Object Oriented Programming (CSEG2016)


class Area
{
public static void main(String args[])
{
int rad=21;
double area, circum;
area=Math.PI*Math.pow(rad, 2); //3.14*rad*rad;
circum =2*Math.PI*rad;
System.out.println ("Radius of Circle is:"+rad);
System.out.println ("Area of Circle is:"+area);
System.out.println ("Circumference of Circle is:"+circum);
}
}

Object Oriented Programming (CSEG2016)


Object Oriented Programming (CSEG2016)
class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++);
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}
}
Output:

Java Unary Operator


Example 2: ++ and --
class OperatorExample1{
public static void main(String args[]){

Object Oriented Programming (CSEG2016)


int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}
}

Object Oriented Programming (CSEG2016)

You might also like