Translate

Showing posts with label What is java. Show all posts
Showing posts with label What is java. Show all posts

Saturday, 20 December 2014

Method Overloading in Java

While defining a method it is compulsory to specify return type, otherwise it would give a compile time error. In java following syntax is followed to define a method.

access_specifier return_type method_name(argument 1, ..., argument n)
{
    .....//main body
}

method_name(argument 1, ..., argument n) is known as signature of method.
We can have as many methods in our class as we want as long as their signature is different. We can have multiple methods with the same name as long as their argument list are different. This is called Method Overloading. Either no of argument must be different or type of argument must be different or order of argument must be different.

For example:-

class Calculator
{
    public int sum(int a, int b)
    {
        return a+b;
    }
    public int sum(int a, int b, int c)
    {
        return a+b+c;
    }
    public float sum(int a, int b)
    {
        return a+b;
    }
}

class Java
{
    public static void main(String Args[])
    {
         Calculator calc = new Calculator();
         int x,y;
         float f1;
         x = calc.sum(3,4);
         System.Out.Println("3+4=" + x);
         y = calc.sum(3,4,5);
         System.Out.Println("3+4+5=" + y);
         f1 = calc.sum(1.2f, 1.4f);
         System.Out.Println("1.2+1.4=" + f1);
    }
}

Method overloading is an example of compile time Polymorphism.

Friday, 15 August 2014

Conditional Statements (if else switch)

If else statements:-

If, else, switch statements are formed using boolean expression and they are used to alter the sequence.

e.g
if( a>=90 && a<=100 )
   System.out.println("A grade");
else if( a>=80 && a<90)
   System.out.println("B grade");
else
   System.out.println("Fail");

Here if variable a lies in 90-100 (including both than it will print A grade).
Here && stand for logical AND which means both conditions a>=90 AND a<=100 should be true for if condition.

Instead of && we can also use || which is logical OR.
if( a<0 || a>0 ) means either a should be less than zero OR a should be greater than zero.

! stands for logical NOT

== is used for equality check and single = is used as assignment so always use  == (double equal to) for equality check.

else if condition is checked if, if condition above it is false.
else condition is checked if, if or else if condition above it is false.


Switch statements:-

If else is used for a condition check on set of values. Switch case is used for a specified value check.
e.g
switch(a)     //here a can be integer or character type only.
{
     case 1:  ..........
                 break;
     case 2:  ..........
                 break;
     default:  ..........
                 break;
}

Here if a is 1 then compiler goes to statements written in case 1 and starts executing. It runs till it finds break.


default is executed when above all cases are not true.
switch(a)
{
     case 1:  ..........
               
     case 2:  ..........
                 break;
     default:  ..........
                 break;
}
Here for case 1 statements in case 1 and 2 both will be executed as there is no break in case 1.

Tuesday, 12 August 2014

Hello world program in java

class Demo
{
    public static void main(String args[])
    {
        System.out.println("Hello World!!");
    }
}


Use command prompt to run java. Please Download java as shown in
http://javaproglearner.blogspot.in/2014/08/java-lets-start.html

Also set environment variable as shown.
Open My Computer-> Properties-> Advanced System Settings-> Advanced -> Environment Variables
        -> user variables -> New ->
Now give variable name = Path
variable value = c/program files/java/jdk/bin                  (Path of java/jdk/bin in your computer)

Now to execute this program open command prompt in your windows PC and enter

c:> javac Demo.java
c:> java Demo

Sunday, 10 August 2014

Java: Let's Start

Java!!
What is java? Some programming language. Blah blah blah. :P
First we have to understand what is programming language? :O
Programming language is a formal language written by user to instruct a machine.
Java is an Object Oriented Programming language created by sun microsystems in 1995. Java is used to create applet and application programs.
Applets are java program that are embedded within a web page.
Application programs are programs that are written for a specific application and which can run on any machine supporting java.
In java first program is compiled then interpreted. For compilation of java code we need JVM (Java Virtual Machine). Java is platform independent but JVM is platform dependent.  JVM is combination of OS and Java interpreter. Here we are going to use Java on windows platform.

To run java into your system first you have to install JDK. You can find it online.
Please follow this link for installation or you can google it. Please set the environment variables too.

http://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html

Now to compile a java program open your command prompt.
c:>javac Demo.java
javac is the compiler that compiles our Demo.java program and creates a file with the extension .class.
class file contains byte code which are binary instruction for java interpreter.
To run application program after compilation enter
c:>java Demo
This will execute the file.  Always save the java file name with the name of class.





Total Pageviews