Java Programming Language
Hello World Problem in java
- High Level
- Strongly Typed means no automatic type conversions in java like C++
- Case Sensitive
- Successor of c++
- Pure Object Oriented Language
Hello World Problem in java
class Test{
public static void main(String a[]) {
System.out.println("Hello World");
}
}
More than one Classes
public static void main(String a[]) {
System.out.println("Hello World");
}
}
More than one Classes
class Test1{
void print () {
System.out.println("Test class");
}
}
class Test{
public static void main(String a[]) {
Test1 t=new Test1();
t.print();
}
}
void print () {
System.out.println("Test class");
}
}
class Test{
public static void main(String a[]) {
Test1 t=new Test1();
t.print();
}
}
Rule of Thumb: Don't write extra functions in driver class
Extra function and data in driver class
class Test{
int sq(int p){
return p*p;
}
public static void main(String a[]) {
Test t = new Test();
System.out.println(t.sq(4));
}
}
..................................
call without object instantiation
..................................
class Test{
static int sq(int p){
return p*p;
}
public static void main(String a[]) {
System.out.println(sq(4));
}
}
.....................................
class Test{
int sq(int p){
return p*p;
}
public static void main(String a[]) {
Test t = new Test();
System.out.println(t.sq(4));
}
}
..................................
call without object instantiation
..................................
class Test{
static int sq(int p){
return p*p;
}
public static void main(String a[]) {
System.out.println(sq(4));
}
}
.....................................
Math Class Example
class MyMath{
int sq(int p){
return p*p;
}
int power(int x , int y){
if (y==0)
return 1;
else
return x * power(x,y-1);
}
}
class Test{
public static void main(String a[]) {
MyMath m= new MyMath();
System.out.println(m.sq(4));
System.out.println(m.power(2,3));
}
}
.....................................
class MyMath{
int sq(int p){
return p*p;
}
int power(int x , int y){
if (y==0)
return 1;
else
return x * power(x,y-1);
}
}
class Test{
public static void main(String a[]) {
MyMath m= new MyMath();
System.out.println(m.sq(4));
System.out.println(m.power(2,3));
}
}
.....................................
calling methods without object instantiation
..................................
class MyMath{
static int sq(int p){
return p*p;
}
static int power(int x, int y){
if (y==0)
return 1;
else
return x * power(x,y-1);
}
}
class Test{
public static void main(String a[]) {
System.out.println(MyMath.sq(4));
System.out.println(MyMath.power(2,3));
}
}
class MyMath{
static int sq(int p){
return p*p;
}
static int power(int x, int y){
if (y==0)
return 1;
else
return x * power(x,y-1);
}
}
class Test{
public static void main(String a[]) {
System.out.println(MyMath.sq(4));
System.out.println(MyMath.power(2,3));
}
}
Assalam o Alaikum i see this blog i think its so informatic and learning more for students ,thanx a lot
ReplyDeleteWelcome.. Muhammad Imran Afzal..
DeleteVery handy and informatic :)
ReplyDeleteWelcome Sirleny (Sisplace )And Keep. Visiting.. :)
Delete