SlideShare a Scribd company logo
CORE JAVA
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
*/
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
An example of a class
class Person { Variable
String name;
int age; Method
void birthday ( )
{
age++;
System.out.println (name +
' is now ' + age);
}
}
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.
{ 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.
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 */
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
Example
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;
}}
}}
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
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!
Example
Programs
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; // and then
decrement x
NSIT ,Jetalpur
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() {}

More Related Content

PPT
Java Concepts
PPT
Core Java
PPTX
Core java concepts
PPT
Core java concepts
PPT
Best Core Java Training In Bangalore
PPT
PPT
Core Java Concepts
PPT
Object and class
Java Concepts
Core Java
Core java concepts
Core java concepts
Best Core Java Training In Bangalore
Core Java Concepts
Object and class

What's hot (20)

PPT
Java Basics
PPT
Core java concepts
PPT
Object and class in java
PPT
Java basic tutorial
PPT
Class & Object - Intro
PDF
Class and Objects in Java
PPTX
Constructor in java
PDF
ITFT-Classes and object in java
PPTX
Classes in c++ (OOP Presentation)
PDF
Core java complete notes - Contact at +91-814-614-5674
PPT
Lect 1-class and object
PPT
Java Basics
 
PPTX
Class object method constructors in java
PDF
Java OOP Programming language (Part 3) - Class and Object
PPT
Java: Objects and Object References
PDF
Java Methods
PPTX
Java class,object,method introduction
PPT
Java oops PPT
PPT
Core java concepts
PPT
C++ classes
Java Basics
Core java concepts
Object and class in java
Java basic tutorial
Class & Object - Intro
Class and Objects in Java
Constructor in java
ITFT-Classes and object in java
Classes in c++ (OOP Presentation)
Core java complete notes - Contact at +91-814-614-5674
Lect 1-class and object
Java Basics
 
Class object method constructors in java
Java OOP Programming language (Part 3) - Class and Object
Java: Objects and Object References
Java Methods
Java class,object,method introduction
Java oops PPT
Core java concepts
C++ classes
Ad

Similar to Core java (20)

PPT
Core java concepts
PPT
Corejava Training in Bangalore Tutorial
PPTX
Java assignment help
PPTX
Chap-2 Classes & Methods.pptx
PPT
Lecture 2 classes i
PPTX
Hemajava
PPT
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPTX
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
PDF
03_Objects and Classes in java.pdf
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PDF
LECTURE 4 CLASSES AND OBJECTS.pdf
PPTX
Class and Object.pptx from nit patna ece department
PPTX
Class and Object.pptx
PPT
Defining classes-and-objects-1.0
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
PPTX
Object oriented concepts
PPT
Jacarashed-1746968053-300050282-Java.ppt
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
PPT
Core Java unit no. 1 object and class ppt
Core java concepts
Corejava Training in Bangalore Tutorial
Java assignment help
Chap-2 Classes & Methods.pptx
Lecture 2 classes i
Hemajava
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
Unit 1 Part - 3 constructor Overloading Static.ppt
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
03_Objects and Classes in java.pdf
Class and Object JAVA PROGRAMMING LANG .pdf
LECTURE 4 CLASSES AND OBJECTS.pdf
Class and Object.pptx from nit patna ece department
Class and Object.pptx
Defining classes-and-objects-1.0
JAVA Class Presentation.pdf Vsjsjsnheheh
Object oriented concepts
Jacarashed-1746968053-300050282-Java.ppt
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Core Java unit no. 1 object and class ppt
Ad

More from Rajkattamuri (20)

PPTX
Github plugin setup in anypointstudio
PPTX
For each component in mule
PPTX
Filter expression in mule
PPTX
File component in mule
PPTX
Database component in mule
PPTX
Choice component in mule
PPT
WebServices
PPTX
Java Basics in Mule
PPTX
WebServices Basic Overview
PPTX
Java For Begineers
PPT
WebServices Basics
PPT
WebServices SOAP WSDL and UDDI
PPTX
Web services soap
PPTX
Web services wsdl
PPTX
Web services uddi
PPT
PPTX
Mule esb dataweave
PPTX
Mule with drools
PPTX
Mule with quartz
PPTX
Mule with rabbitmq
Github plugin setup in anypointstudio
For each component in mule
Filter expression in mule
File component in mule
Database component in mule
Choice component in mule
WebServices
Java Basics in Mule
WebServices Basic Overview
Java For Begineers
WebServices Basics
WebServices SOAP WSDL and UDDI
Web services soap
Web services wsdl
Web services uddi
Mule esb dataweave
Mule with drools
Mule with quartz
Mule with rabbitmq

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

Core java

  • 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 */
  • 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
  • 4. An example of a class class Person { Variable String name; int age; Method void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } }
  • 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. { 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. 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 */
  • 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
  • 8. Example 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. 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
  • 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!
  • 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; // and then decrement x NSIT ,Jetalpur
  • 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() {}