Thursday, 14 July 2011

Class and array

-->

Array:-
Set of finite, homogeneous and continuous.
int[ ] a=new int[20];
or
int a[]=new int[20];
or
int[]=a;
a=new int[20];

int[][] a=new int[20][10];

at compile time
int[] a={1,2,3};
int[][] a={{1,2,3},{2,3,4},{3,4,5}}

Class:-
Collection of elements with some common attributes(properties) and behaviour(method/functions).
Eg. Fruit,book
Object:-
Any particular from the class with some properties and behaviour.
Eg.apple is object where as fruit is class

Note:-
No memory is allocated for class but allocated for object.

How to define a class:-
Class classname
{
Member variables;
Member functions;

}

Note:-difference between argument and parameter
Eg.
void sum(int a,intb) here a and b are arguments
arguments are in definition of function.

S=sum(a,b);
Here a and b are parameters
Parameters are in calling of the function.

Member variables:-
Are defined inside class and outside any function.
This are also known as heap variables. They are autoinitialised.
Eg.
class Member
{
int a,b; //member variable
public void hello(int a)   //member function
{
System.out.print(a);
}
}

Auto-initialise of member variables:-
int=0
float=0.0
boolean=false
char=’0000\u’ ie null
reference=null

Note:-
Local variables are stored in stack so called stack variable. They are not autoinitialised so we need to initialise them.

Eg.
Class A
{
int s;
public static void main(String[] arg)
{
int b;
int c;
c=s+b;
}
}
This will show error as b is local data and is not initialised.
But c is local but it will not show error as by c=s+b; we are
initialising c.

How to create a object:-
classname objectname=new classname();

eg.
A a1=new A();
In this statement the first part A a1 means that a1 is reference variable of typeA.
Where as new A() actually creates the object.

or we can also write
A a1;                              //a1 is reference of type A
a1=new A();          //object is created at heap

Note:-
Allocation of memory of a1 i.e reference variable depends whether it is member or local variable. If it is member then it will be stored on heap.
Otherwise on stack.
But the object created by new A() is always created on heap.


Note:-
Memory is allocated for local data at the time of calling and dellocate as soon as soon as its calling ends.
How to start :-
1.install java
2.copy the address of bin of jdk
Eg. C:\Java\jdk1.5.0_06\bin
3.Then open command prompt then type
cd C:\Java\jdk1.5.0_06\bin\
4.now the directory will look like this
C:\Java\jdk1.5.0_06\bin>
5.now type notepad demo.java for creating file.
6.javac demo.java for compiling.
7.java demo for running the program.

Naming conventions:-
1.First alphabet of class should be capital.
eg. class Complex
2.Alphabet of first word should be small and then big for functions and variables.
eg. void addOnce()


Sample Program:-
class Sample
{
public static void main(String[] arg)
{
System.out.println(“Welcome to JAVA”);
}
}

Command Line:-
Argument passed to main method. This are used to provide input before run time.
 Using commnd line do following programs:-
Ques10:-Write a program of Fibonacci series without using third variable.


No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013