Skip to main content

Java Basics Overview


How Java is Different from others ??

Unlike other programming language java is compiled and interpreted language. Java IDE acts as a compiler and JVM java virtual machine behave like an interpreter. i.e. when any program let say Hello saved after compiling is in Hello.java extension and after compiling this file we get Hello.Class extension file is called as class-file, byte-code or intermediate code. Byte-code is not dependent for any specific machine so it is also called as intermediate code.
To convert this byte-code into machine code or machine understandable format JVM is used which is different for different operating system. JVM is actually a kind of software used for interpreting purpose hence it is called virtual machine.


In Java Programming language, why every program is written inside class?

          As we Know Java is an Object Oriented programming language so every type of the problem is solved with the representation of the object. And as we also know that class is the template for any object. Any functionality, attribute shown by an object is included in that particular type of class. So to implement the behavior of that kind of the object, the class relating that object first have to be determined and implemented. So it is necessary to use class in any program of the java.

           In Java programming even in a same class why it is necessary to create object to access the data and function of that particular class?

Ø     In Object Oriented Programming Language when we create a class it means that we are creating a template which contains certain attributes and functionality of a certain object. To implement that class template features object has to be defined. Memory location is not assigned until you have created the object. So when you create object of that class then only the memory is allocated. And then by accessing that class functionality via object is done.

Or
Ø  We can also understand as this: when Class is declared, only template is created which contain the attribute and functionality of its respective object and to implement that template, object is obviously required. 

       Why import statement are used in Java Programs?

      As we used to include different library files while writing programs in C/C++ by using structure like #include<stdio.h> to implement the function definition like printf, scanf etc.
But as we know java is purely Object Oriented Programming Language so the particular class is imported which contains function definitions and is already built in package using the keyword import.
If we have to use Scanner to give certain input to the program then Scanner class is imported which reside inside the package java.util.Scanner.

Why System.out.println() for printing ?

For taking input from keyboard input stream is used and output stream is used for output. likewise printStream is a class implementing outputStream. System is a built-in class inside java and inside Java. lang package, PrintSream is another class defined inside System class and PrintStream class contain method definition of println. Out is a static member acts as an object of PrintStream class defined as
                Public static final PrintStream out;

So the convention System.out.println(“Arguments”);  is written.

Why Public Static void main (String args []) { /*definition*/ } ?

Public is a kind of access specifier due to which we can access it from outside the class. Since main is the function that acts as an execution point. Main function is called by the JVM which is outside the class so it must be declared as public.

Static is a kind of keyword used in java to specify that there is no need to create any instance of it. As we know main method also reside inside a class and to access the particular function of a class from outside the class (In this case from JVM), an instance of that particular class is needed, so to avoid these we simply put static to make access main method outside of class. Mostly the method which are single for each class are declared as static. Static variables and method are compile time memory.

No any return type of main method since it is the execution point of program, there is no any point on returning other type than void from main method.


      String is a pre-defined class name in java. And args [] is a variable of array types. It’s a variable name of String type object. So we can also change the name of args []. String class and its object are passed here as an argument through command line to pass information to the main method. It is String because String can accept variety of data type like int, float, char, array etc. If int was present instead of String then the error will arise when user enters floating point as an command line arguments.




Comments

Popular posts from this blog

Parsing xml file with JSOUP

Let say we have a xml formatted file as: snapshot of a stack-overflow data dump xml file as posts.xml Here is a sample program to extract title and tag from above xml file using jsoup library in java. Once importing  jsoup library  in our program, we can write program as: public class JsoupParser { public static void main(String[] args) throws Exception { String question; String tag; File fileQuestion = new File( "F: \\ Data \\ stackoverflow.com-Posts \\ posts.xml" ); Document docQuestion = Jsoup. parse (fileQuestion, "UTF-8" ); Elements eachrowQuestion = docQuestion.getElementsByTag( "row" ); for ( int i = 0 ; i < eachrowQuestion.size(); i++) { question= eachrowQuestion.get(i).attr( "Title" ); tag=eachrowQuestion.get(i).attr( "Tags" ); System.out.println(question+ "---->" + tag); } } } Now we can

Shahrukh khan At a Glance

Unbeaten king since twenty-two years: Shah Rukh Khan (“Naam toh suna hi hoga”) Through this article i am not emphasizing over my favorite actor. He is the legend that every-one of you must know about him. Figuratively Shahrukh khan is an actor, film producer and TV personality while speaking literally these words aren’t enough that describes khan. After reading this article anyone can figure out why i am approaching him so deeply.                                                                                                                   Shah Rukh Khan, informally referred to as SRK, is an Indian film actor. Often referred to as “The King of Bollywood”, Khan has acted in over 65 Hindi films.  In 2005, the Government of India honored him with the Padma Shri for his contributions towards Indian Cinema which is regarded as a very honorable award in India. Glancing on his study, he had graduated with a bachelor degree in economics. He had started his career with TV ser