Translate

Showing posts with label JAVA TUTORIALS FOR BEGINNERS. Show all posts
Showing posts with label JAVA TUTORIALS FOR BEGINNERS. Show all posts

Wednesday, 22 June 2016

Java Switch Construct

Hi friends ........

                          selection construct

There are two types of constructs in Java...
        1.if-else
        2.switch-case

Note: if-else you know very well in c/c++ language.In java also if-else have same concepts.

                                              switch-case

There are some points about switch........
1.switch can only have Integer or Integer expression,string,enum.
2.case should always have a constant value or literal.
3.if you not used break inside case then it also executes all the case(followed the case which matches)until the break found or end of switch reached.

Demo:

public class Switch{
 public static void main(String args[]){
  //switch
  int x=8;


  switch(x){
   case 2:
      System.out.println("yo its x=2");
      break;
   case 4:
      System.out.println("yo its x=4");
      break;
   case 6:
      System.out.println("yo its x=6");
      break;
   case 8:
      System.out.println("yo its x=8");
      break;
   default:
      System.out.println("yo its >8");

  }
 }
}

output:

yo its x=8

Java Ternary Operator

                            Ternary Operators
Def: In ternary operators we use 3 operands.

syntax:
        expr1 ? expr2 : expr3

Note: Result of expr1 should be boolean in java.

Demo:

public class Ternary{
 public static void main(String args[]){
  //ternary operator

  int a=5;

  System.out.println( a>4 ? "a is greater than 4" : "a is less than equal to 4");
 }
}

output is:
a is greater than 4

Tuesday, 21 June 2016

Java Binary Operators Tutorial

                                      Binary Operators

Def: Those operatos which are applied on two(2) operands known as binary operatos.

Type of binary operators:
                        1.Arithmetic operators
                        2.Relational operators
                        3.logical operators
                        4.Shift and bitwise operatos
                        5.Assignment operators

I will discuss about each of the above types with examples.If still you have doubt then comment below..
                          Arithmetic Operators

We all are very familiar with these operators.
Arithmetic operators are further classified in 2 type:
1.Multiplicative   like : *,/,%
2.Additive         like : +,-

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_arithmetic{
 public static void main(String args[]){
  int n1,n2;
  n1=10;
  n2=5;

  int sum,diff;
  sum=n1+n2;
  diff=n1-n2;

  //Additive operators
  System.out.println("n1 + n2 is :"+  sum);
  System.out.println("n1 - n2 is :"+  diff);

  //multiplicative operators
  System.out.println("n1 * n2 is :"+  n1*n2);
  System.out.println("n1 / n2 is :"+  n1/n2);
  System.out.println("n1 % n2 is :"+  n1%n2);


 }
}


output of above code is:
n1 + n2 is :15
n1 - n2 is :5
n1 * n2 is :50
n1 / n2 is :2
n1 % n2 is :0
                                       Relational Operators

Relational operators are also generally classified in two category:
1.Relational operators     like: <, >, <=, >=, instance of
2.Equality operators       like: ==, !=

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_relational{
 public static void main(String args[]){
  
  //Equality operators
  int a,b;
  a=10;
  b=9;

  if(a==b){
   System.out.println("a is equal to b");
  }
  
  if(a!=b){
   System.out.println("a is not equal to b");
  }


  //Relational operators

   if(a<b){
    System.out.println("a is less than b");
   }

   if(a>b){
    System.out.println("a is greater than b");
   }

   if(a<=b){
    System.out.println("a is less than equal to b");
   }

   if(a>=b){
    System.out.println("a is greater than equal to b");
   }


   //instance of 
   //this is used for finding that a object is belong to a class or not

   Binary_relational obj=new Binary_relational();

   if(obj instanceof Binary_relational){
    System.out.println("obj is instance of class Binary_relational");
   }


 }
}

output of above code is:
a is not equal to b
a is greater than b
a is greater than equal to b
obj is instance of class Binary_relational
                    Logical Operators

There are two types of relational operators :
And &&
Or ||

Just read the demo for better understanding  gud luck.
DEMO :


public class Binary_logical{
 public static void main(String args[]){
  //And &&  Or ||

  int a=9;
  int b=8;
  int c=10;

  if(a>b && a>c){
   System.out.println("a is largest among three");
  }

  if(a>b || a >c){
   System.out.println("a is greater than either from a or b or both");
  }
 }
}

output of above code is:

a is greater than either from a or b or both
                                 Shift Operators and Bitwise operators

