SlideShare una empresa de Scribd logo
1 de 15
COREJAVA
CONCEPTS
  SURABHI MISHRA
      (LCE)
       NSIT


       NSIT ,Jetalpur
Comments are almost like C++
• The javadoc program generates HTML API
  documentation from the “javadoc” style comments in
  your code.

 /* This kind comment can span multiple lines */
 // This kind is of to the end of the line
/* This kind of comment is a special
     * ‘javadoc’ style comment
     */

                    NSIT ,Jetalpur
JAVA Classes
• The class is the fundamental concept in JAVA (and other
    OOPLs)
•   A class describes some data object(s), and the
    operations (or methods) that can be applied to those
    objects
•   Every object and method in Java belongs to a class
•   Classes have data (fields) and code (methods) and
    classes (member classes or inner classes)
•   Static methods and fields belong to the class itself
•   Others belong to instances


                     NSIT ,Jetalpur
An example of a class
    class Person {                  Variable
        String name;
             int age;               Method

     void birthday ( )
    {
       age++;
       System.out.println (name +
       ' is now ' + age);
     }
}

                   NSIT ,Jetalpur
Scoping
   As in C/C++, scope is determined by the placement of curly braces {}.
A variable defined within a scope is available only to the end of that scope.



                                               This is ok in C/C++ but not in Java.
{ int x = 12;
  /* only x available */
     { int q = 96;
        /* both x and q available */          { int x = 12;
       }                                        { int x = 96; /* illegal */
     /* only x available */                      }
     /* q “out of scope” */                    }
   }




                           NSIT ,Jetalpur
Scope of Objects
• Java objects don’t have the same lifetimes as
    primitives.
•   When you create a Java object using new , it
    hangs around past the end of the scope.
•   Here, the scope of name s is delimited by the {}s
    but the String object hangs around until GC’d
    {
       String s = new String("a string");
    } /* end of scope */



                  NSIT ,Jetalpur
