Java

J
UMBC CMSC 331 Java
JAVA BASICSJAVA BASICS
Presented ByPresented By
UMBC CMSC 331 Java
Comments are almost like C++Comments are almost like C++
The javadoc program generates HTML API
documentation from the “javadoc” style
comments in your code.
2
/* This kind of comment can span multiple lines */
// This kind is to the end of the line
/**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
UMBC CMSC 331 Java
An example of a classAn example of a class
3
class Person {
String name;
int age;
void birthday ( ) {
age++;
System.out.println (name +
' is now ' + age);
}
}
Variable
Method
UMBC CMSC 331 Java
ScopingScoping
 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.
4
{ 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.
UMBC CMSC 331 Java
An array is an objectAn 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!
5
UMBC CMSC 331 Java
Scope of ObjectsScope 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"); 6
UMBC CMSC 331 Java
Methods, arguments and return valuesMethods, 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() {}
7
UMBC CMSC 331 Java
The static keywordThe 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
8
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
UMBC CMSC 331 Java
Array OperationsArray 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
9
UMBC CMSC 331 Java
ExampleExample
ProgramsPrograms
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
C:UMBC331java>
UMBC CMSC 331 Java
Factorial ExampleFactorial 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 1
fact = fact * x; // multiply by x each time
x = x - 1; // and then decrement x
} // Jump back to the star of
loop
return fact; // Return the result 12
From Java in a Nutshell
UMBC CMSC 331 Java
JAVA ClassesJAVA 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
13
UMBC CMSC 331 Java
ExampleExample
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;
}
}
14
UMBC CMSC 331 Java
ConstructorsConstructors
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!)
15
UMBC CMSC 331 Java
Constructor exampleConstructor example
public class Circle {
public static final double PI = 3.14159; // A constant
public double r; // instance field holds circle’s radius
// The constructor method: initialize the radius field
public Circle(double r) { this.r = r; }
// Constructor to use if no arguments
public Circle() { r = 1.0; }
// better: public Circle() { this(1.0); }
// The instance methods: compute values based on radius
public double circumference() { return 2 * PI * r; }
public double area() { return PI * r*r; }
}
16
this.r refers to the r
field of the class
This() refers to a
constructor for the class
UMBC CMSC 331 Java
Extending a classExtending a class
 Class hierarchies reflect subclass-superclass relations among
classes.
 One arranges classes in hierarchies:
◦ A class inherits instance variables and instance methods from all of its
superclasses. Tree -> BinaryTree -> BST
◦ You can specify only ONE superclass for any class.
 When a subclass-superclass chain contains multiple instance
methods with the same signature (name, arity, and argument
types), the one closest to the target instance in the subclass-
superclass chain is the one executed.
◦ All others are shadowed/overridden.
 Something like multiple inheritance can be done via interfaces
(more on this later)
 What’s the superclass of a class defined without an extends
clause?
17
UMBC CMSC 331 Java
Extending a classExtending a class
public class PlaneCircle extends Circle {
// We automatically inherit the fields and methods of Circle,
// so we only have to put the new stuff here.
// New instance fields that store the center point of the circle
public double cx, cy;
// A new constructor method to initialize the new fields
// It uses a special syntax to invoke the Circle() constructor
public PlaneCircle(double r, double x, double y) {
super(r); // Invoke the constructor of the superclass, Circle()
this.cx = x; // Initialize the instance field cx
this.cy = y; // Initialize the instance field cy
}
// The area() and circumference() methods are inherited from Circle
// A new instance method that checks whether a point is inside the circle
// Note that it uses the inherited instance field r
public boolean isInside(double x, double y) {
double dx = x - cx, dy = y - cy; // Distance from center
double distance = Math.sqrt(dx*dx + dy*dy); // Pythagorean theorem
return (distance < r); // Returns true or false 18
UMBC CMSC 331 Java
Overloading, overwriting, and shadowingOverloading, overwriting, and shadowing
 Overloading occurs when Java can distinguish two procedures
with the same name by examining the number or types of their
parameters.
 Shadowing or overriding occurs when two procedures with the
same signature (name, the same number of parameters, and the
same parameter types) are defined in different classes, one of
which is a superclass of the other.
19
UMBC CMSC 331 Java
On designing class hierarchiesOn designing class hierarchies
 Programs should obey the explicit-representation principle, with classes
included to reflect natural categories.
 Programs should obey the no-duplication principle, with instance methods
situated among class definitions to facilitate sharing.
 Programs should obey the look-it-up principle, with class definitions
including instance variables for stable, frequently requested information.
 Programs should obey the need-to-know principle, with public interfaces
designed to restrict instance-variable and instance-method access, thus
facilitating the improvement and maintenance of nonpublic program
elements.
 If you find yourself using the phrase an X is aY when describing the
relation between two classes, then the X class is a subclass of theY class.
 If you find yourself using X has aY when describing the relation between
two classes, then instances of theY class appear as parts of instances of
the X class.
20
UMBC CMSC 331 Java
Data hiding and encapsulationData hiding and encapsulation
Data-hiding or encapsulation is an
important part of the OO paradigm.
Classes should carefully control access to
their data and methods in order to
◦ Hide the irrelevant implementation-level details
so they can be easily changed
◦ Protect the class against accidental or malicious
damage.
◦ Keep the externally visible class simple and easy
to document
Java has a simple access control mechanism
to help with encapsulation 21
UMBC CMSC 331 Java
ExampleExample
encapsulationencapsulation
package shapes; // Specify a package for the class
public class Circle { // The class is still public
public static final double PI = 3.14159;
protected double r; // Radius is hidden, but visible to subclasses
// A method to enforce the restriction on the radius
// This is an implementation detail that may be of interest to subclasses
protected checkRadius(double radius) {
if (radius < 0.0)
throw new IllegalArgumentException("radius may not be negative.");
}
// The constructor method
public Circle(double r) {checkRadius(r); this.r = r; }
// Public data accessor methods
public double getRadius() { return r; };
public void setRadius(double r) { checkRadius(r); this.r = r;}
// Methods to operate on the instance field
public double area() { return PI * r * r; }
public double circumference() { return 2 * PI * r; }
}
22
UMBC CMSC 331 Java
Access controlAccess control
Access to packages
◦ Java offers no control mechanisms for packages.
◦ If you can find and read the package you can
access it
Access to classes
◦ All top level classes in package P are accessible
anywhere in P
◦ All public top-level classes in P are accessible
anywhere
Access to class members (in class C in
package P)
◦ Public: accessible anywhere C is accessible 23
24
UMBC CMSC 331 Java
Getters and settersGetters and setters
 A getter is a method that extracts information from an instance.
◦ One benefit: you can include additional computation in a getter.
 A setter is a method that inserts information into an instance (also
known as mutators).
◦ A setter method can check the validity of the new value (e.g., between 1
and 7) or trigger a side effect (e.g., update a display)
 Getters and setters can be used even without underlying matching
variables
 Considered good OO practice
 Essential to javabeans
 Convention: for variable fooBar of type fbtype, define
◦ getFooBar()
◦ setFooBar(fbtype x)
25
UMBC CMSC 331 Java
ExampleExample
getters and settersgetters and setters
package shapes; // Specify a package for the class
public class Circle { // The class is still public
// This is a generally useful constant, so we keep it public
public static final double PI = 3.14159;
protected double r; // Radius is hidden, but visible to subclasses
// A method to enforce the restriction on the radius
// This is an implementation detail that may be of interest to subclasses
protected checkRadius(double radius) {
if (radius < 0.0)
throw new IllegalArgumentException("radius may not be negative.");
}
// The constructor method
public Circle(double r) { checkRadius(r); this.r = r;}
// Public data accessor methods
public double getRadius() { return r; };
public void setRadius(double r) { checkRadius(r); this.r = r;}
// Methods to operate on the instance field
public double area() { return PI * r * r; }
public double circumference() { return 2 * PI * r; }
26
UMBC CMSC 331 Java
Abstract classes and methodsAbstract classes and methods
Abstract vs. concrete classes
Abstract classes can not be instantiated
public abstract class shape { }
An abstract method is a method w/o a
body
public abstract double area();
(Only) Abstract classes can have abstract
methods
In fact, any class with an abstract method
is automatically an abstract class 27
UMBC CMSC 331 Java
ExampleExample
abstract classabstract class
public abstract class Shape {
public abstract double area(); // Abstract methods: note
public abstract double circumference();// semicolon instead of body.
}
class Circle extends Shape {
public static final double PI = 3.14159265358979323846;
protected double r; // Instance data
public Circle(double r) { this.r = r; } // Constructor
public double getRadius() { return r; } // Accessor
public double area() { return PI*r*r; } // Implementations of
public double circumference() { return 2*PI*r; } // abstract methods.
}
class Rectangle extends Shape {
protected double w, h; // Instance data
public Rectangle(double w, double h) { // Constructor
this.w = w; this.h = h;
}
public double getWidth() { return w; } // Accessor method
public double getHeight() { return h; } // Another accessor
public double area() { return w*h; } // Implementations of
public double circumference() { return 2*(w + h); } // abstract methods. 28
UMBC CMSC 331 Java
Syntax NotesSyntax Notes
No global variables
◦ class variables and methods may be applied to
any instance of an object
◦ methods may have local (private?) variables
No pointers
◦ but complex data objects are “referenced”
Other parts of Java are borrowed from
PL/I, Modula, and other languages
29
1 von 29

Recomendados

Java von
JavaJava
JavaKhasim Cise
456 views29 Folien
Java Basics von
Java BasicsJava Basics
Java BasicsRajkattamuri
637 views29 Folien
Java Basics von
Java BasicsJava Basics
Java BasicsF K
309 views29 Folien
Java basic tutorial von
Java basic tutorialJava basic tutorial
Java basic tutorialBui Kiet
573 views29 Folien
Java02 von
Java02Java02
Java02Vinod siragaon
1.8K views29 Folien
Java basic von
Java basicJava basic
Java basicSonam Sharma
26K views29 Folien

Más contenido relacionado

Was ist angesagt?

Java Concepts von
Java ConceptsJava Concepts
Java ConceptsAbdulImrankhan7
347 views15 Folien
Classes, objects in JAVA von
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
28.4K views27 Folien
JAVA Notes - All major concepts covered with examples von
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
2.7K views10 Folien
Core java concepts von
Core java conceptsCore java concepts
Core java conceptslaratechnologies
300 views13 Folien
Overloading and overriding in vb.net von
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.netsuraj pandey
7.1K views13 Folien
Java ppt Gandhi Ravi (gandhiri@gmail.com) von
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
568 views58 Folien

Was ist angesagt?(20)

Classes, objects in JAVA von Abhilash Nair
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair28.4K views
Overloading and overriding in vb.net von suraj pandey
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
suraj pandey7.1K views
Java ppt Gandhi Ravi (gandhiri@gmail.com) von Gandhi Ravi
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Gandhi Ravi568 views
Introduction to class in java von kamal kotecha
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha16.3K views
Method overloading, recursion, passing and returning objects from method, new... von JAINAM KAPADIYA
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA1.1K views
C++ classes tutorials von FALLEE31188
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
FALLEE311887K views
Class object method constructors in java von Raja Sekhar
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
Raja Sekhar10.5K views
Java OOP Programming language (Part 3) - Class and Object von OUM SAOKOSAL
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL482 views
Pi j3.2 polymorphism von mcollison
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison208 views
Chapter 02: Classes Objects and Methods Java by Tushar B Kute von 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
Tushar B Kute8.4K views
Built in classes in java von Mahmoud Ali
Built in classes in javaBuilt in classes in java
Built in classes in java
Mahmoud Ali17.6K views

Destacado

10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta von
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta richunenbdvsrp
133 views2 Folien
Doa dhuha von
Doa  dhuhaDoa  dhuha
Doa dhuhaAbdul Razak Isnin
689 views2 Folien
Идея "Аниме магазин" von
Идея "Аниме магазин"Идея "Аниме магазин"
Идея "Аниме магазин"Daniar777
193 views7 Folien
0944388579 von
09443885790944388579
0944388579Trịnh Hữu Việt
81 views3 Folien
Туристический портал – «Афиша Подмосковья» von
Туристический портал – «Афиша Подмосковья» Туристический портал – «Афиша Подмосковья»
Туристический портал – «Афиша Подмосковья» culture-brand
152 views16 Folien
Calentamiento general y específico del voleibol von
Calentamiento general y específico del voleibolCalentamiento general y específico del voleibol
Calentamiento general y específico del voleibolYlenia López García
6.6K views12 Folien

Destacado(6)

10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta von richunenbdvsrp
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta
10 Cosas Que Hay Que Hacer Ya antes De Quedarte Encinta
richunenbdvsrp133 views
Идея "Аниме магазин" von Daniar777
Идея "Аниме магазин"Идея "Аниме магазин"
Идея "Аниме магазин"
Daniar777193 views
Туристический портал – «Афиша Подмосковья» von culture-brand
Туристический портал – «Афиша Подмосковья» Туристический портал – «Афиша Подмосковья»
Туристический портал – «Афиша Подмосковья»
culture-brand152 views

Similar a Java

Core java concepts von
Core java  conceptsCore java  concepts
Core java conceptsRam132
35.8K views15 Folien
Java assignment help von
Java assignment helpJava assignment help
Java assignment helpJacob William
213 views17 Folien
Best Core Java Training In Bangalore von
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalorerajkamaltibacademy
31 views15 Folien
Corejava Training in Bangalore Tutorial von
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
108 views24 Folien
Ppt of c++ vs c# von
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#shubhra chauhan
13.3K views50 Folien
Core Java Concepts von
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
349 views15 Folien

Similar a Java(20)

Core java concepts von Ram132
Core java  conceptsCore java  concepts
Core java concepts
Ram13235.8K views
Core Java Concepts von mdfkhan625
Core Java ConceptsCore Java Concepts
Core Java Concepts
mdfkhan625349 views
Unit 1 Part - 3 constructor Overloading Static.ppt von DeepVala5
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala56 views
Java tutorial for Beginners and Entry Level von Ramrao Desai
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai3.4K views
02-OOP with Java.ppt von EmanAsem4
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem44 views
java: basics, user input, data type, constructor von Shivam Singhal
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
Shivam Singhal4.8K views

Más de javeed_mhd

For each component in mule von
For each component in muleFor each component in mule
For each component in mulejaveed_mhd
425 views8 Folien
Filter expression in mule von
Filter expression in muleFilter expression in mule
Filter expression in mulejaveed_mhd
311 views7 Folien
File component in mule von
File component in muleFile component in mule
File component in mulejaveed_mhd
287 views8 Folien
Database component in mule von
Database component in muleDatabase component in mule
Database component in mulejaveed_mhd
380 views8 Folien
Choice component in mule von
Choice component in muleChoice component in mule
Choice component in mulejaveed_mhd
322 views8 Folien
Vm component in mule von
Vm component in muleVm component in mule
Vm component in mulejaveed_mhd
489 views9 Folien

Más de javeed_mhd(20)

For each component in mule von javeed_mhd
For each component in muleFor each component in mule
For each component in mule
javeed_mhd425 views
Filter expression in mule von javeed_mhd
Filter expression in muleFilter expression in mule
Filter expression in mule
javeed_mhd311 views
File component in mule von javeed_mhd
File component in muleFile component in mule
File component in mule
javeed_mhd287 views
Database component in mule von javeed_mhd
Database component in muleDatabase component in mule
Database component in mule
javeed_mhd380 views
Choice component in mule von javeed_mhd
Choice component in muleChoice component in mule
Choice component in mule
javeed_mhd322 views
Vm component in mule von javeed_mhd
Vm component in muleVm component in mule
Vm component in mule
javeed_mhd489 views
Until successful component in mule von javeed_mhd
Until successful component in muleUntil successful component in mule
Until successful component in mule
javeed_mhd592 views
Quartz component in mule von javeed_mhd
Quartz component in muleQuartz component in mule
Quartz component in mule
javeed_mhd367 views
Mule management console installation von javeed_mhd
Mule management console installation Mule management console installation
Mule management console installation
javeed_mhd316 views
Mule esb made system integration easy von javeed_mhd
Mule esb made system integration easy Mule esb made system integration easy
Mule esb made system integration easy
javeed_mhd365 views
Message properties component in mule von javeed_mhd
Message properties component in muleMessage properties component in mule
Message properties component in mule
javeed_mhd340 views
Junit in mule demo von javeed_mhd
Junit in mule demo Junit in mule demo
Junit in mule demo
javeed_mhd401 views
How to install sonarqube plugin in anypoint von javeed_mhd
How to install sonarqube plugin in anypoint How to install sonarqube plugin in anypoint
How to install sonarqube plugin in anypoint
javeed_mhd329 views
How to commit a project in svn using svn plugin in anypointstudio von javeed_mhd
How to commit a project in svn using svn plugin in anypointstudioHow to commit a project in svn using svn plugin in anypointstudio
How to commit a project in svn using svn plugin in anypointstudio
javeed_mhd384 views
Mapping and listing with mule von javeed_mhd
Mapping and listing with mule Mapping and listing with mule
Mapping and listing with mule
javeed_mhd311 views
Mule any point exchange von javeed_mhd
Mule any point exchangeMule any point exchange
Mule any point exchange
javeed_mhd361 views
Mule esb api layer von javeed_mhd
Mule esb api layer Mule esb api layer
Mule esb api layer
javeed_mhd364 views
Mule Maven Plugin von javeed_mhd
Mule Maven PluginMule Maven Plugin
Mule Maven Plugin
javeed_mhd341 views
Mule esb stripe von javeed_mhd
Mule esb stripeMule esb stripe
Mule esb stripe
javeed_mhd469 views
Mule with stored procedure von javeed_mhd
Mule with stored procedureMule with stored procedure
Mule with stored procedure
javeed_mhd386 views

Último

Throughput von
ThroughputThroughput
ThroughputMoisés Armani Ramírez
32 views11 Folien
CXL at OCP von
CXL at OCPCXL at OCP
CXL at OCPCXL Forum
208 views66 Folien
Tunable Laser (1).pptx von
Tunable Laser (1).pptxTunable Laser (1).pptx
Tunable Laser (1).pptxHajira Mahmood
21 views37 Folien
MemVerge: Past Present and Future of CXL von
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXLCXL Forum
110 views26 Folien
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi von
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
"AI Startup Growth from Idea to 1M ARR", Oleksandr UspenskyiFwdays
26 views9 Folien
Data-centric AI and the convergence of data and model engineering: opportunit... von
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...Paolo Missier
29 views40 Folien

Último(20)

CXL at OCP von CXL Forum
CXL at OCPCXL at OCP
CXL at OCP
CXL Forum208 views
MemVerge: Past Present and Future of CXL von CXL Forum
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXL
CXL Forum110 views
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi von Fwdays
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
Fwdays26 views
Data-centric AI and the convergence of data and model engineering: opportunit... von Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier29 views
Combining Orchestration and Choreography for a Clean Architecture von ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs168 views
"Fast Start to Building on AWS", Igor Ivaniuk von Fwdays
"Fast Start to Building on AWS", Igor Ivaniuk"Fast Start to Building on AWS", Igor Ivaniuk
"Fast Start to Building on AWS", Igor Ivaniuk
Fwdays36 views
"How we switched to Kanban and how it integrates with product planning", Vady... von Fwdays
"How we switched to Kanban and how it integrates with product planning", Vady..."How we switched to Kanban and how it integrates with product planning", Vady...
"How we switched to Kanban and how it integrates with product planning", Vady...
Fwdays61 views
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ... von Fwdays
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ..."Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
Fwdays33 views
Microchip: CXL Use Cases and Enabling Ecosystem von CXL Forum
Microchip: CXL Use Cases and Enabling EcosystemMicrochip: CXL Use Cases and Enabling Ecosystem
Microchip: CXL Use Cases and Enabling Ecosystem
CXL Forum129 views
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV von Splunk
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
Splunk86 views
MemVerge: Gismo (Global IO-free Shared Memory Objects) von CXL Forum
MemVerge: Gismo (Global IO-free Shared Memory Objects)MemVerge: Gismo (Global IO-free Shared Memory Objects)
MemVerge: Gismo (Global IO-free Shared Memory Objects)
CXL Forum112 views
GigaIO: The March of Composability Onward to Memory with CXL von CXL Forum
GigaIO: The March of Composability Onward to Memory with CXLGigaIO: The March of Composability Onward to Memory with CXL
GigaIO: The March of Composability Onward to Memory with CXL
CXL Forum126 views
TE Connectivity: Card Edge Interconnects von CXL Forum
TE Connectivity: Card Edge InterconnectsTE Connectivity: Card Edge Interconnects
TE Connectivity: Card Edge Interconnects
CXL Forum96 views
Empathic Computing: Delivering the Potential of the Metaverse von Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst449 views
Understanding GenAI/LLM and What is Google Offering - Felix Goh von NUS-ISS
Understanding GenAI/LLM and What is Google Offering - Felix GohUnderstanding GenAI/LLM and What is Google Offering - Felix Goh
Understanding GenAI/LLM and What is Google Offering - Felix Goh
NUS-ISS39 views
Future of Learning - Khoong Chan Meng von NUS-ISS
Future of Learning - Khoong Chan MengFuture of Learning - Khoong Chan Meng
Future of Learning - Khoong Chan Meng
NUS-ISS31 views

Java

  • 1. UMBC CMSC 331 Java JAVA BASICSJAVA BASICS Presented ByPresented By
  • 2. UMBC CMSC 331 Java Comments are almost like C++Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc” style comments in your code. 2 /* This kind of comment can span multiple lines */ // This kind is to the end of the line /** * This kind of comment is a special * ‘javadoc’ style comment */
  • 3. UMBC CMSC 331 Java An example of a classAn example of a class 3 class Person { String name; int age; void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } } Variable Method
  • 4. UMBC CMSC 331 Java ScopingScoping  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. 4 { 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.
  • 5. UMBC CMSC 331 Java An array is an objectAn 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! 5
  • 6. UMBC CMSC 331 Java Scope of ObjectsScope 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"); 6
  • 7. UMBC CMSC 331 Java Methods, arguments and return valuesMethods, 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() {} 7
  • 8. UMBC CMSC 331 Java The static keywordThe 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 8 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
  • 9. UMBC CMSC 331 Java Array OperationsArray 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 9
  • 10. UMBC CMSC 331 Java ExampleExample ProgramsPrograms
  • 11. 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 C:UMBC331java>
  • 12. UMBC CMSC 331 Java Factorial ExampleFactorial 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 1 fact = fact * x; // multiply by x each time x = x - 1; // and then decrement x } // Jump back to the star of loop return fact; // Return the result 12 From Java in a Nutshell
  • 13. UMBC CMSC 331 Java JAVA ClassesJAVA 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 13
  • 14. UMBC CMSC 331 Java ExampleExample 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; } } 14
  • 15. UMBC CMSC 331 Java ConstructorsConstructors 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!) 15
  • 16. UMBC CMSC 331 Java Constructor exampleConstructor example public class Circle { public static final double PI = 3.14159; // A constant public double r; // instance field holds circle’s radius // The constructor method: initialize the radius field public Circle(double r) { this.r = r; } // Constructor to use if no arguments public Circle() { r = 1.0; } // better: public Circle() { this(1.0); } // The instance methods: compute values based on radius public double circumference() { return 2 * PI * r; } public double area() { return PI * r*r; } } 16 this.r refers to the r field of the class This() refers to a constructor for the class
  • 17. UMBC CMSC 331 Java Extending a classExtending a class  Class hierarchies reflect subclass-superclass relations among classes.  One arranges classes in hierarchies: ◦ A class inherits instance variables and instance methods from all of its superclasses. Tree -> BinaryTree -> BST ◦ You can specify only ONE superclass for any class.  When a subclass-superclass chain contains multiple instance methods with the same signature (name, arity, and argument types), the one closest to the target instance in the subclass- superclass chain is the one executed. ◦ All others are shadowed/overridden.  Something like multiple inheritance can be done via interfaces (more on this later)  What’s the superclass of a class defined without an extends clause? 17
  • 18. UMBC CMSC 331 Java Extending a classExtending a class public class PlaneCircle extends Circle { // We automatically inherit the fields and methods of Circle, // so we only have to put the new stuff here. // New instance fields that store the center point of the circle public double cx, cy; // A new constructor method to initialize the new fields // It uses a special syntax to invoke the Circle() constructor public PlaneCircle(double r, double x, double y) { super(r); // Invoke the constructor of the superclass, Circle() this.cx = x; // Initialize the instance field cx this.cy = y; // Initialize the instance field cy } // The area() and circumference() methods are inherited from Circle // A new instance method that checks whether a point is inside the circle // Note that it uses the inherited instance field r public boolean isInside(double x, double y) { double dx = x - cx, dy = y - cy; // Distance from center double distance = Math.sqrt(dx*dx + dy*dy); // Pythagorean theorem return (distance < r); // Returns true or false 18
  • 19. UMBC CMSC 331 Java Overloading, overwriting, and shadowingOverloading, overwriting, and shadowing  Overloading occurs when Java can distinguish two procedures with the same name by examining the number or types of their parameters.  Shadowing or overriding occurs when two procedures with the same signature (name, the same number of parameters, and the same parameter types) are defined in different classes, one of which is a superclass of the other. 19
  • 20. UMBC CMSC 331 Java On designing class hierarchiesOn designing class hierarchies  Programs should obey the explicit-representation principle, with classes included to reflect natural categories.  Programs should obey the no-duplication principle, with instance methods situated among class definitions to facilitate sharing.  Programs should obey the look-it-up principle, with class definitions including instance variables for stable, frequently requested information.  Programs should obey the need-to-know principle, with public interfaces designed to restrict instance-variable and instance-method access, thus facilitating the improvement and maintenance of nonpublic program elements.  If you find yourself using the phrase an X is aY when describing the relation between two classes, then the X class is a subclass of theY class.  If you find yourself using X has aY when describing the relation between two classes, then instances of theY class appear as parts of instances of the X class. 20
  • 21. UMBC CMSC 331 Java Data hiding and encapsulationData hiding and encapsulation Data-hiding or encapsulation is an important part of the OO paradigm. Classes should carefully control access to their data and methods in order to ◦ Hide the irrelevant implementation-level details so they can be easily changed ◦ Protect the class against accidental or malicious damage. ◦ Keep the externally visible class simple and easy to document Java has a simple access control mechanism to help with encapsulation 21
  • 22. UMBC CMSC 331 Java ExampleExample encapsulationencapsulation package shapes; // Specify a package for the class public class Circle { // The class is still public public static final double PI = 3.14159; protected double r; // Radius is hidden, but visible to subclasses // A method to enforce the restriction on the radius // This is an implementation detail that may be of interest to subclasses protected checkRadius(double radius) { if (radius < 0.0) throw new IllegalArgumentException("radius may not be negative."); } // The constructor method public Circle(double r) {checkRadius(r); this.r = r; } // Public data accessor methods public double getRadius() { return r; }; public void setRadius(double r) { checkRadius(r); this.r = r;} // Methods to operate on the instance field public double area() { return PI * r * r; } public double circumference() { return 2 * PI * r; } } 22
  • 23. UMBC CMSC 331 Java Access controlAccess control Access to packages ◦ Java offers no control mechanisms for packages. ◦ If you can find and read the package you can access it Access to classes ◦ All top level classes in package P are accessible anywhere in P ◦ All public top-level classes in P are accessible anywhere Access to class members (in class C in package P) ◦ Public: accessible anywhere C is accessible 23
  • 24. 24
  • 25. UMBC CMSC 331 Java Getters and settersGetters and setters  A getter is a method that extracts information from an instance. ◦ One benefit: you can include additional computation in a getter.  A setter is a method that inserts information into an instance (also known as mutators). ◦ A setter method can check the validity of the new value (e.g., between 1 and 7) or trigger a side effect (e.g., update a display)  Getters and setters can be used even without underlying matching variables  Considered good OO practice  Essential to javabeans  Convention: for variable fooBar of type fbtype, define ◦ getFooBar() ◦ setFooBar(fbtype x) 25
  • 26. UMBC CMSC 331 Java ExampleExample getters and settersgetters and setters package shapes; // Specify a package for the class public class Circle { // The class is still public // This is a generally useful constant, so we keep it public public static final double PI = 3.14159; protected double r; // Radius is hidden, but visible to subclasses // A method to enforce the restriction on the radius // This is an implementation detail that may be of interest to subclasses protected checkRadius(double radius) { if (radius < 0.0) throw new IllegalArgumentException("radius may not be negative."); } // The constructor method public Circle(double r) { checkRadius(r); this.r = r;} // Public data accessor methods public double getRadius() { return r; }; public void setRadius(double r) { checkRadius(r); this.r = r;} // Methods to operate on the instance field public double area() { return PI * r * r; } public double circumference() { return 2 * PI * r; } 26
  • 27. UMBC CMSC 331 Java Abstract classes and methodsAbstract classes and methods Abstract vs. concrete classes Abstract classes can not be instantiated public abstract class shape { } An abstract method is a method w/o a body public abstract double area(); (Only) Abstract classes can have abstract methods In fact, any class with an abstract method is automatically an abstract class 27
  • 28. UMBC CMSC 331 Java ExampleExample abstract classabstract class public abstract class Shape { public abstract double area(); // Abstract methods: note public abstract double circumference();// semicolon instead of body. } class Circle extends Shape { public static final double PI = 3.14159265358979323846; protected double r; // Instance data public Circle(double r) { this.r = r; } // Constructor public double getRadius() { return r; } // Accessor public double area() { return PI*r*r; } // Implementations of public double circumference() { return 2*PI*r; } // abstract methods. } class Rectangle extends Shape { protected double w, h; // Instance data public Rectangle(double w, double h) { // Constructor this.w = w; this.h = h; } public double getWidth() { return w; } // Accessor method public double getHeight() { return h; } // Another accessor public double area() { return w*h; } // Implementations of public double circumference() { return 2*(w + h); } // abstract methods. 28
  • 29. UMBC CMSC 331 Java Syntax NotesSyntax Notes No global variables ◦ class variables and methods may be applied to any instance of an object ◦ methods may have local (private?) variables No pointers ◦ but complex data objects are “referenced” Other parts of Java are borrowed from PL/I, Modula, and other languages 29