Shift operators are : <<, >>, >>>
bitwise operators are : bitwise And &,bitwise Or |, bitwise ex-or ^

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_shift_bitwise{
 public static void main(String args[]){

  //shift operators
  int x=10;

  int left_shift=x<<1;
  System.out.println("left shift of x by 1 bit is :" + left_shift);

  int right_shift = x>>1;
  System.out.println("right shift of x by 1 bit is :" + right_shift);

  //>>>(zero filled right shift)
  int zero_filled_right_shift= x >>>1;
  System.out.println("zero_filled_right_shift of x by 1 bit is :" + zero_filled_right_shift);
 
     //bitwise operators
     /*  & (bitwise and)
            Binary AND Operator copies a bit to the result if it exists in both operands.

            Example: (A & B) will give 12 which is 0000 1100
        */

        int a,b;
        a=60;
        b=13;
           int and_result=a&b;
           System.out.println(" a & b is:" +  and_result);

        /* | (bitwise or)
           Binary OR Operator copies a bit if it exists in either operand.

           Example: (A | B) will give 61 which is 0011 1101
        */
            int or_result=a|b;
            System.out.println(" a | b is:" +  or_result);

           /*^ (bitwise XOR)
              Binary XOR Operator copies the bit if it is set in one operand but not both.

              Example: (A ^ B) will give 49 which is 0011 0001

           */
            int xor_result=a^b;
            System.out.println(" a ^ b is:" +  xor_result);

 }
}

output is:

left shift of x by 1 bit is :20
right shift of x by 1 bit is :5
zero_filled_right_shift of x by 1 bit is :5
 a & b is:12
 a | b is:61
 a ^ b is:49
                         Assignment Operators

there are many variations of this like ; =,+=,-=,*=,/=,%=,>>=,<<=,>>>=

a+=b  is same as a=a+b
a-=b is same as a=a-b
and so on

I think there is no need for demo in this if u still have doubt then comment below i will show u demo...

JAVA Operators Tutorials

Hello friends.....
Today I will write about Operatos in Java in the best way so that you can understand this very well.

Q.what are the operators?
Ans.Operators are special symbols, Which applied on single or multiple operands and return some result.

Q.what are the types of Operators in Java?
Ans.there are 3 types of Operators in java.
    1.Unary      
    2.Binary       link:http://spojdev.blogspot.in/2016/06/java-binary-operators-tutorial.html
    3.Ternary      link:http://spojdev.blogspot.in/2016/06/java-ternary-operator.html
                                              
                                            Unary Operators

Definition:operators which are applied on single operand.

Types:
      1.Postfix expr       example: expr++,expr--
      2.Prefix expr        example: ++expr,--expr
      3.logical not        example: !(expr)
      4.bitwise not        example: ~
      5.sign indicator     example: +expr, -expr

Demo:

public class Unary{
 public static void main(String args[]){
 int x=5;

 //postfix expr
 System.out.println("x++ is:"+ x++);
 System.out.println("x-- is:"+ x--);
 System.out.println("x is:"+ x);



 //prefix expr
 System.out.println("++x is:"+ ++x);
 System.out.println("--x is:"+ --x);
 System.out.println("x is:"+ x);

 //logical not
 if(!(x==5)){
  System.out.println("x is not equal to 5");
 }
 else{
  System.out.println("x is equal to 5");
 }


 //bitwise not 
 System.out.println("bitwise not of aa is :" + ~x);



 //sign indicator
 System.out.println("its very simple like " +  -x);
 System.out.println("its very simple like " +  +x);



 }
}



ouput of demo is:

x++ is:5
x-- is:6
x is:5
++x is:6
--x is:5
x is:5
x is equal to 5
bitwise not of aa is :-6
its very simple like -5
its very simple like 5

Friday, 12 February 2016

Welcome to Java(Print "Hello world code")

Hey guys this is the your first program in java for printing any text.
public class Solution {
   
   public static void main(String []argv)
   {
       System.out.println("Hello World.");
       System.out.println("Hello Java.");
   }

}

Saturday, 6 February 2016

HOW TO RUN A JAVA PROGRAM IN UBUNTU

problem:How to run a java program in ubuntu?

Solution: Open JDK is the best.It's simple and i never got any problem with this.

Steps: 
1. Install open jdk using terminal 
 sudo apt-get install openjdk-7-jdk

2. now write your program and save it as filename.java

3. for compilation :
 javac filename.java

4. for run there are two cases:

case 1. if your class containing main() method is public then
 java filename

case 2. if your class containing main() method is not public then 
 java classname

Working With Java Collections