Академический Документы
Профессиональный Документы
Культура Документы
Concepts in Java
Basic Object-Oriented concepts
(In Java everything is an object or a class )
Concept: An object has behaviors
In old style programming, you had:
data, which was completely passive
functions, which could manipulate any data
An object contains both data and methods that
manipulate that data
An object is active, not passive; it does things
An object is responsible for its own data
Example: A Rabbit object
You could (in a game, for example) create an
object representing a rabbit
It would have data:
How hungry it is
How frightened it is
Where it is
And methods:
eat, hide, run, dig
Concept: Classes describe objects
Every object belongs to (is an instance of) a class
An object may have fields, or variables
The class describes those fields
An object may have methods
The class describes those methods
A class is like a template, or cookie cutter
Concept: Classes are like
Abstract Data Types
An Abstract Data Type (ADT) bundles together:
some data, representing an object or "thing"
the operations on that data
Example: a CheckingAccount, with operations
deposit, withdraw, getBalance, etc.
Classes enforce this bundling together
Example of a class
class Employee {
// fields
String name;
double salary;
// a method
void pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
}
}
Approximate Terminology
instance = object
field = instance variable
method = function
sending a message to an object =
calling a function
These are all approximately true
Example of inheritance
class Person {
String name;
int age;
static int population;
Person (String name) {
this.name = name;
this.age = 0;
population++;
}
}
Kinds of access
publicclassWelcome1{
//mainmethodbeginsexecutionofJavaapplication
publicstaticvoidmain(Stringargs[])
{
System.out.println("Hello"+args[0]+""+
args[1]);
System.out.println("WelcometoJavaProgramming!");
}//endmethodmain
}//endclassWelcome1
Essentials of Java Programs
Every program must have at least one class, and this
class name must be the same as the file name
Case sensitive
If the compiler can resolve the binding at the compile time only then
such a binding is called Static Binding or Early Binding.
All the instance method calls are always resolved at runtime,
but all the static method calls are resolved at compile time itself and
hence we have static binding for static method calls.
Because static methods are class methods and hence they can be
accessed using the class name itself .
How to enter the value in java
import java.io.*;
class d{
public static void main(String arg[]) throws Exception
{
String f;
int i;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
}
}
Run Applet
import java.awt.*;
import java.applet.*;
public class myap extends Applet
{
String str=" ";
public void init()
{
str="init";
}
public void paint(Graphics g)
{
g.drawString(str,50,80);
}
/*<applet code="myap" height=200 width=300> </applet>*/
}
C:\Program Files\Java\jdk1.6.0\bin>javac d.java
C:\Program Files\Java\jdk1.6.0\bin>java d
enter the value
dd
enter the interger value
34
value isdd
integer vlaue is :34
class testcommand{
public static void main(String arg[])
{
int count,i=0;
String st;
count=arg.length;
System.out.println("no of argument"+count);
while(i<count)
{
st=arg[i];
i=i+1;
System.out.println(st);
}
}
}
Command line argument
C:\Program Files\Java\jdk1.6.0\bin>java
testcommand 1 3 5 7 87
no of argument5
1
3
5
7
87
Abstract Data Types
36
Java.util.Collection interface
A Java collection is any class that holds objects and
implements the Collection interface