Skip to main content

Java Basics Quick Review with examples

Java Programming Language
  • 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
class Test1{
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));
 }

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

Comments

  1. Assalam o Alaikum i see this blog i think its so informatic and learning more for students ,thanx a lot

    ReplyDelete
  2. Replies
    1. Welcome Sirleny (Sisplace )And Keep. Visiting.. :)

      Delete

Post a Comment

Popular posts from this blog

Generalization in java with examples

      Generalization basic points  Type/Subtype Relationship  "IS A " Relationship  One class has many subclasses   General class can have many Special classes as  subclasses  General names can be used to Address special names  Top to bottom hierarchy goes towards specialization  Bottom to top Hierarchy goes towards generalization e.g         Soap     Bath Soap and Washing Soap is a subclass of Soap e.g COldDrink   Pepsi      Team -Cold Drink is a general class -Pepsi and team are special classes e.g    Person Teacher       Student e.g            Car     Sedan           Hatchback Honda   Suzuki     Suzuki   Toyota civic        Liana      Alto      vitz ....................................................\ Some Examples from Nature e.g.               Animal            Mammals            Reptiles    Human           Cow     Snake         Croc Student Teacher             Cobra Python     e.g                     Fruits      Mango      

Composition in Java with code examples

Composition  "Have a " Relationship  OR  "Must have" relationship                    between two or more classes. Container class contains component's class object For example a  Car is composed of engine and body. Where Car is a container class and both Engine and Body are components class.  Life Time Issue in Composition Life time of component objects depends on container class If Container object destroyed, component objects will be also destroyed Car Example  There is a composition relationship between car's body and engine. Car must have Engine and body If Car destroyed, both Engine and Body will be destroyed class  Engine {   Engine  () {    System.out.println ( "Engine created" ) ; }   void  start () {    System.out.println ( "Engine Start" ) ;   } } class  Body {   Body  () { System.out.println ( "Body created" ) ; }   void  type () {    System.out.println ( "Sa

Aggregation in java with example codes

What is aggregation?   "Have a" or "May have" relationship  Aggregation is special type of association  Life time of Component object is independent from its  container object E.g A school has one student class  Student  { String name; Student  () { name= null ;  } Student  ( String n ) { name=n;  } void  setName ( String n ) { name=n; } String getName () {  return  name; } } class  School { Student  st; School  (  ) {  st= null ; } School  ( Student s ){ st=s; } void  studentName (){    System.out.println ( st.getName ()) ; } } class  Post { public static  void  main  ( String a []){ Student st= new  Student ( "Imran" ) ; School s=  new  School ( st ) ; s.studentName () ; } } Output Hello Problem : A Car must has Engine and body.           A Car may or may not has Ac Adding Ac as an Aggregate object in car class  Engine { void  start () { System.out.println ( &