Java Basics

 PROGRAM EXECUTION



Java virtual Machine 

Java virtual Machine (JVM), is a virtual Machine that provides runtime environment to execute java byte code. It doesn't understand Java type, that's why we compile our *.java files to  obtain *.class files that contain the bytecodes understandable by the JVM. JVM control execution of every Java program.

Bytecode

                Bytecode is nothing but the intermediate representation of Java source code which is produced by the Java compiler by compiling that source code. This byte code is an machine independent code.It is not an completely a compiled code but it is an intermediate code somewhere in the middle which is later interpreted and executed by JVM.Bytecode is a machine code for JVM.










Anatomy of a Java Program

·         Comments

·         Package

·         Reserved words

·         Modifiers

·         Statements

·         Blocks

·         Classes

·         Methods

·         The main method

Comments

                In Java, comments are preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.

 Package

·         Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.

·         Packages are used for:

o   Preventing naming conflicts. For example there can be two classes with name Employee in two packages

o   Making searching/locating and usage of classes, interfaces, enumerations and annotations easier

o   Providing controlled access: protected and default have package level access control.

§  A protected member is accessible by classes in the same package and its subclasses.

§  A default member (without any access specifier) is accessible by classes in the same package only.

o   Packages can be considered as data encapsulation (or data-hiding).

Reserved Words

                Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class.

Modifiers    

    Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs. 

A pair of braces in a program forms a block that groups components of a program.


Statements

                A statement represents an action or a sequence of actions. Every statement in Java ends with a semicolon (;).

Blocks






Classes

                The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. a program is defined by using one or more classes.

Methods

What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message. 

main Method

The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method.

The main method looks like this:

public static void main(String[] args)

{

  // Statements;

}

First Java Program

public class MyFirstJavaProgram

 {

 /* This is my first java program. 

  This will print 'Hello World' as the output

*/

public static void main(String []args)

 {            

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

    }

}





How to write and execute a java program

·         Open notepad and add the code as above. 

·         Save the file as: MyFirstJavaProgram.java. 

·         Open a command prompt window and go to the directory where you saved the class. 

·         Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line 

·         Now, type ' java MyFirstJavaProgram ' to run your program. 

·         You will be able to see ' Hello World ' printed on the window


Basic Syntax

·         Case Sensitivity

o   Class Names :For all class names the first letter should be in Upper Case.

§  Example: class MyFirstJavaClass

o   Method Names :All method names should start with a Lower Case letter.

§  Example: public void myMethodName()

·         Program File Name :Name of the program file should exactly match the class name.

·         When saving the file, we should save it using the class name and append '.java' to the end of the name

·         public static void main(String args[]) : 

                        Java program processing starts from the main() method which is a mandatory part of every Java program.

 



 Go through the following link for more:

         https://www.youtube.com/watch?v=WouuyDxSZJc

No comments:

Post a Comment