Friday 31 January 2014

Getting Started with Java

Learn Java With Java Funz-Merlin Thomas

Java is a product from SUN Microsystems designed by James Gosling. It is a computer programming language that is concurrent, class-based and object-oriented. It was released in 1991 by the name Oak and re-released in 1995 by name Java .( Oak and Java both means Coffee bean )

 

 WHY JAVA BECOME POPULAR ?

1-  Rich Constructs

2- Rich library

3-  Applet 

(Applet are small program written in Java, executed in browser)

 

FEATURES OF JAVA 

1. Simple

Java

 

2. Portable

 

3. Dynamic memory allocation

 

4. Distributed storage

 

5. Secure and Rowbust

 

6. Multithreading

7. True Oops

 

            1)Data Encapsulation

            2)Data Abstraction

            3)Inheritance

            4)Polymorphism

 

8. Platform Independent :  

It is independent because it follows WORA (write once, run anywhere) , meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. 

.java  ----(compile)--->  .class / bytecode ----(interpret) ---> output

  

Running a Java Program

 

SOFTWARE REQUIREMENT :

>  jdk : Java Developmnt Kit (1.6)

         (jdk does not have its own editor) 

>Java API to view applet

> Editor :

        1. Notepad (For Windows)

        2. Integrated Development Environment (IDE)

             -NetBeans

             -Eclipse

             -JCreator 

 

 

 STEPS TO CREATE AND EXECUTE IN JAVA :


Saving A Java Program In Notepad

1. Open Notepad :

Start | Run | Notepad

 

2. Type the program.

 

3. Save it .

     Things to Remember :

       (1) Class name and File name should be same.

       (2) Extension should be  .java

       (3) It is case-sensitive.

 

4. Compile it -

    Go to Command Prompt.

  Start | Run | cmd

 

SYNTAX :

javac FileName.java

 

5. Interpret it -

 

 SYNTAX :

java FileName

 



 

 EXAMPLE of simple JAVA Program :

                                                        EXAMPLE of simple JAVA Program


class First{

    public static void main(String args[]) {

   System.out.println("Hello World");

       }

  }

Output of EXAMPLE of simple JAVA Program













// Hello World simple program

 => In command prompt is the result

       of above program

 

DESCRIPTION :

 - public  : can be called from anywhere.

 - static : no object is required to invoke main.

 - void : main does not return any value.

 -  String args[] :Command line argument.

 - System.out.println : message in new line.

Before going to further programming first we would clear some basic things.


DATA TYPE : 

      It specifies two things :

  •  Type of Data

  • Size of Data type

     

VARIABLES : It is temporary memory location to store a value. The value stored in a variable can change/vary.

# NOTE: Variable in Java makes use of  camelCase.

                 Eg: calAdd , myVar


 

LITERALS : Literals are used to specify data type of a value not of  a variable.

for eg :

10.5f // where f for float

10 // int

10.5 // by default Java take it as Double

10.5d // where d for Double

10l // where l for long.

 

TYPE CASTING : It is converting from one data type to another data type.

In Java type casting is of 2 types :

      1. Narrowing / Explicit : A big value data type into small value data type.

                Eg:

                       int i =10;

                       long j = 20;

                       i =  (int) j;

     2. Widening / Implicit : A small value data type into big value data type.

                Eg:

                       int i =10;

                       long j = 20;

                       j =  (long) i;


INPUT/OUTPUT  : For quick input from user Scanner class is used and class comes from "java.util" package .

 To  use this class in your program write :-

import java.util.Scanner; 

Note :This class was introduced with jdk 1.5

To associate "Scanner" to keyboard (object)

 Scanner obj = new Scanner(System.in);    //new keyword to allocate memory

 

TO ACCEPT :

  • String from User :

         String str=obj.next();

  • Character from User :

        char c=obj.next().charAt(0);

  • Integer from User :

         int num=obj.nextInt();

  • Float from User :  

    float fs=obj.nextFloat();

     

    Example :

     

    Program to accept and display the name and address.

import java.util.Scanner;

class My{

    public static void main(String args[] ){

    String name,address;
    Scanner obj = new Scanner(System.in);
    System.out.println("Enter your name:");
    name = obj.nextLine();  // to accept a string from user
    System.out.println("Enter your address:");
    address = obj.nextLine();
   

        System.out.println("Name:" +name); //  '+' is a coagulation operator
        System.out.println("Address:" +address); // 'println' will print in new line

    }   
}  

IN NOTEPAD:

 

Program to accept and display the name and address.


OUTPUT :

 


Output of Program to accept and display the name and address.

DECISION STRUCTURES :

> It is also called SELECTION or  CONDITIONAL STRUCTURES.

> They are used to select single out of multiple options.

> Like C and C++ ,Java also provides conditional structures like :-

              1. if-else

             2. switch

 

1)  if-else  :

if (condition){

   // True part

             }

else

{

 // False part

             } 

 

 2) switch

 

  switch (variable) {

case <value 1> :  // Statement for value 1

break;

case <value 2> :  // Statement for value 2

break;

:

:

default : False part

Example : if-else

 Program to accept a character and print whether it is uppercase,lowercase or special character.

Program to accept a character and print whether it is uppercase,lowercase or special character.

 OUTPUT :

Output of Program to accept a character and print whether it is uppercase,lowercase or special character.




 Example : switch

 

Program to accept a character and check whether it is a vowel/not  .



OUTPUT :

15 comments:

  1. Its seems interesting when we start learning a language from its basics ..thanks 4 the blog... :)

    ReplyDelete
  2. I liked the blog Plz keep on updating it ...

    ReplyDelete
  3. Lookzzzz like u had a great troble makin dis page..Anyways good job my dear lil sis

    ReplyDelete
  4. Lookzzzz like u had a great trouble makin dis page..Anyways good job my dear lil sis.........I'm proud of you dear......

    ReplyDelete
  5. When will u start with Netbeans????

    ReplyDelete
  6. First I will complete the basics of Java then switch to Netbeans.

    ReplyDelete
  7. superrr se bhi uperrr... ye hai apni merlin...d true engineer..:)...gud going dearo..keep it up....:)

    ReplyDelete
  8. nice one mri gappu merlin....keep it up nd it looks intresting...luv u:)

    ReplyDelete
  9. It was really a good blog for those who want to learn JAVA.Many of us starts working with NetBeans before clearing the basics....Thanks for the blog...Merlin :)

    ReplyDelete
  10. You explained the concept of Platform independency feature of Java really well !

    ReplyDelete
  11. Inheritance par acha likha hai !!!

    ReplyDelete
  12. I think you should add video tutorials too !!!

    ReplyDelete
  13. Very informative, keep posting such good articles, it really helps to know about things.

    ReplyDelete
  14. Hey keep posting such good and meaningful articles.

    ReplyDelete

Chapter : 1 - First code in Node JS Previously I have written a blog about Getting Started with Node JS and its installation. Now lets s...