SlideShare a Scribd company logo
NSIT ,JetalpurNSIT ,Jetalpur
CORECORE JAVAJAVA
CONCEPTSCONCEPTS
SURABHISURABHI MISHRAMISHRA
(LCE)(LCE)
NSITNSIT
NSIT ,Jetalpur
Comments are almost like C++Comments are almost like C++
• The javadoc program generates HTML APIThe javadoc program generates HTML API
documentation from the “javadoc” style comments indocumentation from the “javadoc” style comments in
your code.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 ClassesJAVA Classes
• TheThe classclass is the fundamental concept in JAVA (and otheris the fundamental concept in JAVA (and other
OOPLs)OOPLs)
• A class describes some data object(s), and theA class describes some data object(s), and the
operations (or methods) that can be applied to thoseoperations (or methods) that can be applied to those
objectsobjects
• Every object and method in Java belongs to a classEvery object and method in Java belongs to a class
• Classes have data (fields) and code (methods) andClasses have data (fields) and code (methods) and
classes (member classes or inner classes)classes (member classes or inner classes)
• Static methods and fields belong to the class itselfStatic methods and fields belong to the class itself
• Others belong to instancesOthers belong to instances
NSIT ,Jetalpur
An example of a classAn example of a class
class Person { Variable
String name;
int age; Method
void birthday ( )
{
age++;
System.out.println (name +
' is now ' + age);
}
}
NSIT ,Jetalpur
ScopingScoping
As in C/C++, scope is determined by the placement of curly braces {}.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.A variable defined within a scope is available only to the end of that scope.
{ int x = 12;
/* only x available */
{ int q = 96;
/* both x and q available */
}
/* only x available */
/* q “out of scope” */
}
{ int x = 12;
{ int x = 96; /* illegal */
}
}
This is ok in C/C++ but not in Java.
NSIT ,Jetalpur
Scope of ObjectsScope of Objects
• Java objects don’t have the same lifetimes asJava objects don’t have the same lifetimes as
primitives.primitives.
• When you create a Java object usingWhen you create a Java object using newnew, it, it
hangs around past the end of the scope.hangs around past the end of the scope.
• Here, the scope of name s is delimited by the {}sHere, the scope of name s is delimited by the {}s
but the String object hangs around until GC’dbut the String object hangs around until GC’d
{{
String s = newString s = new String("aString("a string");string");
} /* end of scope */} /* end of scope */
NSIT ,Jetalpur
TheThe staticstatic keywordkeyword
• Java methods and variables can be declared staticJava methods and variables can be declared static
• These existThese exist independent of any objectindependent of any object
• This means that a Class’sThis means that a Class’s
– static methods can be calledstatic methods can be called even if no objects of thateven if no objects of that
class have been created andclass have been created and
– static data is “shared” by all instances (i.e., one rvaluestatic data is “shared” by all instances (i.e., one rvalue
per class instead of one per instanceper 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
ExampleExample
public class Circle {public class Circle {
// A class field// A class field
public static final double PI= 3.14159; // A usefulpublic static final double PI= 3.14159; // A useful
constantconstant
// A class method: just compute a value based on the// A class method: just compute a value based on the
argumentsarguments
public static double radiansToDegrees(double rads) {public static double radiansToDegrees(double rads) {
return rads * 180 / PI;return rads * 180 / PI;
}}
// An instance field// An instance field
public double r; // The radius of thepublic double r; // The radius of the
circlecircle
// Two methods which operate on the instance fields of// Two methods which operate on the instance fields of
an objectan object
public double area() { // Compute the area ofpublic double area() { // Compute the area of
the circlethe circle
return PI * r * r;return PI * r * r;
}}
public double circumference() { // Compute thepublic double circumference() { // Compute the
circumference of the circlecircumference of the circle
return 2 * PI * r;return 2 * PI * r;
}}
}}
NSIT ,Jetalpur
Array OperationsArray Operations
• Subscripts always start at 0 as in CSubscripts always start at 0 as in C
• Subscript checking is done automaticallySubscript checking is done automatically
• Certain operations are defined on arraysCertain operations are defined on arrays
of objects, as for other classesof objects, as for other classes
– e.g. myArray.length == 5e.g. myArray.length == 5
NSIT ,Jetalpur
An array is an objectAn array is an object
• Person mary = new Person ( );Person mary = new Person ( );
• int myArray[ ] = new int[5];int myArray[ ] = new int[5];
• int myArray[ ] = {1, 4, 9, 16, 25};int myArray[ ] = {1, 4, 9, 16, 25};
• String languages [ ] = {"Prolog", "Java"};String languages [ ] = {"Prolog", "Java"};
• Since arrays are objects they are allocated dynamicallySince arrays are objects they are allocated dynamically
• Arrays, like all objects, are subject to garbage collectionArrays, like all objects, are subject to garbage collection
when no more references remainwhen no more references remain
– so fewer memory leaksso fewer memory leaks
– Java doesn’t have pointers!Java doesn’t have pointers!
NSIT ,Jetalpur
ExampleExample
ProgramsPrograms
NSIT ,Jetalpur
Echo.javaEcho.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 ExampleFactorial Example
/* This program computes the factorial of a number/* This program computes the factorial of a number
*/*/
public class Factorial { // Define a classpublic class Factorial { // Define a class
public static void main(String[] args) { // The program startspublic static void main(String[] args) { // The program starts
herehere
int input = Integer.parseInt(args[0]); // Get the user'sint input = Integer.parseInt(args[0]); // Get the user's
inputinput
double result = factorial(input); // Compute thedouble result = factorial(input); // Compute the
factorialfactorial
System.out.println(result); // Print out theSystem.out.println(result); // Print out the
resultresult
} // The main() method} // The main() method
ends hereends here
public static double factorial(int x) { // This methodpublic static double factorial(int x) { // This method
computes x!computes x!
if (x < 0) // Check for bad inputif (x < 0) // Check for bad input
return 0.0; // if bad, return 0return 0.0; // if bad, return 0
double fact = 1.0; // Begin with andouble fact = 1.0; // Begin with an
initial valueinitial value
while(x > 1) { // Loop until x equalswhile(x > 1) { // Loop until x equals
fact = fact * x; // multiply by xfact = fact * x; // multiply by x
each timeeach time
x = x - 1; // and thenx = x - 1; // and then
decrement xdecrement x
NSIT ,Jetalpur
ConstructorsConstructors
• Classes should define one or more methods to createClasses should define one or more methods to create
or construct instances of the classor construct instances of the class
• Their name is the same as the class nameTheir name is the same as the class name
– note deviation from convention that methods begin withnote deviation from convention that methods begin with
lower caselower case
• Constructors are differentiated by the number andConstructors are differentiated by the number and
types of their argumentstypes of their arguments
– An example of overloadingAn example of overloading
• If you don’t define a constructor, a default one will beIf you don’t define a constructor, a default one will be
created.created.
• Constructors automatically invoke the zero argumentConstructors automatically invoke the zero argument
constructor of their superclass when they begin (noteconstructor of their superclass when they begin (note
that this yields a recursive process!)that this yields a recursive process!)
NSIT ,Jetalpur
Methods, arguments andMethods, arguments and
return valuesreturn values
• Java methods are like C/C++ functions.Java methods are like C/C++ functions.
General case:General case:
returnTypereturnType methodNamemethodName (( arg1arg1,, arg2arg2, …, … argNargN))
{{
methodBodymethodBody
}}
The return keyword exits a method optionally with a valueThe return keyword exits a method optionally with a value
int storage(String s) {return s.length() * 2;}int storage(String s) {return s.length() * 2;}
boolean flag() { return true; }boolean flag() { return true; }
float naturalLogBase() { return 2.718f; }float naturalLogBase() { return 2.718f; }
void nothing() { return; }void nothing() { return; }
void nothing2() {}void nothing2() {}

More Related Content

PPT
Core Java Concepts
PPT
Core java concepts
PPTX
Core java concepts
PPT
Core java concepts
ODP
Best practices in Java
PDF
Object Oriented Programming in PHP
PDF
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
PPT
Core java
Core Java Concepts
Core java concepts
Core java concepts
Core java concepts
Best practices in Java
Object Oriented Programming in PHP
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
Core java

What's hot (20)

PDF
Procedure Typing for Scala
PDF
PPT
Java tutorial for Beginners and Entry Level
PPT
Java Tutorials
PPT
core java
PPT
java training faridabad
PPT
PDF
Java Day-5
PPT
Java Tut1
ODP
Synapseindia reviews.odp.
PPT
Java 5 Features
PPTX
Java best practices
PPT
Java tutorials
PDF
Java Day-4
PDF
OOPs & Inheritance Notes
ODP
Functional Objects & Function and Closures
PDF
Built in classes in java
PPTX
About Python
PDF
Workshop Scala
Procedure Typing for Scala
Java tutorial for Beginners and Entry Level
Java Tutorials
core java
java training faridabad
Java Day-5
Java Tut1
Synapseindia reviews.odp.
Java 5 Features
Java best practices
Java tutorials
Java Day-4
OOPs & Inheritance Notes
Functional Objects & Function and Closures
Built in classes in java
About Python
Workshop Scala
Ad

Similar to Core java concepts (20)

PPT
Java Concepts
PPT
Core java concepts
PPT
Core Java
PPT
Best Core Java Training In Bangalore
PPT
Corejava Training in Bangalore Tutorial
PPTX
Chap-2 Classes & Methods.pptx
PPTX
Java assignment help
PPTX
Hemajava
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPTX
Java basics
PDF
03_Objects and Classes in java.pdf
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PPT
Defining classes-and-objects-1.0
PPT
Java Basics
 
PPTX
Classes, objects in JAVA
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
PPTX
Class and Object.pptx from nit patna ece department
PPTX
Chapter 8 java
PPT
Lecture 2 classes i
PPT
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
Java Concepts
Core java concepts
Core Java
Best Core Java Training In Bangalore
Corejava Training in Bangalore Tutorial
Chap-2 Classes & Methods.pptx
Java assignment help
Hemajava
Unit 1 Part - 3 constructor Overloading Static.ppt
Java basics
03_Objects and Classes in java.pdf
Class and Object JAVA PROGRAMMING LANG .pdf
Defining classes-and-objects-1.0
Java Basics
 
Classes, objects in JAVA
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Class and Object.pptx from nit patna ece department
Chapter 8 java
Lecture 2 classes i
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
Ad

Core java concepts

  • 1. NSIT ,JetalpurNSIT ,Jetalpur CORECORE JAVAJAVA CONCEPTSCONCEPTS SURABHISURABHI MISHRAMISHRA (LCE)(LCE) NSITNSIT
  • 2. NSIT ,Jetalpur Comments are almost like C++Comments are almost like C++ • The javadoc program generates HTML APIThe javadoc program generates HTML API documentation from the “javadoc” style comments indocumentation from the “javadoc” style comments in your code.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 */
  • 3. NSIT ,Jetalpur JAVA ClassesJAVA Classes • TheThe classclass is the fundamental concept in JAVA (and otheris the fundamental concept in JAVA (and other OOPLs)OOPLs) • A class describes some data object(s), and theA class describes some data object(s), and the operations (or methods) that can be applied to thoseoperations (or methods) that can be applied to those objectsobjects • Every object and method in Java belongs to a classEvery object and method in Java belongs to a class • Classes have data (fields) and code (methods) andClasses have data (fields) and code (methods) and classes (member classes or inner classes)classes (member classes or inner classes) • Static methods and fields belong to the class itselfStatic methods and fields belong to the class itself • Others belong to instancesOthers belong to instances
  • 4. NSIT ,Jetalpur An example of a classAn example of a class class Person { Variable String name; int age; Method void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } }
  • 5. NSIT ,Jetalpur ScopingScoping As in C/C++, scope is determined by the placement of curly braces {}.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.A variable defined within a scope is available only to the end of that scope. { int x = 12; /* only x available */ { int q = 96; /* both x and q available */ } /* only x available */ /* q “out of scope” */ } { int x = 12; { int x = 96; /* illegal */ } } This is ok in C/C++ but not in Java.
  • 6. NSIT ,Jetalpur Scope of ObjectsScope of Objects • Java objects don’t have the same lifetimes asJava objects don’t have the same lifetimes as primitives.primitives. • When you create a Java object usingWhen you create a Java object using newnew, it, it hangs around past the end of the scope.hangs around past the end of the scope. • Here, the scope of name s is delimited by the {}sHere, the scope of name s is delimited by the {}s but the String object hangs around until GC’dbut the String object hangs around until GC’d {{ String s = newString s = new String("aString("a string");string"); } /* end of scope */} /* end of scope */
  • 7. NSIT ,Jetalpur TheThe staticstatic keywordkeyword • Java methods and variables can be declared staticJava methods and variables can be declared static • These existThese exist independent of any objectindependent of any object • This means that a Class’sThis means that a Class’s – static methods can be calledstatic methods can be called even if no objects of thateven if no objects of that class have been created andclass have been created and – static data is “shared” by all instances (i.e., one rvaluestatic data is “shared” by all instances (i.e., one rvalue per class instead of one per instanceper 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
  • 8. NSIT ,Jetalpur ExampleExample public class Circle {public class Circle { // A class field// A class field public static final double PI= 3.14159; // A usefulpublic static final double PI= 3.14159; // A useful constantconstant // A class method: just compute a value based on the// A class method: just compute a value based on the argumentsarguments public static double radiansToDegrees(double rads) {public static double radiansToDegrees(double rads) { return rads * 180 / PI;return rads * 180 / PI; }} // An instance field// An instance field public double r; // The radius of thepublic double r; // The radius of the circlecircle // Two methods which operate on the instance fields of// Two methods which operate on the instance fields of an objectan object public double area() { // Compute the area ofpublic double area() { // Compute the area of the circlethe circle return PI * r * r;return PI * r * r; }} public double circumference() { // Compute thepublic double circumference() { // Compute the circumference of the circlecircumference of the circle return 2 * PI * r;return 2 * PI * r; }} }}
  • 9. NSIT ,Jetalpur Array OperationsArray Operations • Subscripts always start at 0 as in CSubscripts always start at 0 as in C • Subscript checking is done automaticallySubscript checking is done automatically • Certain operations are defined on arraysCertain operations are defined on arrays of objects, as for other classesof objects, as for other classes – e.g. myArray.length == 5e.g. myArray.length == 5
  • 10. NSIT ,Jetalpur An array is an objectAn array is an object • Person mary = new Person ( );Person mary = new Person ( ); • int myArray[ ] = new int[5];int myArray[ ] = new int[5]; • int myArray[ ] = {1, 4, 9, 16, 25};int myArray[ ] = {1, 4, 9, 16, 25}; • String languages [ ] = {"Prolog", "Java"};String languages [ ] = {"Prolog", "Java"}; • Since arrays are objects they are allocated dynamicallySince arrays are objects they are allocated dynamically • Arrays, like all objects, are subject to garbage collectionArrays, like all objects, are subject to garbage collection when no more references remainwhen no more references remain – so fewer memory leaksso fewer memory leaks – Java doesn’t have pointers!Java doesn’t have pointers!
  • 12. NSIT ,Jetalpur Echo.javaEcho.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
  • 13. NSIT ,Jetalpur Factorial ExampleFactorial Example /* This program computes the factorial of a number/* This program computes the factorial of a number */*/ public class Factorial { // Define a classpublic class Factorial { // Define a class public static void main(String[] args) { // The program startspublic static void main(String[] args) { // The program starts herehere int input = Integer.parseInt(args[0]); // Get the user'sint input = Integer.parseInt(args[0]); // Get the user's inputinput double result = factorial(input); // Compute thedouble result = factorial(input); // Compute the factorialfactorial System.out.println(result); // Print out theSystem.out.println(result); // Print out the resultresult } // The main() method} // The main() method ends hereends here public static double factorial(int x) { // This methodpublic static double factorial(int x) { // This method computes x!computes x! if (x < 0) // Check for bad inputif (x < 0) // Check for bad input return 0.0; // if bad, return 0return 0.0; // if bad, return 0 double fact = 1.0; // Begin with andouble fact = 1.0; // Begin with an initial valueinitial value while(x > 1) { // Loop until x equalswhile(x > 1) { // Loop until x equals fact = fact * x; // multiply by xfact = fact * x; // multiply by x each timeeach time x = x - 1; // and thenx = x - 1; // and then decrement xdecrement x
  • 14. NSIT ,Jetalpur ConstructorsConstructors • Classes should define one or more methods to createClasses should define one or more methods to create or construct instances of the classor construct instances of the class • Their name is the same as the class nameTheir name is the same as the class name – note deviation from convention that methods begin withnote deviation from convention that methods begin with lower caselower case • Constructors are differentiated by the number andConstructors are differentiated by the number and types of their argumentstypes of their arguments – An example of overloadingAn example of overloading • If you don’t define a constructor, a default one will beIf you don’t define a constructor, a default one will be created.created. • Constructors automatically invoke the zero argumentConstructors automatically invoke the zero argument constructor of their superclass when they begin (noteconstructor of their superclass when they begin (note that this yields a recursive process!)that this yields a recursive process!)
  • 15. NSIT ,Jetalpur Methods, arguments andMethods, arguments and return valuesreturn values • Java methods are like C/C++ functions.Java methods are like C/C++ functions. General case:General case: returnTypereturnType methodNamemethodName (( arg1arg1,, arg2arg2, …, … argNargN)) {{ methodBodymethodBody }} The return keyword exits a method optionally with a valueThe return keyword exits a method optionally with a value int storage(String s) {return s.length() * 2;}int storage(String s) {return s.length() * 2;} boolean flag() { return true; }boolean flag() { return true; } float naturalLogBase() { return 2.718f; }float naturalLogBase() { return 2.718f; } void nothing() { return; }void nothing() { return; } void nothing2() {}void nothing2() {}