Programming language developed for the Web.
About 3 billion Devices using Java
Java is used in Embedded devices, Mobile phones, Enterprise Servers, Super computers, Web Servers and Enterprise Applications
Java applications are compiled to class file ( bytecode ) that can run on any Java virtual machine (JVM)
The success of Java is because of its write once and run anywhere
JDK is bundle of software that you can use to develop Java based software
JRE is an implementation of the Java Virtual Machine which actually executes Java programs
JAVA JDK Installtion
Download Java JDK
Set JAVA_HOME in Environment Variaible with JDk Installation Directory
set Environment path with JDk bin Directory
In Java the name of the source file should be followed by the .java (dot java) extension.
>Write program shown below and save as Demo.java
Program 1
// Writing my first Java Application program
// Your first Java application:
/*
This is a simple Java program
Call this file "Demo.java".
*/
public class Demo {
// Your program begins with a call to main.
public static void main (String args [ ] ) {
System.out.println ( "This is a simple Java program.");
}
}
Compile the Program
To compile the example program, execute the command javac followed by filename
C:\>javac Demo.java
The javac compiler creates a file called Demo.class that contains the bytecode version of the program.
Running the Program
To actually run the program, you must use the java interpreter, called java. It is as shown below/p>
C:\>java Demo
When the program is run, the following output is displayed:
This is a simple Java program.
Program 2
public class Demo2 {
int a=5;
int b=10;
public static void main(String arg[]) {
Demo2 myDemo=new Demo2();
System.out.println("Sum:"+ myDemo.add());
//System.out.println("Sub:"+ myDemo.sub());
//System.out.println("Mul:"+ myDemo.mul());
//System.out.println("Div:"+ myDemo.div());
}
public int add() { return a+b; }
//public int sub() { return a-b; }
//public int mul() { return a*b; }
//public int div() { return a/b; }
}
Programs 3
public class ControlStructure {
public static void main ( String[] args) {
ControlStructure comp = new ControlStructure();
comp.compare(3,5);
comp.prime(100);
comp.checkPoly("hello");
comp.findMax(1,6);
}
public void compare( int x, int y){
if( x > y) System.out.println( " X is greater than y");
else if ( x < y ) System.out.println( " X is less than y");
else System.out.println( " Both X and y are equal");
}
public void prime(int num) {
int countPrime=0;
System.out.println("Prime Numbers");
for ( int i = 0; i < num; i++ ) {
if( i %2 !=0 && i %3 != 0 ) { System.out.println(i); countPrime++; }
}
System.out.println("Total No of Prime: " + countPrime);
}
public void checkPoly( String word) {
int len = word.length();
boolean flag=true;
System.out.println("No of Chars in word:" + len);
for ( int i = 0; i < len; i++ ) {
System.out.println( word.charAt(i) + ":" + word.charAt(len-1 -i));
if(word.charAt(i) != word.charAt(len-1 -i) ) { flag=false; break; }
}
if( flag == false ) System.out.println("Not a ploy");
else System.out.println(" ploy");
}
public void findMax( int a , int b) {
int max = (a > b) ? a : b;
System.out.println("max of two no :" + max);
}
}
Progoram 4
Program Using Switch
public class MySwitch {
public static final int YELLOW=1;
public static final int BROWN=2;
public static final int RED=3;
public static final int GREEN=4;
public static final int ORANGE=5;
public static final int NAVYBLUE=6;
public void selectFruits(int color) {
System.out.println("Selected fruit based on color" + color);
switch(color) {
case YELLOW: System.out.println("Banana"); break;
case BROWN: System.out.println("TATININGU"); break;
case RED: System.out.println("APPLE"); break;
case GREEN: System.out.println("GUAVA"); break;
case ORANGE: System.out.println("ORANGE"); break;
case NAVYBLUE: System.out.println("GRAPE"); break;
default: System.out.println("Not a valid color");
}
}
public static void main(String[] args) {
MySwitch sw = new MySwitch();
sw.selectFruits(YELLOW);
sw.selectFruits(2);
}
}
Program 5
Wrting Recursive Functions
public class RecursiveFunctions {
int findFact(int m)
{
if(m==0)return 1;
return m*findFact(m-1);
}
public static void main(String args[]){
RecursiveFunctions mydoc= new RecursiveFunctions();
int FACT=mydoc.findFact(6);
System.out.println("The factorial is--"+FACT);
mydoc.countDown(10);
mydoc.countUp(10,20);
System.out.println(" Is polindrone:" + mydoc.poly("hello"));
}
public void countDown( int n ) {
if( n <= 0) return;
else {
System.out.println(n);
countDown(--n);
}
}
public void countUp( int start, int end) {
if(start>=end) return;
else {
System.out.println(start);
countUp(start++, end);
}
}
}