Friday, December 26, 2014

Object and Class in Java

Object in java:

The objects are the physically existing entities in java which are used for specific purpose.

Every object has certain state and behaviour, and will perform specific action.
An object has three characteristics:
  • State: represents data of an object.
  • Behaviour: represents the functionality of an object such as deposit, withdraw etc.
  • Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

Object is an instance of a class. Class is a template or blueprint from which objects are created.

Class in java

A class is a core component of  java because without a class we cannot write a single statement in java.A class is a collection of objects that have the same properties. Class contains the fallowing members

  • data member
  • method
  • constructor
  • block
syntax
class <class name>{
//blocks
//constructors
//variables
//methods
}

 Example of Object and Class

In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value.
class Employee{ 
int  eno;  // instance variable declaration
String  ename; // instance variable declaration
public static void main(String ar[]){   // main method
Employee e=new Enployee();    // creating object to employee class
System.out.println(e.eno); // displaying employee number  of  employee class
System.out.println(e.ename);  // displaying employee name  of employee class
}
}
Out-put:
0 
null

Java Naming conventions

The naming conventions are a set of rules that the developers have to fallow while writing the code. These rules decide what to name your identifiers such as packages, interfaces, classes, variables, constants and methods. While giving the names to these identifiers we should fallow these conventions given by sun Microsystems.
The fallowing are the areas where we have to fallow the naming conventions:
Class name: A class name should be meaningful and always it should start with uppercase letter. If the class name is the combination of two or more words then every word first letter should be in uppercase and we should see that there should not be any spaces or special characters except underscore in between the class name.
e.g. String, Color, Button, System, Thread etc.
Interface name: The interface names also fallow the same naming conventions as that of class. 
 e.g. Runnable, Remote, ActionListener etc.
Package name: unlike class name or interface name the package name always should start with lowercase letters.
e.g. java, lang, sql, util etc.
method name: A method name should start with lowercase and if the method name is a combination of two or more words then the second word onwards every word  first letter should be in uppercase.
e.g. actionPerformed(), main(), print(), println() etc.
variable name: A variable name should start with lowercase in all the cases.
e.g. firstName, orderNumber etc. 
constants name:  The constants should be declared in uppercase. 
e.g. RED, YELLOW, MAX_PRIORITY etc.

Object oriented programming language

An object is considered as a real world entity. Object is an instance of a class. Any programming language in the world which satisfies the object oriented features is considered as a object oriented programming language i.e every program developed using the concerned programming language must fallow the basic concepts that are stated in Oops. The fallowing are the basic concepts of oops:
  • Class
  • Object
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Class

A class is a logical one which cannot be visible. A class contains variables and methods. A class  is a model for creating objects.

Object

An entity which has a specific structure and behaviour is called as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

Inheritance

  The process of deriving a new class from the existing class is called inheritance. In this context the new class is called derived class and the existing class is called as base class. The derived class will acquire all the properties and behaviours of the base class. Using inheritance we can achieve code reusability. It is also used to achieve runtime polymorphism.

 Polymorphism
 An Object which exhibits multiple behaviours is called as polymorphism. Polymorphism is derived from the Greek word where poly means many morphic means forms. Using method overloading and method overriding we can achieve polymorphism.
Abstraction:
Hiding the internal details of a programme but displaying the functionality is called  abstraction.
Encapsulation:
The process of binding the member variables with the methods of a class is called encapsulation. Java beans is the best example for encapsulation.

Control Statements in Java

We have 3 types of Control Statements in Java. They are,
-- Conditional Control Statements
-- Iterative Control Statements
-- Transfer/Jump Control Statements
Control Statements in java
1) Conditional control statements:
Conditional Statements will work based on the conditions. We have different types of Conditional Statements. They are,
i) if : If the condition is true then the control will execute the block of statements otherwise the control will not enter into if block
Syntax:
if(condition)
{
statement1;
statement2;
----------
}
ii) if-else : If the condition is true then the control will execute the if block otherwise the control will execute else block
Syntax:
if(condition)
{
statement1;
statement2;
}
else{
statement3;
statement4;
}
2) Iterative Control Statements:
Iterative control statements are used to execute the group of statements repeatatively based on a condition.
ex:
i) while
ii) do-while
iii) for
3) Transfer/Jump Control Statements:
whenever these statements are encountered then the control will be transfered from one place to another.
ex:
i) break
ii) continue
iii) return

Tuesday, December 23, 2014

Variables in Java

Variable in java can be defined as a name for a memory location which can hold the data.
Variable declartion:
syntax:
<modifier> <Data Type> <var-Name>;
or
<modifier> <Data Type> <var-Name>= value;
There are 3 types of variables are there in java. They are,
1)Local Variables
2)Instance Variables
3)Static Variables
1) Local Variable:
a variable which is declared inside a method is called as Local variable.
ex:
void m1(){
int a=10; // local varible declaration
int b=20; // local varible declaration
}
Note:
Local variables must and should be initialized before using. Otherwise we will get Compile time error.
2) Instance Variable:
a variable which is declared inside a class but not inside any method, without static keyword is called as Instance variable.
ex:
class MyProgram{
int a=10; // Instance varible declaration
int b; // Instance varible declaration
}
Note:
If we not initialize Instance variables then it will be initialized by default values of that data type.
3) Static Variable:
a variable which is declared inside a class but not inside any method, with static keyword is called as Static variable.
ex:
class MyProgram{
static int a=10; // local varible declaration
static int b; // local varible declaration
}
Note:
If we not initialize Static variables then it will be initialized by default values of that data type.

Control Statements in Java
We have 3 types of Control Statements in Java. They are,
-- Conditional Control Statements
-- Iterative Control Statements
-- Transfer/Jump Control Statements
Control Statements in java
1) Conditional control statements:
Conditional Statements will work based on the conditions. We have different types of Conditional Statements. They are,
i) if : If the condition is true then the control will execute the block of statements otherwise the control will not enter into if block
Syntax:
if(condition)
{
statement1;
statement2;
----------
}
ii) if-else : If the condition is true then the control will execute the if block otherwise the control will execute else block
Syntax:
if(condition)
{
statement1;
statement2;
}
else{
statement3;
statement4;
}
2) Iterative Control Statements:
Iterative control statements are used to execute the group of statements repeatatively based on a condition.
ex:
i) while
ii) do-while
iii) for
3) Transfer/Jump Control Statements:
whenever these statements are encountered then the control will be transfered from one place to another.
ex:
i) break
ii) continue
iii) return

Data Types in Java

DataType is used to represent the data which we store in the memory in the form of variables.
In Java we have 2 major types of datatypes.
-- Primitive Datatypes
-- Non-Primitive Datatypes
Data Types
1) Primitive Data Types
-- Numeric
----------Integer
      -----------byte
      -----------short
      -----------int
      -----------long
----------Decimal
      -----------float
      -----------double
--Non-Numeric
      -----------char
      -----------boolean
2) Non-Primitive Data Types
--Derived Data Type
      -----------String
      -----------Array, etc.
--User-Defined Data Type
      -----------class
      -----------interface
      -----------enum, etc.

Basic java Programm

Let us consider a java program with file name MyFirst.java
Creating Hello world program
public class MyFirst{
public static void main(String args[]){
System.out.println("Hello World..!");
}
}
to compile:
javac MyFirst.java
to execute:
java MyFirst

Output:
Hello World..!