Showing posts with label java for cs. Show all posts
Showing posts with label java for cs. Show all posts

Friday, 30 January 2015

Java (programming language)



  • Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another.
  • Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. Java is, as of 2015, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 9 million developers.
  • Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.






  • The original and reference implementation Java compilers, virtual machines, and class libraries were originally released by Sun under proprietary licences. As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. 
  • Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java (bytecode compiler), GNU Classpath (standard libraries), and IcedTea-Web (browser plugin for applets).

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);  
}  
}  

Unicode System

Unicode is a universal international standard character encoding that is capable of representing most of the world's written languages.

Why java uses Unicode System?

Before Unicode, there were many language standards:
  • ASCII (American Standard Code for Information Interchange) for the United States.
  • ISO 8859-1 for Western European Language.
  • KOI-8 for Russian.
  • GB18030 and BIG-5 for chinese, and so on.
This caused two problems:
  1. A particular code value corresponds to different letters in the various language standards.
  2. The encodings for languages with large character sets have variable length.Some common characters are encoded as single bytes, other require two or more byte.
To solve these problems, a new language standard was developed i.e. Unicode System.
In unicode, character holds 2 byte, so java also uses 2 byte for characters.
lowest value:\u0000
highest value:\uFFFF

Types of Java Applications.

There are mainly 4 type of applications that can be created using java programming:

1) Standalone Application

It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications.

2) Web Application

An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.

3) Enterprise Application

An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.

4) Mobile Application

An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.

What is Java ?

Java is a programming language and a platform.
Java is a high level, robust, secured and object-oriented programming language.
Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform.


Java Example

Let's have a quick look at java programming example. A detailed description of hello java example is given in next page.

class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}