Follow on Facebook

Like and Share on Facebook

Showing posts with label How to set path in Java. Show all posts
Showing posts with label How to set path in Java. Show all posts

Friday, January 23, 2015

Operators in java


Operators in java

Operator in java is a symbol that is used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.

OperatorsPrecedence
postfixexpr++ expr--
unary++expr --expr +expr -expr ~ !
multiplicative* / %
additive+ -
shift<< >> >>>
relational< > <= >= instanceof
equality== !=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
logical AND&&
logical OR||
ternary? :
assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=

Useful Programs:

There is given some useful programs such as factorial number, prime number, fibonacci series etc.

It is better for the freshers to skip this topic and come to it after OOPs concepts.

1) Program of factorial number.

class Operation{  
  
 static int fact(int number){  
  int f=1;  
  for(int i=1;i<=number;i++){  
  f=f*i;  
  }  
 return f;  
 }  
  
 public static void main(String args[]){  
  int result=fact(5);  
  System.out.println("Factorial of 5="+result);  
 }  
}  

2) Program of fibonacci series.

class Fabnoci{  
   
  public static void main(String[] args)  
  {  
    int n=10,i,f0=1,f1=1,f2=0;  
    for(i=1;i<=n;i++)  
    {  
      f2=f0+f1;  
      f0=f1;  
      f1=f2;  
          f2=f0;  
          System.out.println(f2);  
    }  
      
   }  
 }  

3) Program of armstrong number.

class ArmStrong{  
  public static void main(String...args)  
  {  
    int n=153,c=0,a,d;  
    d=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(d==c)  
    System.out.println("armstrong number");   
    else  
    System.out.println("it is not an armstrong number");   
      
   }  
}  

4) Program of checking palindrome number.

class Palindrome  
{  
   public static void main( String...args)  
  {  
   int a=242;  
   int  n=a,b=a,rev=0;  
   while(n>0)  
   {  
     a=n%10;  
     rev=rev*10+a;  
     n=n/10;  
   }  
   if(rev==b)  
   System.out.println("it is Palindrome");  
   else  
   System.out.println("it is not palinedrome");  
    
  }  
}  

5) Program of swapping two numbers without using third variable.

class SwapTwoNumbers{  
public static void main(String args[]){  
int a=40,b=5;  
a=a*b;  
b=a/b;  
a=a/b;  
  
System.out.println("a= "+a);  
System.out.println("b= "+b);  
  
}  
}  

6) Program of factorial number by recursion

class FactRecursion{  
  
static int fact(int n){  
if(n==1)  
return 1;  
  
return n*=fact(n-1);  
}  
  
public static void main(String args[]){  
  
int f=fact(5);  
System.out.println(f);  
}  
}  

Variable and Datatype in Java


Variable and Datatype in Java

In this page, we will learn about the variable and java data types. Variable is a name of memory location. There are three types of variables: local, instance and static. There are two types of datatypes in java, primitive and non-primitive.

Variable
Variable is name of reserved area allocated in memory.


  1. int data=50;//Here data is variable  

Types of Variable

There are three types of variables in java
  • local variable
  • instance variable
  • static variable


Local Variable

A variable that is declared inside the method is called local variable.

Instance Variable

A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.

Static variable

A variable that is declared as static is called static variable. It cannot be local.

We will have detailed learning of these variables in next chapters.

Example to understand the types of variables

class A{  
int data=50;//instance variable  
static int m=100;//static variable  
void method(){  
int n=90;//local variable  
}  
}//end of class 


Data Types in Java

In java, there are two types of data types
  • primitive data types
  • non-primitive data types

Data Type
Default Value
Default size
boolean
false
1 bit
char
'\u0000'
2 byte
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
float
0.0f
4 byte
double
0.0d
8 byte

Why char uses 2 byte in java and what is \u0000 ?

because java uses unicode system rather than ASCII code system. \u0000 is the lowest range of unicode system.To get detail about Unicode see below.

Difference between JDK, JRE and JVM


Difference between JDK, JRE and JVM

Understanding the difference between JDK, JRE and JVM is important in Java. We are having brief overview of JVM here.
If you want to get the detailed knowledge of Java Virtural Machine, move to the next page. Firstly, let's see the basic differences between the JDK, JRE and JVM.

JVM ( Java Virtual Machine )

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent.
The JVM performs following main tasks:
  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment

JRE (Java Runtime Environment )


JRE is an acronym for Java Runtime Environment.It is used to provide runtime environment.It is the implementation of JVM.It physically exists.It contains set of libraries + other files that JVM uses at runtime.
Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.



JDK (Java Development Tool kit )

JDK is an acronym for Java Development Kit.It physically exists.It contains JRE + development tools.

Thursday, January 22, 2015

How to set path in Java

How to set path in Java

The path is required to be set for using tools such as javac, java etc.
If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all the tools will be available in the current directory.
But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.
There are 2 ways to set java path:
  1. temporary
  2. permanent

1) How to set Temporary Path of JDK in Windows

To set the temporary path of JDK, you need to follow following steps:
  • Open command prompt
  • copy the path of jdk/bin directory
  • write in command prompt: set path=copied_path

For Example:

set path=C:\Program Files\Java\jdk1.6.0_23\bin

Let's see it in the figure given below:


2) How to set Permanent Path of JDK in Windows

For setting the permanent path of JDK, you need to follow these steps:
  • Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok

For Example:

1)Go to MyComputer properties

2)click on advanced tab


3)click on environment variables 
 4)click on new tab of user variables



5)write path in variable name


6)Copy the path of bin folder

7)paste path of bin folder in variable value

8)click on ok button


9)click on ok button

Now your permanent path is set.You can now execute any program of java from any drive.

Setting Java Path in Linux OS

Setting the path in Linux OS is same as setting the path in the Windows OS. But here we use export tool rather than set. Let's see how to set path in Linux OS:
export PATH=$PATH:/home/jdk1.6.01/bin/

Here, we have installed the JDK in the home directory under Root (/home).