Pre & Post increment operator example Java

Hi,

Today we will be Finding the difference between pre- and post- increment of variables.

Pre -increment Operator:

int x=1;
System.out.println("x:"+x);
System.out.println("x:"+(++x));
System.out.println("x:"+x);

Post -increment Operator:

int x=1;
System.out.println("x:"+x);
System.out.println("x:"+x++);
System.out.println("x:"+x);

So what will be output you think??

For Post – increment Operator:

Output:


x:1
x:1
x:2

For Pre -increment Operator:
Output:

x:1
x:2
x:2

Explanation:

For pre – increment operator :

System.out.println(“x:”+x);  //Display value of x i.e 1

System.out.println(“x:”+(++x));  //First increase the value of x and then display i.e 2

System.out.println(“”x:”+x);  //Display value of x i.e 2

For post -increment operator :

System.out.println(“x:”+x);  //Display value of x i.e 1

System.out.println(“x:”+(x++));  //First display the value of x and then increment i.e 1

System.out.println(“”x:”+x);  //Display the incremented value of x i.e 2

 

That’s it so simple of Pre increment and Post increment Operation in Java.

Note: The operator (++) is called as Increment Operator and (–) is called as Decrement operator and these both Operator fall under the category of Unary Operator.

Feel free to comment 🙂

Differences between C++ & Java

By the way , C++ is also an object oriented programming language , just like Java. But there are some important features wise differences , between C++ and Java.

Let’s have a glance at them :

C++ Jva
1 C++ is not a purely object-oriented programming language
, since its possible to write C++ programs without using a class or an object.
Java is purely an object – oriented programming language ,
since it’s not possible to write Java program without construction of class.
2 Pointers are available in C++. We cannot create and use pointers in Java
3 Allocation and deallocation memory is the responsibility of the programmer. Here its taken care by JVM
4 Automatic casting is available in C++. In some cases , implicit casting is available , but one should use casting when required
5 C++ has goto statements. Its forbidden in java.
6 Multiple inheritance feature is available in C++. No Multiple inheritance in Java , but one can achieve in using Interface 🙂
7 There are 3 access specifiers in C++:
1)private
2)protected
3)public
Java supports 4 access specifiers:
1)private
2)protected
3)public
4)default
8 There are constructors and destructors in C++ . Only Constructors are available.

These where some of the key points that differentiate between C++ and Java.

Feel free to comments.

Java If-Else

Lets understand java’s if else programs which will help  you understand if else in java

1)Java program to check whether you are eligble for voting or not.

public class checkAge

{

public static void main(String arg[])

{

Scanner sc = new Scanner(System.in);

int age=sc.nextInt(); //Taking input from the user

if(age>=18)

{System.out.println(“You are elgible for voting in India”);}

else

{System.out.println(“You are not eligble”);}

}

}

Here if else so easy as you can see.

2)If -else if ladder

Java program to

Given an integer n perform the following conditional actions:

  • If is odd, print Weird
  • If is even and in the inclusive range of 2 to 5 , print Not Weird
  • If is even and in the inclusive range of 6 to 20 , print Weird
  • If is even and greater than 20 , print Not Weird

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String ans=””;
if(n%2==1){
ans = “Weird”;
}
else {
if(n>=2 && n<=5) { ans= “Not Weird”; } else if(n>=6 && n<=20)
{
ans=”Weird”;
}
else
{
ans =”Not Weird”;
}
//Complete the code

}
System.out.println(ans);

}
}

Feel free to comment to you have doubt regarding the program.