☰ Topics

Basics of java programming

Prior to the working in java, we must set the path.

Steps:-

  • Open 'This Pc' and click on 'System Properties'.
  • Click on 'Advanced system settings' and click 'Environment Varibles' under 'Advanced' tab.
  • Click on 'New' and type 'path' under variable name and under varible value, type the actual path where the java is installed. By default, it is installed in C:\Program Files (x86), so, changr your path to 'C:\Program Files (x86)\Java\jdk1.7.0_09\bin'


Sample Program


class first_prog
{
	public static void main(String[] arg)
	{
		System.out.println("Hello");
	}
}
                            

  • Write the following code in notepad and save it as 'first_prog.java'. The name of generated byte code is same as class name. Therefore to avoid the ambiguity, we should save our program with classname.
  • Open command prompt, go to the location where you have saved your program.
  • Type javac <filename.java> i.e. javac first_prog.java to compile the program and press enter. If there is any error in the program, it will be listed there.
  • Now type java <classname> i.e. java first_prog to execute the program and press enter. You will get output as 'Hello'.

OUTPUT:-

E:\java\new_java>javac first_prog.java

E:\java\new_java>java first_prog
Hello
                        

Explanation:-

  • class keyword is used to define a class.
  • public keyword is a access specifier(discussed later in detail) which is used with a function so that it can be called from anywhere.
  • static keyword is used with function so that there is no need to create object i.e. it do not require object for calling.
  • void keyword is used as a return type. It do not return any value.
  • keywords are always written in lower case.
  • main() is a pre-defined method from where the execution of the program starts. An anonymous object is acknowledged by compiler to call main function.
  • String[] is a datatype and also a class. The square bracket indicates that it is an array. It is used for command line argument i.e. we can pass arguments during run time.
  • System is a pre-defined class.
  • We write pre-defined class as first letter capital and followed by word first letter capital. Eg:- PrintStream
  • out is a pre-defined object of class System.
  • We write pre-defined ojects in lower case.
  • print() and println() are the pre-defined methods of class system.
  • We write pre-defined methods as first letter small and followed by word first letter capital. Eg:- getChar()

Datatypes

Datatype refers to the behaviour of data elements i.e. what type of data it is i.e. whether integer, string etc. It also specify the operations performed on it like we can't perform mathematical operations on string datatype and we can't concatinate integer datatypes.

It is of two types:-

  1. Primitive Datatype
  2. Reference Datatype/Derived Datatype/Abstract Datatype


Primitive datatype is of value type and abstract/derived datatype is of reference type i.e. pointer that means it contains the address of value stored in it.


 

                            
Primitive Datatype  Reference Datatype
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean

There is an additional reference datatype called String.

Conversion of a primitive datatype to its corresponding reference type is called boxing whereas conversion of reference datatype to its corresponding primitive type is called unboxing. These reference types of its corresponding primitive datatype are classes. Eg:- int belong to Integer class, float belong to Float class and so on. These belong to java.lang package i.e. java.lang.Integer, java.lang.Float and so on. Packages are the collection of classes.(Discussed later in detail)
These classses are known as Wrapper class and the objects of these classes are needed to be instantiated.
Eg:- Integer a=new Integer(5);

String is a special wrapper class.

If reference datatypes are used without creating objects i.e. as primitive datatypes, then these are called literals.
Integer a=5; //Integer literal
Integer a=new Integer(5); //Integer wrapper class

Decision Making Statements

As the name suggest, these statements are used to control the flow of the program as per the given condition.

if else

Java follows the same syntax of if-else as it was in C language.

Syntax:
  1. Single if statement
                                        if(<condition>)
                                        {
                                            statement(s);
                                        }
    
                             
  2. if-else statement
                                        if(<condition>)
                                        {
                                            statement(s);
                                        }
                                        else
                                        {
                                            statement(s);
                                        }
    
                             
  3. if-else-if statement
                                        if(<condition>)
                                        {
                                            statement(s);
                                        }
                                        else if(<condition>)
                                        {
                                            statement(s);
                                        }
    
                             
  4. nested if statement
                                        if(<condition>)
                                        {
                                            if(<condition>)
                                            {
                                                statement(s);
                                            }
                                            else
                                            {
                                                statement(s);
                                            }
    
                                        }
                                        
    
                             


Example of if statement:-
class greater
{
	public static void main(String[] arg)
	{
		int a=5,b=6;
		if(a>b)
		{
			System.out.println(a+" is greater");
		}
		else if(b>a)
		{
			System.out.println(b+" is greater");
		}
		else
		{
			System.out.println("Both are equal");
		}
	}
}
                            
OUTPUT:-

E:\java\new_java>javac greater.java

E:\java\new_java>java greater
6 is greater
                        

switch case

switch case is used when multiple conditions are needed to be evaluated. A integer or character constant is matched with multiple possible ones. For each condition, we define one case with integer or character constant. The case with which the constant matches is executed. The last statement of every case is break. The moment a program encounter a break statement, it exit from the witch case body. It also follow the same syntax as C language.


Syntax:-
switch(<variable/expression>)
{
        case <constant-1>:
            statement(s);
            break;
        case <constant-2>:
            statement(s);
            break;
        -
        -
        -
        case <constant-n>:
            statement(s);
            break;
        default:
            statement(s);
}
                    

Example of switch case:-
class exswitch
{
	public static void main(String[] arg)
	{
		int day=3;
		switch(day)
		{
			case 1:System.out.println("Sunday");
			break;
			case 2:System.out.println("Monday");
			break;
			case 3:System.out.println("Tuesday");
			break;
			case 4:System.out.println("Wednesday");
			break;
			case 5:System.out.println("Thursday");
			break;
			case 6:System.out.println("Friday");
			break;
			case 7:System.out.println("Saturday");
			break;
			default:System.out.println("Invalid Choice");			
		}
	}
}
    

OUTPUT:-

E:\java\new_java>javac exswitch.java

E:\java\new_java>java exswitch
Tuesday
                    


Looping

There may be cases when we need to execute one or a set of statements multiple times as per the given condition. In such cases, we can use loops to execute the statements as many times as required. Java includes for, while, do-while and foreach....in loops.

Syntax:-
  1. for loop:-
    for(<initialization><condition><step value>)
    {
        //body
    }
                
  2. while loop:-
    while(<condition>)
    {
        //body
    }
                
  3. do-while loop:-
    do
    {
        //body
    }
    while(<condition>);
                
  4. foreach...in loop:-
    This loop is mostly used in array(discussed later in detail)
    foreach(&localvariable>:&"Arrayname">)
    {
        //body
    }
                

Loops can also be nested.

Example of looping:-
class exLoop
{
	public static void main(String[] arg)
	{
		for(int i=1;i<=5;i++)
		{
			for(int j=1;j<=i;j++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
    

OUTPUT:-

E:\java\new_java>javac exLoop.java

E:\java\new_java>java exLoop
*
**
***
****
*****