Monday, 1 December 2014

Write a Program to print your name on screen | My First Java Program

By: Unknown On: 01:26
  • Share The Gag

  • Friends lets start with our first Java program. This is going to be most simple program to print your name on screen.

    Steps to follow:

    1. First you need to create a class
    2. Give your class a name example MyName
    3. defining main() function as execution begins from main()
    4. your code to display your name


    This is how your class should look:

    class class_name
    {
       main()

    {  
      your code to display your name;
    }
    }


    Program to print your name on screen:

    class MyName <-------- Class Name
    {
    public static void main(String s[]) <-------- main() function
    {

    System.out.print("Nitin Kumar"); <------ printing command that will print Nitin Kumar

    }
    }


    Output:

    Nitin Kumar


    Friends this program may look strange to you at first. Specially this line public static void main(String s[]) will confuse you most.

    Friends as you know that execution of your program starts at main() function. So first JVM will find main() function in your program. After that your code inside main() will be executed.

    Why we have used "public" before main() function?
    Answer: public is a access specifier. There are 3 types of access specifier in Java i.e. public, private and protected. if we write private or protected before main() the JVM is not able to find main function and your program will not be executed.

    private static void main(String s[]) is wrong as JVM will not be able to find main() function and your program will not be executed.

    protected static void main(String s[]) is also wrong again JVM will not be able to find main() function and your program will not be executed.

    public static void main(String s[]) is right as JVM can find the main() function because main() is now public.

    That is why we have used public instead of private and protected.

    Why we have use "static" before main() function ?
    Answer: "static" is a keyword. To understand its working you have to learn how we call a method or function in our program.

    Generally what happens is that when you create a method or say function you can use that method only by calling that method using object.
    Example:

    void function()  <-------function name
    {
    System.out.println("I am a Function"); <------- this will print I am a Function
    }

    friends I have created a function named as function(). Now I can not use this function unless I call this function using Object. First I have to make an object and than using that object I can call this function.

    So Function is of no use unless you call them using object.
    1. creating a function
    2. creating an object
    3. using that object to call function()

    These 3 steps are generally followed to use or to call a function.

    Look at our program public static void main(String s[])  There is a function i.e. main() function. To use main function we need to call main() function. But as it require an object to call any method Java gave us another method of calling function without creating object. "static" keyword will tell the JVM that main() function is automatically called without using object. So by putting "static" keyword before any function or method that method will be called automatically by JVM with out using any object.


    What is the use of "void" that is used before main() function ?
    Answer: "void" is just  return type. In our first program we are not returning any value so we put void before main() function. Note: you must provide a return type before any function or your function will not work.

    Example:
    void function1() <---------- this is a function named as function1() of return type void
    {
    System.out.println("My Name"); <------- this will print My Name on screen and there is no need to return any value
    }

    int function2() <---------- this is a function named as function2() of return type int
    {
    return 30/3;<--------- we must return any integer value as function is of int type
    }

    function3()<-------- this function has no return type it will show you a compile time error
    {
    System.out.println("I am wrong");<----- I am not going to execute as no return type is provided please write void before function3() to make me execute
    }

    So using void before main() function is because our main() function is not returning any value.

    What the hell is "String s[]" in my first Java program ?
    Answer: In Java everything we do like printing name or taking input from user is all by default is handled in the form of String. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Do not worry about it right now just always remember to write String s[] inside main() i.e. main(String s[]).

    What is System.out.print() ????
    Answer: This is used  as a print command in Java. It will print everything that is inside "  ". System.out is a  class here and print() is a function. You will learn about them later on.

    Friends this is just a beginning if you have any doubt please do write in comment. If you want to point out any mistake you are most welcome.

    Sunday, 30 November 2014

    How to install jdk in Windows 8 and how to set path for jdk in Windows 8?

    By: Unknown On: 10:03
  • Share The Gag
  • Friends Java program is executed by Java Virtual Machine (JVM). So first thing you require to do is to install JDK i.e. Java Development Kit in your computer. After installing JDK you need to set path of jdk/bin directory.

    Where to download JDK ?
    You can download JDK from click me to download JDK 

    How to set path of jdk/bin directory ?
    You can set path only after installation of JDK in your PC. Steps that you are required to follow I am mentioning them below:

    1. Go to My Computer Properties



    2. Click on Advanced System Setting



    3. Click on Advanced Tab



    4. Click on Environment Variable



    5. Click on new user variable tab



    6. Write path variable name



    7. Copy the path of bin folder where jdk is installed



    8. Paste the Path of bin folder in variable path



    9. Click on OK button




    Saturday, 29 November 2014

    Where is Java used ? | Types of Java Applcation

    By: Unknown On: 11:47
  • Share The Gag
  • Java is used in all the devices that runs Java Virtual Machine (JVM). According to Sun there are more than 3 billion devices that run Java. Java is used vastly in designing following:

    1. Desktop Applications: Application such as antivirus, music player are mostly designed using Java.
    2. Web Applications: Java is used in designing websites such as Indian Railway Website irctc.co.in
    3. Enterprise Applications: Enterprise Applications such as Banking Application is designed using Java. 
    4. Mobile: Almost all the mobiles supports Java.
    5. Embedded System: Java is used to design embedded systems.
    6. Designing Games: Java is used for making games for desktops, for tablets and for mobiles.
    7. Artificial Intelligent: Java is also used in robotics field. It is used to design robots with artificial intelligence.

    So as you can see Java is used almost everywhere from mobiles to desktop and to tablets. Learning Java is also essential for Android Developer as Android is also a Extension of Java language. To be good Android Developer one should learn Core Java concepts. So overall it is evergreen language that is simple yet powerful in designing software.  

    Types of Java Applications:

    There mainly 4 types of Java Applications:

    1. Desktop Application: Desktop Applications are also known as Standalone applications. Java uses AWT and Swing to design Desktop Applications. Example of Desktop Applications are music player, antivirus software.

    2. Web Applications: Java uses servlet, jsp, struts to design Web Applications. These applications runs on server side and creates dynamic pages to serve client.

    3. Enterprise Application: Java is used to design Enterprise Applications. Example Banking Applications which require high level of security is designed using Java EJB.

    5. Mobile Application: Before Android, Java was used widely to design mobile applications and games. Example mobile games and applications developed for Nokia Symbian Operating System. But after Android comes, J2ME is not used that much but still it is in use. Note: Android Development also require Java platform to run application and games.Currently Android and Java ME (J2ME) are used for creating mobile applications.




    Friday, 28 November 2014

    History of Java | Features of Java

    By: Unknown On: 22:45
  • Share The Gag
  • History of Java: 
    Java is a programming language just like C++ and C. Java inherits its Syntax from C and OOPs Concept from C++. So Java is quite similar to both of these languages and yet more powerful and simpler than C and C++. Java was developed in 1995 with a motive Compile Once Run Anywhere.

    Java drives its syntax from C and supports Object Oriented Programming concepts such as inheritance, abstraction, polymorphism etc.

    Note: Java is a PURE Object Oriented Programming Language i.e. each and every program in Java is written inside a Class. In simple words we do all our coding inside a Class. We cannot make a Java program without using Classes. We will learn about Class Later on.

    Important Features of Java or why we use Java when we already have many other programming languages such as C, C++ 

    1. Simple: Java is a simple programming language. Why I called it simple you will understand it later on. One important point is to remember that if you want to learn Java you have to understand Class first.

    2. OOPs: As I already explained that Java is a PURE Object Oriented Programming Language. So to understand Java we will learn about abstraction, inheritance, polymorphism later on. One more point to remember here is that C++ is not a pure Object Oriented Programming Language but a partial Object Oriented Programming Language.

    3. Platform Independent: Java programs runs on Java Virtual Machine (JVM). So Java is a Platform Independent Programming language i.e. we can run our program or code in any machine or in any operating system having Java Virtual Machine (JVM) installed in it. Basic difference why Java is Platform Independent and C++ is not is explained below.

    Code written in C++: Suppose you have written your code in a Windows XP(any Windows OS) Operating System using C++. Now you can run that program only in Windows operating Systems. You cannot run that code in Linux or in MAC OS or in Ubuntu. So your code will run only in Windows. You have to write your code again for all other Operating Systems. This makes C++ PLATFORM DEPENDENT as well as Machine Dependent.

    Code written in Java: Suppose you have written your code in a Windows XP (any Windows OS) using Java. Now as you know that your program is run by Java Virtual Machine JVM not by Windows OS so it is independent of Operating System. You can run your code in any Operating System and in any device having JVM installed.

    4. Machine Independents:  You can run your program in any machine having JVM installed on it. You can run same code in mobiles, laptops, desktops, tablets. So you don't have to write your program again and again for different machines. Just Write Once Run Anywhere.

    5. Robust: Java is a robust programming language. For Example we cannot use a variable before initialize them. We have to give some value to a variable before using it. Exception handling and Memory management part will help us to understand it better.

    6. Garbage Collection (Memory Management): Whenever you write code some part of the memory is used by your code. But after your program runs successfully the memory space of that code has to be freed. This is done by Garbage Collection. You don't have to worry about Garbage Collection because it is automatically done by JVM. So don't think of it that much because JVM will take care of it.

    7. Exception Handling: Exception means undesirable conditions that may occur during compile time or during run time. Exception handling is yet another important feature of Java.We will come to this topic later on. Exception example like 2 divide by 0 (2/0). We will learn later on how to handle such situation because 2/0 is not possible to calculate.

    8. Multi-threading: This is one more important concept in Java. Using this we can make our program more efficient and fast. We can run our large program into small pieces using Multi-Threading. I don't want you to think of it that much we will learn it later on. 

    9. Secure: Java is far more secure than any other programming language. Why and How Java provides security we will learn about it later on.

    So all of these are the some important features of Java aka The Best Programming language. Guys don't be in hurry to learn everything within a day just read this entire post and there is nothing to worry right now. All of these will be explained with example programs for better understanding. You cannot be a good programmer (Java Programmer) in one day or in one month. It takes hard work and full dedication to understand each and every feature of Java. But remember it is still a simple language. I will explain to you each and every topic with more programs so that you can understand them.

    This is just a beginning PICTURE ABHI BAKI HAI MERE DOST.