You must set path variable to compile and run java programs as follows:
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.7.0_25\bin
then must restart your computer.
to run java program type :
c:\myjavaprog>javac hello.java
The javac compiler creates a file called hello.class that contains the bytecode version of the program.
The java bytecode is the intermediate representation of your progrtam that contains instuction the java interpreter will execute.
So the output of javac, bytecode is not code that can be directly executed.
To run the java program, you must use the java interpreter, called java.exe.
c:\myjavaprog>java hello
or to take a output into a file:
c:\myjavaprog>java hello > fl.txt
Escape Sequence is used to print
=============== ================
\' Single quote
\" Double quote
\n New line
\f Form Feed
\b Backspace
\\ Backslash
\t Tab
\r Carriage return
\ddd Character in Octal format
\uxxx Unicode character in hexadecimal format
//example of double data type
class dd {
public static void main(String ar[]) {
double a, r, p;
r=5; p = 3.1416;
a = r * r * p;
System.out.println("Area of circle is :" + a);
}
}
to compile :
c:\mayjavaprog>javac dd.java
to run
c:\mayjavaprog>java dd
output :
Area of circle is :78.53999999999999
/*char data type
Java uses Unicode to represent characters.
In Java char is 16 bit.
*/
class cc {
public static void main(String s[]) {
char x,y;
x = 'A';
y = 65;
System.out.println("Value of x="+x);
System.out.println("Value of y="+y);
}
}
to compile :
c:\mayjavaprog>javac cc.java
to run
c:\mayjavaprog>java cc
output :
Value of x=A
Value of y=A
/*boolean data type
Java uses boolean data type to store true or false values.
*/
class bo {
public static void main(String s[]) {
boolean b;
b= true;
if(b==true)
System.out.println("Value of b="+b);
else
System.out.println("Value of b="+b);
}
}
to compile :
c:\mayjavaprog>javac bo.java
to run
c:\mayjavaprog>java bo
output :
Value of b=true
/*Array
Java uses array to store similar values under same name.
*/
class ar {
public static void main(String s[]) {
int no[];
no = new int[5];
no[0] = 100;
no[1] = 200;
no[2] = 300;
no[3] = 400;
no[4] = 500;
int a[]={5,4,6};
System.out.println("Value of no[0]"+no[0]);
System.out.println("Value of no[1]"+no[1]);
System.out.println("Value of no[2]"+no[2]);
System.out.println("Value of no[3]"+no[3]);
System.out.println("Value of no[4]"+no[4]);
System.out.println("");
System.out.println("Value of a[0]"+a[0]);
System.out.println("Value of a[1]"+a[1]);
System.out.println("Value of a[2]"+a[2]);
}
}
to compile :
c:\mayjavaprog>javac ar.java
to run
c:\mayjavaprog>java ar
output :
Value of no[0]100
Value of no[1]200
Value of no[2]300
Value of no[3]400
Value of no[4]500
Value of a[0]5
Value of a[1]4
Value of a[2]6
/*Array
Java uses two dimensional array to store similar values as a table.
*/
class tdar {
public static void main(String s[]) {
int no[][]; //declare no as a int array 2 rows 3 coloums
no = new int[2][3]; //acquire memory for no 2 x 3
//store values in it
no[0][0] = 100;
no[0][1] = 200;
no[0][2] = 300;
no[1][0] = 10;
no[1][1] = 20;
no[1][2] = 30;
System.out.println("Value of no[0][0]= "+no[0][0]);
System.out.println("Value of no[0][1]= "+no[0][1]);
System.out.println("Value of no[0][2]= "+no[0][2]);
System.out.println("Value of no[1][0]= "+no[1][0]);
System.out.println("Value of no[1][1]= "+no[1][1]);
System.out.println("Value of no[1][2]= "+no[1][2]);
}
}
to compile :
c:\mayjavaprog>javac ar.java
to run
c:\mayjavaprog>java ar
output :
Value of no[0][0]= 100
Value of no[0][1]= 200
Value of no[0][2]= 300
Value of no[1][0]= 10
Value of no[1][1]= 20
Value of no[1][2]= 30
You can declare array as follows:
int no[]= new int[5];
int ar[][] = new int[2][3];
-----------------------------------------------
Arithmetci Operators
Operator Meaning
======= =======
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Division
+= Addition and then assignment
++ Increment by one
-= Subtraction and then assignment
-- Decrement by one
*= Multiplication and then assignment
/= Division and then assignment
%= Modulus and then assignment.
/*
Operator Illustration.
*/
class op {
public static void main(String s[]) {
int a,b,c; //variable declaration.
a=50; b=8;
c= a+b;
System.out.println("a+b= "+c);
c= a-b;
System.out.println("a-b= "+c);
c= a*b;
System.out.println("a*b= "+c);
c= a/b;
System.out.println("a/b= "+c);
c= a%b;
System.out.println("a%b= "+c);
c += 1 ; // c = c + 1
System.out.println("c += 1 = "+c);
c -= 1; // c = c - 1
System.out.println("c -= 1 = "+c);
c *= 2; //c= c * 2
System.out.println("c *= 2 = "+c);
c /= 3; //c= c / 3
System.out.println("c /= 3 = "+c);
c++ ; //c= c + 1
System.out.println("c++ = "+c);
c-- ; //c= c - 1
System.out.println("c-- = "+c);
}
}
to compile :
c:\mayjavaprog>javac op.java
to run
c:\mayjavaprog>java op
output :
a+b= 58
a-b= 42
a*b= 400
a/b= 6
a%b= 2
c += 1 = 3
c -= 1 = 2
c *= 2 = 4
c /= 3 = 1
c++ = 2
c-- = 1