Core JAVA

Write Once, Run Anywhere

  • Java Basic
Java - Overview
Java - Environmental Setup
First Step towards Java Programming
Importing Classes
Java - Basic Datatypes
Java - Variable Types
Java - Modifier types
Java - Basic Operators
Java - Loop Control
Java - Decision Making
Java - Numbers
Java - Characters
Java - Strings
Java - Arrays
Java - Date & Time
Java - Regular Expressions
Java - Methods
Java - Files and I/O
Java - Exceptions
  • Java Object Oriented
Java - Inheritance
Java - Overriding
Java - Polymorphism
Java - Abstraction
Java - Encapsulation
Java - Interfaces
Java - Packages
  • Java Advanced
Java - Data Structures
Java - Collections
Java - Serialization
Java - Networking
Java - Multithreading
Java - Applet Basics


      In our first Java program, we are using namely, System and String. These classes belong to a package called java.lang(here lang represents language). So thees two classes must be imported into our program, as shown below:





WhenEver we want to import several classes of the same package, we need not write several import statements, as shown above; instead, we can write a single statement as:





     Here * means all the classes and interfaces of that package, i.e. java.lang, are imported into our program. In import statement the package name that we have written acts like a reference to the JVM to search for the classes there. JVM will not copy any code from the classes or packages. On the other hand, when a class name or method name is used, JVM goes to the Java library, executes the code there, comes back, and substitutes the result in taht place of the program. Thus the Java program size will not increased.

After importing the classes into the program, the next step is to write a class. Since Java is purely an object oriented programming language, we cannot write a Java program without having at least one class or object. So it is mandatory that every Java program should have at least one class in it. How to write a class? We should use class keyword for this purpose and then write the class name.





A class code starts with a { and ends with a }. We know that a class or an object contains variables and methods (functions). So we can create any number of variables and methods inside the class. This is our first program, so we will create only one method, i.e. compulsory in it-main( ) method.

Why should we write main( ) method? Because, if main( ) method is not written in a Java program, JVM will not execute it. main( ) is the starting point for JVM to start execution of a Java program.





Next to main( ) method's name, we have written String args[]. Before discussing the main( )method, let us first see how a method works. A method, generally, performs two functions.

  • It can accept some data from outside.
  • It can also return some result.

Let us take sqrt( ) method. This method is used to calculate square root value of a given number.So at the time of calling sqrt( ) method, we should pass a number(e.g. 16)for which we want to calculate square root. Then, it calculates square root value and returns the result (e.g. 4) to us. Similarly, main( ) method also accepts some data from us. For example, it accepts a group of strings, which is also called a string type array. This array is String args[ ], which is written along with the main( ) method as:





Here args[ ] is a array name and it is of String type. This means that it can store a group of strings. Remember, this array can also store a group of numbers but in the form of strings only. The value passed to main( ) method are called arguments. These arguments are stored into args[ ] array, so the name args[ ] is generally used for it.

A method can return some result. If we want the method to return the result in form of an integer, then we should write int before the method name. Similarly, to get the result in string form or as a single character, we can write string or char before it, respectively. If a method is not meant to return any value, then we should write void before that method's name. void means, no value. main( ) method does not return any value, so void should be written before that method's name.

A method is executed only when it is called. But, how to call a method? Methods are called using 2 steps in Java.

1. Create an object to the class to which the method belongs. The syntax of creating the object is:




2. Then the method should be called using the objectname.methodname( ).

Since main( ) method exists in the class First; to call main( ) method, we should first of all create an object to First class something like this:





Then call the main( ) method as:





So, if we write the statement:





inside the main( ) method, then JVM will execute this statement and create the object.





    By looking at the code, we can understand that an object could be created only after calling the main( ) method. But for calling the main( ) method, first of all we require an object. Now, how is it possible to create an object an object before calling the main( ) method? So, we should call the main( ) method without creating an object. Such methods are called static methods and should be declared as static

Static methods are the methods, which can be called and executed without creating the objects.Since we want to call main( ) method without using an object, we should declare main ( ) method as static . Then how is the main( ) method called and executed? The answer is by using the classname.methodname( ). JVM calls main( ) method using its class name as First.main( ) at the time of running the program.

Powered by Blog - Widget
Face Upward - Widget