Program of displaying a certain pattern on screen

/*Program of displaying a certain pattern on screen*/

class NestedWhileDemo
{
    public static void main(String args[])
    {
        int i = 1 , j , n ;
        n = 6 ; // value of n should be between 1 to 10

        while (i<=n)
        {
            j = 1 ;
            while (j<=i)
            {
                System.out.print("*\t");
                j++ ;
            }
            System.out.println(" ") ;
            i++ ;
        }
    }
}
READ MORE - Program of displaying a certain pattern on screen

Program to display a particular pattern of stars using nested for loops.


class NestedForDemo
{
    public static void main(String args[])
    {
        int i , j , num ;
        num = 6 ;         //value of num is between 1 to 10
        for (i=1 ; i<=num ; i++)
        {
            for (j=1 ; j<=i ; j++)
                System.out.print("*\t");
            System.out.println(" ");
        }
    }
}
READ MORE - Program to display a particular pattern of stars using nested for loops.

Program to find the minimum and maximum number out of the given three numbers


class MinMax
{
    public static void main(String args[])
    {
        int a , b , c , min , max ;
        a = 15;
        b = 100;
        c = -5;

        if (a < b)
            min = a ;
        else
            min = b ;

        if (c < min)
            min = c ;
        System.out.println("The minimum number is "+min);

        if (a > b)
            max = a ;
        else
            max = b ;

        if (c > max)
            max = c ;
        System.out.println("The maximum number is "+max);
    }
}
READ MORE - Program to find the minimum and maximum number out of the given three numbers

Program to find Minimum of three numbers, using nested if else statements


class Min
{
    public static void main(String args[])
    {
        int n1, n2, n3;
        n1= 7;
        n2= 3;
        n3 = -3;

          if (n1 < n2)
                    if (n1 < n3)
             System.out.println("Smallest number is : "+n1);
                    else    
                   System.out.println("Smallest number is : "+n3);
        else  // n1 >= n2
                 if (n2 < n3)
                   System.out.println("Smallest number is "+n2);
                    else
               System.out.println("Smallest number is "+n3);
    }
}
READ MORE - Program to find Minimum of three numbers, using nested if else statements

Program to demonstrate Multiple statements within if

class IfDemo2
{
    public static void main(String args[])
    {   
        int marks;
        marks = 35;

        if(marks<40)
        {
            int lessmarks=40-marks;
            System.out.println("\nSorry!!!!!you are failed");
            System.out.println("\nYou should have got "+lessmarks+" more marks");

        }
    }
}
READ MORE - Program to demonstrate Multiple statements within if

Java Compilation and Interpretation Process

Above figure shows compilation & Interpretation process

Following steps explains how it works:-

  •  We create  a program using program editor(eg. Notepad) and save it as filename.java file in specified directory.
  •  Then we compile it using javac command(eg. javac filename.java), A java compiler then convert .java file into .class file which is called as bytecode and this bytecode is platform independent can run on different platforms eg.Windows, Linux,MAC etc.
  • If any bugs found during compilation that should be fixed first then compiled again, If there is not any bugs found  then bytecode is created and handed over to JVM(java virtual machine) for interpretation.
  • JVM interpret class file by running java space class name command(eg. java filename)  as given in figure.
  • JVM has three components to interpret program first: Bytecode verifier which verifies bytecode, second: Class loader which loads required classes from java class library, third one is a JIT(just in time) compiler which compiles class file and convert it into machine readable format and then hand over to operating system.
  • If there any bugs(mostly logical) found during interpretation(by JVM) then one have to edit program to fix bugs and compile it again.If not any more bugs then final output will be displayed on screen.           


Note: It's not necessary to have same class name and file name. Thing is that file name is required during compilation and class name is required during interpretation but it would be great to use same name for class and file(file name is a name by which we store program).Also compilation process actually figure out any syntactical error while interpretation checks logical error.

READ MORE - Java Compilation and Interpretation Process

This is a simple Java Program

/*  This is a simple Java Program.
   Call this file “Hello.java”.  */

class Hello
{
        // Your program begins with a call to main( ).
         public static void main(String args[])
        {
               System.out.println(" Hello World ");
        }
}
READ MORE - This is a simple Java Program

What is java?(precisely)


    What is java?
    • Developed by Sun Microsystems ( by James Gosling)
    • A general-purpose object-oriented language(while C++ is procedural plus object oriented)
    • Based on C/C++
    • Designed for easy Web/Internet applications


    Java Features:

      Note: features are always asked by interviewer


    1. Java is Simple:
    • Fixes some clumsy features of C++
    • No pointers
    • Automatic garbage collection
    • Rich pre-defined class library
         2.Java is Object oriented:
    • Focus on the data (objects) and methods manipulating the data
    • All functions are associated with objects
    • Almost all data-types are objects (files, strings, etc.)
    • Potentially better code organization and reuse
         3.Interpreted:
    • Java compiler generate byte-codes(and  not native machine code which is happens in C,C++)
    • The compiled byte-codes are platform-independent
    • Java byte-codes are translated on the fly to machine readable instructions at run-time (by Java Virtual Machine)
         4.Portable:
    • Same application can run on all platforms
    • The sizes of the primitive data types are always the same
    • The libraries define portable interfaces 
         5.Reliable:
    • Extensive compile-time and run-time error checking
    • No pointers but real arrays. Memory corruptions or unauthorized memory accesses are impossible
    • Automatic garbage collection tracks objects usage over time
         6.Secure:
    • Usage in networked environments requires more security
    • Memory allocation model is a major defense
    • Access restrictions are forced (private, public)
         7. Multithreaded:
    • multiple concurrent threads of executions can run simultaneously
    • utilizes a sophisticated set of synchronization primitives (based on monitors and condition variables paradigm) to achieve this
          8.Dynamic:
    • Java is designed to adapt to evolving environment
    • Libraries can freely add new methods and instance variables without any effect on their clients
    • Interfaces promote flexibility and reusability in code by specifying a set of methods an object can perform, but leaves open how these methods should be implemented
    • Can check the class type at run-time.

    READ MORE - What is java?(precisely)

     
     
     
     


    Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|