Introduction to Java
Introduction to Java.
Beginner to java..?? No problem in this article let's see how to start programmimg in java.Usually when people hear the word "Java" they think it is very difficult, guys trust me java is easier than C programming. Just that java is object oriented programming language.If you know the syntax in java you can write any basic programs.
Introduction:
Java is an object oriented programming language.Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at least some extent object-oriented.Java was designed to be easy for the professional programmer to learn and use effectively. Assuming that you have some programming experience, you will not find Java hard to master. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier.Pre-requisites to start the programming in java(Windows).
1)Install jdk software.(Or you can use the online compiler click here.)2)Write a simple program(As shown below) in text editors(eg:notepad++).
3)Save the file name with extension .java(eg: program.java).
4)Open the command prompt in the folder you saved the java program.
5)The command used for compilation is "javac example.java".
6)When it compiles you will get example.class file(or any .class file depending on which name you declared).
7)The command to run java is "java example"(.class name of the file).
Guys let's see now how to write an example code and understand each line of it.
Code:
import java.util.*;
class addition
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the first number to be added");
a=scan.nextInt();
System.out.println("Enter the first number to be added");
b=scan.nextInt();
c=a+b;
System.out.println("The sum of two numbers is "+c);
}
}
Explanation:
import java.util.*;
This line indicates that the user is importing the required library for the execution of program.The "*" indicates that user is importing all the classes from this library.class addition
All the java programs begin with the word class. Here class is collection of the objects.Let's see the actual meaning of class in my next post.public static void main(String args[])
This line begins the main( ) method. As the comment preceding it suggests, this is the line at which the program will begin executing. All Java applications begin execution by calling main().The public keyword is an access modifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. In main( ), there is only one parameter whic is passed. String args[ ] declares a parameter named args, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character strings.Scanner scan=new Scanner(System.in);
To take the input from the user we declare a class Scanner with it's instance named scan by passing the parameter System.in to read the inputs from user.int a,b,c;
In this line we declare the variables required for our program.System.out.println("Enter the first number to be added");
This line displays the message written in " ". The syntax for this is system.out.println("") and println is used to jump to next line and we can also use just system.out.print(""). This would not jump to next line.a=scan.nextInt();
Here a indicates in which variable you wanna store the data from user and scan.nextInt syntax indicates that you are scanning the type of data from the user.If you want to scan float then it is scan.next.Float etc so on.
No comments: