Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
Java All : Operators (Arithmetic-Assignment-Unary-Relational-Logical)
Concept Learning Code Sheets

Sum of Inputs #557

This is a program to learn different ways of getting the desired console output

class SumOfInputs
{
public static void main(String[] args)
{
int a=10;
int b=20;
int c=a+b;
System.out.println(c);
System.out.println(a+b);
System.out.println("Sum of Inputs :"+(a+b));
}
}

Arithmetic Operators #3657

Addition, subtraction, multiplication, division and modulus operator

class ArithmeticOp
{
public static void main(String[] args)
{
int a=10;
int b=4;
int c=a+b;
int d=a-b;
int e=a*b;
int f=a/b;
int g=a%b;
System.out.println("a = "+a+" b = "+b);
System.out.println("a+b = "+c);
System.out.println("a-b = "+d);
System.out.println("a*b = "+e);
System.out.println("a/b = "+f+" (Integer Division)");
System.out.println("a%b = "+g);
}
}

Simple Assignment and Compound Assignments #3661

Using simple assignment means = operator and special variations with arithmetic operators like +=, -=, *=, /= and %= .

class AssignmentOp
{
public static void main(String[] args)
{
int a,b,c,d;
a=10;
b=20;
System.out.println("a=10, b=20 so a+b="+ (a+b));
a+=10;
System.out.println("after a+=10 a="+ a);
b-=10;
System.out.println("after b-=10 b="+ b);
c=d=10;
System.out.println("after c=d=10 c="+ c);
System.out.println("after c=d=10 d="+ d);
c*=5;
d/=2;
System.out.println("after c*=5 c="+ c);
System.out.println("after d/=2 d="+ d);
d%=2;
System.out.println("after d%=2 d="+ d);
}
}

Unary Operator Post and Pre Operations #3663

Understanding unary increment with post operation and pre operation example.

class UnaryPostPre
{
public static void main(String[] args)
{
int a=10,b=10;
System.out.println("a++ directly done while printing = "+ a++);
System.out.println("Value of a in next statement = "+ a);
System.out.println("++b directly done while printing = "+ ++b);
System.out.println("Value of b in next statement = "+ b);

}
}

Relational operators usage for data comparison #3676

Use of equality check operator == and other relational operators like not equal to !=, less than < , greater than > , less than or equal to <= and greater than or equal to >=  operators.

class RelationalOP
{
public static void main(String[] args)
{
int a=10,b=10;
boolean c= (a==b);
System.out.println("a=10,b=10 so a==b will be "+c);
c=a!=b; System.out.println("a=10,b=10 so a!=b will be "+c);
c=a>5; System.out.println("a=10 so a>5 will be "+c);
c=b<11; System.out.println("b=10 so b<11 will be "+c);
c=a<=10;System.out.println("a=10 so a<=10 will be "+c);
c=b>=10;System.out.println("b=10 so a>=10 will be "+c);
}
}

Unary Operator plus, minus and inversion #3692

Understanding unary operator plus, minus and inversion.

class UnaryNegNot
{
public static void main(String[] args)
{
int a=+5;
int b=-a;
boolean c=!(a>5);
System.out.println(b);
System.out.println(c);
}
}

Conditional or Logical Operators AND, OR #3698

Understanding the use of condition operators and (&&) and or (||)

class ConditionalOP
{
public static void main(String[] args)
{
int a=10,b=10;
boolean c;
c=(a==10) && (b>=10);
System.out.println(c);
c=(a>10) || (b>=10);
System.out.println(c);
c=(a>10) || (b>10);
System.out.println(c);
}
}

Conditional Ternary Operators #3702

Understanding the use of conditional ternary operator with three operands

class TernaryOP
{
public static void main(String[] args)
{
int a=10;
boolean b=(a==10)?true:false;
System.out.println(b);
b=(a>10)?true:false;
System.out.println(b);
}
}

Operator precedence in arithmetic and unary expressions #3735

Understanding operator precedence with simple arithmetic and unary operators.

class OperatorPrecedence
{
public static void main(String[] args)
{
int a=1,b=2,c=3;
int d= a++ - ++c/-b * a + c%++b;
System.out.println(d);
}
}

Operator precedence with brackets #3738

Understanding operator precedence when brackets are used.

class OperatorPrecedenceBrackets
{
public static void main(String[] args)
{
int a=5,b=7,c=9;
int d= a++ + (b%(c%a)) + (a+b+c);
System.out.println(d);
}
}

Operator precedence with comparison and logical operators #3740

Understanding operator precedence with comparison(relational) and logical(conditional) operators.

class OpPrecCompCond
{
public static void main(String[] args)
{
int a=10,b=12,c=14;
boolean d= a-c%b >= 8 && a*2 == b*2-4;
System.out.println(d);
}
}

Auto conversion, Widening Type Promotion or Implicit Casting #3787

Understanding rules of widening type promotions from byte->short->int->long->float->double.

class TypeConversionAuto
{
public static void main(String[] args)
{
byte a=127;
short b=a;
int c=b;
long d=c;
float e=d;
double f=e;
System.out.println("byte initialised a="+a);
System.out.println("byte to short b="+b);
System.out.println("short to int c="+c);
System.out.println("int to long d="+d);
System.out.println("long to float e="+e);
System.out.println("float to double f="+f);
}
}
Solved Problems

Increment, Decrement operator Output Writing #3666

Write output of the following program.

Assignment operators based Output Writing #3672

Write output of the following program.

Relational operators based Output Writing #3682

Write output of the following program.

Conditional Operator Output Value Proving #3704

Prove that output of the following program would be true.

Precedence of Operator #3911

Operators with higher precedence are evaluated before operators with relative

Exam Questions
Quizzes

Code Sheets:12  Solved Problems:6  Exam Questons:7  Quizzes:6
Back