Showing posts with label Oops with JAVA. Show all posts
Showing posts with label Oops with JAVA. Show all posts

Tuesday, 23 June 2015

Methods in JAVA

Methods:
       
 Functions in C++ are called as methods in JAVA.Methods  do have some specifications.There are three parts of methods.They are

  • Method declaration
  • Method definition
  • Method calling/Invoking
Method declaration is not possible.Programmer can directly define and call their method.There are some built-in methods available as in other programming languages.

Sample JAVA program using methods:

Class Myclass
{
          public void hello()
          {
                 System.out.println("First program with a method");
          }
}
Class Mainclass
{
          public static void main(String[] args)
          {
                Myclass c=new Myclass();
                c.hello();     
          }
}

In this program,there are two classes.when you save your program you should save with name of class in which the main function is residing.

Static Functions:
            It is possible to create a static functions in JAVA.Static functions are those functios  which can be called without using objects.

Example of a static functions:

Class Myclass
{
          public void hello()
          {
                 System.out.println("First program with a  static method");
          }
          public static void main(String[] args)
          {
                hello();     
          }
}

Important concepts in JAVA

Class:
Class is an entity that represents the state or behaviour of the object.Class may contain data members and member functions.
Syntax:
Class First
{
          public static void main(String[]  args)
          {
                 System.out.println("Fun to learn JAVA");
          }
}
public static void main(String[] args)
public :- The method main() can be accessed by anything including interpreter.
Static :- This keyword tells the compiler that the main() method is used in the class. There is no need of instance to execute the static method.
Void :- it indicates that the main function should not return any value.
String args[] :- the main method is declare with a single parameter “args” which is a type of String array.
COMPILING AND RUNNING THE JAVA PROGRAM
Step1: Type the program in a notepad and save as classname.java and place it in the C:\Program Files\Java\jdk1.7.0_17\bin
Step 2 : open the command prompt change the path to C:\Program Files\Java\jdk1.7.0_17\bin and then to compile type javac classname.java, to run type java classname
Step 3 : output will be displayed.
Instead running the program in command prompt better we can use IDE like netbeans or eclipse.
VARIABLES & SCOPE
There are two ways to describe variables:
à   Variable of primitive type
à   Variable of reference type
We can declare variable inside a method or outside a method.
Variable which is defined inside a method is called local variable. Sometime this is also called as automatic, temporary or stacks variables.
Variable defined outside a method are created when the object is constructed using the new abc() call. Variable can be define outside a method in two ways,
à   Using static keyword is called class variable.
à   Without using static keyword is called as instance variable or member variable.

DEFAULT VALUE OF PRIMITIVE TYPES
VARIABLE
VALUE
Byte
0
Short
0
Int
0L
Long
0.0F
Float
0.0D
Double
‘\u0000’
Boolean
False
All reference types
null

OPERATORS
Important types of operators are there in java programming language.
à   Arithmetic Operator (+, -, *, /)
à   Logical Operator (AND (&), OR (|), NOT (!), XOR (^))
à   Short-circuit logical Operator (&&, ||)

Monday, 3 November 2014

Objects in java

  • Object may be defined as real time entity or run time entity.
  • An object is an instance or variable of type class.
  • Using objects, we can access the functions and variables of the class.
  • Every object can have data and functions.
Creating objects:
              Syntax:  class_name obj_name=new class_name();
              Example: Program p=new Program();
where Program is the name of the class and p is object.

If the function is declared as static,it can be accessed without using an object

Introduction to JAVA

Java is an purely object oriented language initially developed by Sun microsystems in 1991.Java is called purely object oriented language,because the main function resides inside the class, whereas C++ is called as partially object oriented language or middle level language.

Features of Java:

  •     Simple
  •     Secure
  •     Architecture neutral
  •     Platform independent
  •     Object oriented
  •     Robustness
  •     Dynamic

Conventions followed by JAVA:
     In java,name of the class in which the main function resides should be same as the file name and it is case-sensitive.
    There is no need of IDE to compile and run java programs.It is possible to compile and run a java program using Command prompt itself.But one can use certain IDE(Integrated Development Environment) to compile and run the java program.
Structure of java program:
    Packages, if required
    Class declaration
    Main function
Printing on the console screen:
       In java,to display some messages on console screen, System.out.println(); or System.out.print(); methods is used.
Example:
              System.out.println("Welcome");
              System.out.print("HI");
Sample java program:
Class Hello
{
         public static void main(String args[])
         {
                System.out.println("HELLO WORLD!!!!!!");
         }
}


  •                           The main function of JAVA is defined as static.The keyword static enables a programmer to call the function without using objects.
  •                           The java program can be typed in a notepad can be saved as "Filename.java".It can be compiled as javac Filename.java.To run the java program, java Filename command is used.

javac compiler:
                      The javac compiler is used to compile the java program.Once the java program is compiled,the byte code and class file is generated which can be used to run the java program under different platforms.