Core Java Concepts
Core Java Concepts
CONCEPTS
CORE
SURABHI MISHRA
(LCE)
NSIT
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 {
String name;
int age;
Variable
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
{ int x = 12;
Java.
/* 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 */
}
}
NSIT ,Jetalpur
Scope of Objects
Java objects dont 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 GCd
{
Strings=newString("astring");
}/*endofscope*/
NSIT ,Jetalpur
static keyword
The
Example
publicclassCircle{
//Aclassfield
publicstaticfinaldoublePI=3.14159;//Auseful
constant
//Aclassmethod:justcomputeavaluebasedonthe
arguments
publicstaticdoubleradiansToDegrees(doublerads){
returnrads*180/PI;
}
//Aninstancefield
publicdoubler;//Theradiusofthe
circle
//Twomethodswhichoperateontheinstancefieldsofan
object
publicdoublearea(){//Computetheareaof
thecircle
returnPI*r*r;
}
publicdoublecircumference(){//Computethe
circumferenceofthecircle
return2*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
Personmary=newPerson();
intmyArray[]=newint[5];
intmyArray[]={1,4,9,16,25};
Stringlanguages[]={"Prolog","Java"};
dynamically
Arrays, like all objects, are subject to garbage
collection when no more references remain
so fewer memory leaks
Java doesnt have pointers!
NSIT ,Jetalpur
Example
Programs
NSIT ,Jetalpur
Echo.java
C:\UMBC\331\java>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:\UMBC\331\java>javac echo.java
C:\UMBC\331\java>
NSIT ,Jetalpur
Factorial
Example
/* This program computes the factorial of a number
*/
public class Factorial {
public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
double result = factorial(input);
System.out.println(result);
}
here
public static double factorial(int x) {
if (x < 0)
return 0.0;
double fact = 1.0;
value
while(x > 1) {
fact = fact * x;
time
x = x - 1;
}
loop
return fact;
}
}
//
//
//
//
//
//
Define a class
The program starts here
Get the user's input
Compute the factorial
Print out the result
The main() method ends
//
//
//
//
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
will be created.
Constructors automatically invoke the zero
argument constructor of their superclass when
they begin (note that this yields a recursive
NSIT ,Jetalpur
process!)