0% found this document useful (0 votes)
45 views

Core Java Concepts

The document discusses several Java concepts including classes, methods, constructors, scopes, arrays, and examples. It defines that a class describes data objects and operations that can be applied to objects. Methods can be declared as static to exist independently of any object. Constructors are special methods that create or construct instances of a class. Scopes determine where variables are accessible. Arrays are objects that can be dynamically allocated and subject to garbage collection. Examples provided include an echo program and a factorial program.

Uploaded by

karthikstsm
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Core Java Concepts

The document discusses several Java concepts including classes, methods, constructors, scopes, arrays, and examples. It defines that a class describes data objects and operations that can be applied to objects. Methods can be declared as static to exist independently of any object. Constructors are special methods that create or construct instances of a class. Scopes determine where variables are accessible. Arrays are objects that can be dynamically allocated and subject to garbage collection. Examples provided include an echo program and a factorial program.

Uploaded by

karthikstsm
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

JAVA

CONCEPTS
CORE

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 {
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

Java methods and variables can be declared static


These exist independent of any object
This means that a Classs
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

The

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

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"};

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 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>java echo this is pretty silly


this
is
pretty
silly

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

//
//
//
//

This method computes x!


Check for bad input
if bad, return 0
Begin with an initial

// Loop until x equals


//
multiply by x each
//
and then decrement x
// Jump back to the star of
// Return the result
// factorial() ends here
// The class ends here

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 dont 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
NSIT ,Jetalpur
process!)

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
intstorage(Strings){returns.length()*2;}
booleanflag(){returntrue;}
floatnaturalLogBase(){return2.718f;}
voidnothing(){return;}
voidnothing2(){}
NSIT ,Jetalpur

You might also like