The         static keyword
•   Java methods and variables can be declared static
•   These exist independent of any object
•   This means that a Class’s
     – static methods can be called even if no objects of that
       class have been created and
     – static data is “shared” by all instances (i.e., one rvalue
       per class instead of one per instance
               class StaticTest {static int i = 47;}
               StaticTest st1 = new StaticTest();
               StaticTest st2 = new StaticTest();
               // st1.i == st2.I == 47
               StaticTest.i++;         // or st1.I++ or
               st2.I++
               // st1.i == st2.I == 48
                       NSIT ,Jetalpur
Example
public class Circle {
  // A class field
  public static final double PI= 3.14159;      // A useful
  constant
    // A class method: just compute a value based on the
  arguments
  public static double radiansToDegrees(double rads) {
     return rads * 180 / PI;
  }
  // An instance field
  public double r;                   // The radius of the
  circle
    // Two methods which operate on the instance fields of
    an object
    public double area() {            // Compute the area of
    the circle
      return PI * r * r;
    }
    public double circumference() {   // Compute the
    circumference of the circle
      return 2 * PI * r;
    }                 NSIT ,Jetalpur
}
Array Operations
•   Subscripts always start at 0 as in C
•   Subscript checking is done automatically
•   Certain operations are defined on arrays
    of objects, as for other classes
    – e.g. myArray.length == 5




                 NSIT ,Jetalpur
An array is an object
•   Person mary = new Person ( );
•   int myArray[ ] = new int[5];
•   int myArray[ ] = {1, 4, 9, 16, 25};
•   String languages [ ] = {"Prolog", "Java"};

• Since arrays are objects they are allocated dynamically
• Arrays, like all objects, are subject to garbage collection
    when no more references remain
    – so fewer memory leaks
    – Java doesn’t have pointers!


                      NSIT ,Jetalpur
Example
Programs

 NSIT ,Jetalpur
Echo.java
•   C:UMBC331java>type echo.java
•   // This is the Echo example from the Sun tutorial
•   class echo {
•     public static void main(String args[]) {
•       for (int i=0; i < args.length; i++) {
•         System.out.println( args[i] );
•       }
•     }
•   }


• C:UMBC331java>javac echo.java

•   C:UMBC331java>java echo this is pretty silly
•   this
•   is
•   pretty
•   silly
                        NSIT ,Jetalpur
Factorial Example
/* This program computes the factorial of a number
 */
public class Factorial {                    // Define a class
  public static void main(String[] args) { // The program starts
   here
     int input = Integer.parseInt(args[0]); // Get the user's
   input
     double result = factorial(input);      // Compute the
   factorial
     System.out.println(result);            // Print out the
   result
  }                                         // The main() method
   ends here
  public static double factorial(int x) { // This method
   computes x!
     if (x < 0)                             // Check for bad input
       return 0.0;                          //   if bad, return 0
     double fact = 1.0;                     // Begin with an
   initial value
     while(x > 1) {                         // Loop until x equals
       fact = fact * x;                     //   multiply by x
   each time
       x = x - 1;       NSIT ,Jetalpur      //   and then
   decrement x
Constructors
• Classes should define one or more methods to create
    or construct instances of the class
•   Their name is the same as the class name
    – note deviation from convention that methods begin with
      lower case
• Constructors are differentiated by the number and
    types of their arguments
    – An example of overloading
• If you don’t define a constructor, a default one will be
    created.
•   Constructors automatically invoke the zero argument
    constructor of their superclass when they begin (note
    that this yields a recursive process!)
                       NSIT ,Jetalpur
Methods, arguments and

      return values
• Java methods are like C/C++ functions.
   General case:
       returnType methodName ( arg1, arg2, … argN)
            {
                methodBody
             }
The return keyword exits a method optionally with a value
   int storage(String s) {return s.length() * 2;}
   boolean flag() { return true; }
   float naturalLogBase() { return 2.718f; }
   void nothing() { return; }
   void nothing2() {}


                   NSIT ,Jetalpur

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Objective c
Objective cObjective c
Objective c
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Ios development
Ios developmentIos development
Ios development
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 
Java
JavaJava
Java
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
 

Destacado

Designing JEE Application Structure
Designing JEE Application StructureDesigning JEE Application Structure
Designing JEE Application Structureodedns
 
Webservices testing using SoapUI
Webservices testing using SoapUIWebservices testing using SoapUI
Webservices testing using SoapUITesting World
 
Web Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI ToolWeb Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI ToolSperasoft
 
Testing web services
Testing web servicesTesting web services
Testing web servicesTaras Lytvyn
 

Destacado (6)

Java Programming Assignment
Java Programming AssignmentJava Programming Assignment
Java Programming Assignment
 
Boolean Guidance
Boolean GuidanceBoolean Guidance
Boolean Guidance
 
Designing JEE Application Structure
Designing JEE Application StructureDesigning JEE Application Structure
Designing JEE Application Structure
 
Webservices testing using SoapUI
Webservices testing using SoapUIWebservices testing using SoapUI
Webservices testing using SoapUI
 
Web Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI ToolWeb Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI Tool
 
Testing web services
Testing web servicesTesting web services
Testing web services
 

Similar a Core java concepts

Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalorerajkamaltibacademy
 
Java Basics
Java BasicsJava Basics
Java BasicsF K
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3Sónia
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.pptKhizar40
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 

Similar a Core java concepts (20)

Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
Java basic
Java basicJava basic
Java basic
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Java
JavaJava
Java
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
core java
core javacore java
core java
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Java Methods
Java MethodsJava Methods
Java Methods
 

Core java concepts

  • 1. COREJAVA CONCEPTS SURABHI MISHRA (LCE) NSIT NSIT ,Jetalpur
  • 2. Comments are almost like C++ • The javadoc program generates HTML API documentation from the “javadoc” style comments in your code. /* This kind comment can span multiple lines */ // This kind is of to the end of the line /* This kind of comment is a special * ‘javadoc’ style comment */ NSIT ,Jetalpur
  • 3. JAVA Classes • The class is the fundamental concept in JAVA (and other OOPLs) • A class describes some data object(s), and the operations (or methods) that can be applied to those objects • Every object and method in Java belongs to a class • Classes have data (fields) and code (methods) and classes (member classes or inner classes) • Static methods and fields belong to the class itself • Others belong to instances NSIT ,Jetalpur
  • 4. An example of a class class Person { Variable String name; int age; Method void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } } NSIT ,Jetalpur
  • 5. Scoping As in C/C++, scope is determined by the placement of curly braces {}. A variable defined within a scope is available only to the end of that scope. This is ok in C/C++ but not in Java. { int x = 12; /* only x available */ { int q = 96; /* both x and q available */ { int x = 12; } { int x = 96; /* illegal */ /* only x available */ } /* q “out of scope” */ } } NSIT ,Jetalpur
  • 6. Scope of Objects • Java objects don’t have the same lifetimes as primitives. • When you create a Java object using new , it hangs around past the end of the scope. • Here, the scope of name s is delimited by the {}s but the String object hangs around until GC’d { String s = new String("a string"); } /* end of scope */ NSIT ,Jetalpur
  • 7. The static keyword • Java methods and variables can be declared static • These exist independent of any object • This means that a Class’s – static methods can be called even if no objects of that class have been created and – static data is “shared” by all instances (i.e., one rvalue per class instead of one per instance class StaticTest {static int i = 47;} StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); // st1.i == st2.I == 47 StaticTest.i++; // or st1.I++ or st2.I++ // st1.i == st2.I == 48 NSIT ,Jetalpur
  • 8. Example public class Circle { // A class field public static final double PI= 3.14159; // A useful constant // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; } // An instance field public double r; // The radius of the circle // Two methods which operate on the instance fields of an object public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference of the circle return 2 * PI * r; } NSIT ,Jetalpur }
  • 9. Array Operations • Subscripts always start at 0 as in C • Subscript checking is done automatically • Certain operations are defined on arrays of objects, as for other classes – e.g. myArray.length == 5 NSIT ,Jetalpur
  • 10. An array is an object • Person mary = new Person ( ); • int myArray[ ] = new int[5]; • int myArray[ ] = {1, 4, 9, 16, 25}; • String languages [ ] = {"Prolog", "Java"}; • Since arrays are objects they are allocated dynamically • Arrays, like all objects, are subject to garbage collection when no more references remain – so fewer memory leaks – Java doesn’t have pointers! NSIT ,Jetalpur
  • 12. Echo.java • C:UMBC331java>type echo.java • // This is the Echo example from the Sun tutorial • class echo { • public static void main(String args[]) { • for (int i=0; i < args.length; i++) { • System.out.println( args[i] ); • } • } • } • C:UMBC331java>javac echo.java • C:UMBC331java>java echo this is pretty silly • this • is • pretty • silly NSIT ,Jetalpur
  • 13. Factorial Example /* This program computes the factorial of a number */ public class Factorial { // Define a class public static void main(String[] args) { // The program starts here int input = Integer.parseInt(args[0]); // Get the user's input double result = factorial(input); // Compute the factorial System.out.println(result); // Print out the result } // The main() method ends here public static double factorial(int x) { // This method computes x! if (x < 0) // Check for bad input return 0.0; // if bad, return 0 double fact = 1.0; // Begin with an initial value while(x > 1) { // Loop until x equals fact = fact * x; // multiply by x each time x = x - 1; NSIT ,Jetalpur // and then decrement x
  • 14. Constructors • Classes should define one or more methods to create or construct instances of the class • Their name is the same as the class name – note deviation from convention that methods begin with lower case • Constructors are differentiated by the number and types of their arguments – An example of overloading • If you don’t define a constructor, a default one will be created. • Constructors automatically invoke the zero argument constructor of their superclass when they begin (note that this yields a recursive process!) NSIT ,Jetalpur
  • 15. Methods, arguments and return values • Java methods are like C/C++ functions. General case: returnType methodName ( arg1, arg2, … argN) { methodBody } The return keyword exits a method optionally with a value int storage(String s) {return s.length() * 2;} boolean flag() { return true; } float naturalLogBase() { return 2.718f; } void nothing() { return; } void nothing2() {} NSIT ,Jetalpur