Pages

Introduction to Java


Definition :- A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface.

JAVA Stands for
J - James Gosling J - Just
A - Arthur A - Another
V - Van Hoff and V - Virtual and
A - Andy Bechtolsheim A - Architecture
SUN Stands for
S - Stanford
U - University
N - Network

JRE (Java Runtime Environment)

  1. Java Runtime Environment (JRE) is included in JDK. It is one of the main parts of a JDK to run java application. It is a minimum requirement to run any Java application.
  2. Java Runtime Environment (JRE) consists of Java Virtual Machine (JVM), various core classes, libraries and various other components to run Java programs and applets.
  3. Java Runtime Environment (JRE) plug-in is installed in every browser to run java applets and to deploy Java Web Start. Java Web Start is used to deploy individual application over the network.
  4. Java Runtime Environment (JRE) is a sub-set of JDK and it is can also be downloaded separately. Java Runtime Environment (JRE) is required on all operating systems to run java applications. It is also present on mobile devices which are capable of running Java applications.
  5. Java Runtime Environment is the very basic requirement to run java programs. It loads and runs java applications with the use of JVM.
  6. When a java file is complied it generates a byte code file i.e. “.class” file. The byte code file is platform independent but the Java Runtime Environment is platform dependent.
  7. It means if you need to run java programs on Linux, you need a JRE for Linux. JRE is present for most of the operating system.
  8. Hence java programs are platform independent but Java Runtime Environment is platform dependent means different platform have different JRE.

JVM (Java Virtual Machine)

  1. Java Virtual Machine also known as JVM is a virtual machine that is included in Java Runtime Environment to run java applications.
  2. JVM can also be called as the heart of Java platform. If you have JRE installed on your computer or any device it means you have JVM installed in it.
  3. JVM is acts like a virtual environment to run platform independent java byte codes.
  4. Byte codes are the files with “.class” extension that are generated after compilation of java file.
  5. Byte codes are the intermediate code between java and machine language. JVM provides across platform functionality to java applications.
  6. Most programming languages like C compiled the programs according to the operating system and processor architecture; it means they cannot run on machines having different processor architecture.
  7. The JVM is a machine within original machine that acts as intermediate between the application and the original machine.
  8. The JVM converts the byte code files according to the original machine requirement.
  9. Hence java applications are platform independent as the JVM take care of all the platform dependencies.
  10. JVM also runs the “.jar” files that are Java Archive files. Java API is also bundled with JVM in Java Runtime Environment.
  11. The JVM is responsible for “write once run anywhere” (WORA) nature of java programs.
  12. JVM also have lots of other feature like automated exception handling, memory management, garbage collection.

JVM Architecture (How Does the JVM Work ?)

Java Virtual Machine is divided into three main subsystems.

  1. Class Loader Subsystem
  2. Runtime Data Area
  3. Execution Engine

1. Class Loader Subsystem


Java's dynamic class loading functionality is handled by the class loader subsystem. It loads, links. and initializes the class file when it refers to a class for the first time at runtime, not compile time.

Loading : Classes will be loaded by this component. BootStrap class Loader, Extension class Loader, and Application class Loader are the three class loader which will help in achieving it.
  1. Boot Strap ClassLoader : Responsible for loading classes from the bootstrap classpath, nothing but rt.jar. Highest priority will be given to this loader.
  2. Extension ClassLoader : Responsible for loading classes which are inside the ext folder (jre\lib).
  3. Application ClassLoader : Responsible for loading Application Level Classpath, path mentioned Environment Variable etc.
The above Class Loaders will follow Delegation Hierarchy Algorithm while loading the class files.

Linking :
  1. Verify : Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get the verification error.
  2. Prepare : For all static variables memory will be allocated and assigned with default values.
  3. Resolve : All symbolic memory references are replaced with the original references from Method Area.
Initialization : This is the final phase of Class Loading, here all static variables will be assigned with the original values, and the static block will be executed.

2. Runtime Data Area


The Runtime Data Area is divided into 5 major components:

Method Area : All the class level data will be stored here, including static variables. There is only one method area per JVM, and it is a shared resource.

Heap Area : All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread-safe.

Stack Area : For every thread, a separate runtime stack will be created. For every method call, one entry will be made in the stack memory which is called as Stack Frame. All local variables will be created in the stack memory. The stack area is thread-safe since it is not a shared resource. The Stack Frame is divided into three sub entities:
  1. Local Variable Array : Related to the method how many local variables are involved and the corresponding values will be stored here.
  2. Operand stack : If any intermediate operation is required to perform, operand stack acts as runtime workspace to perform the operation.
  3. Frame data : All symbols corresponding to the method is stored here. In the case of any exception, the catch block information will be maintained in the frame data.
PC Registers : Each thread will have separate PC Registers, to hold the address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.

Native Method stacks : Native Method Stack holds native method information. For every thread, a separate native method stack will be created.

3. Execution Engine


The bytecode which is assigned to the Runtime Data Area will be executed by the Execution Engine. The Execution Engine reads the bytecode and executes it piece by piece

Interpreter : The interpreter interprets the bytecode faster, but executes slowly. The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required.

JIT Compiler : The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine will be using the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler, which compiles the entire bytecode and changes it to native code. This native code will be used directly for repeated method calls, which improve the performance of the system.
  1. Intermediate Code generator : Produces intermediate code.
  2. Code Optimizer : Responsible for optimizing the intermediate code generated above.
  3. Target Code Generator : Responsible for Generating Machine Code or Native Code.
  4. Profiler : A special component, responsible for finding hotspots, that is whether the method is called multiple times or not..
Garbage Collector : Collects and removes unreferenced objects. Garbage Collection can be triggered by calling System.gc(), but the execution is not guaranteed. Garbage collection of the JVM collects the objects that are created.

Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native Libraries required for the Execution Engine.

Native Method Libraries : This is a collection of the Native Libraries which is required for the Execution Engine.


Source File Declaration

  1. There can be only one public class per source code file.
  2. Comments can appear at the beginning or end of any line in the source code file.
  3. They are independent of any of the positioning rules discussed here.
  4. If there is a public class in a file, the name of the file must match the name of the public class.
  5. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.
  6. If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  7. If there are import statements, they must go between the package statement and the class declaration.
  8. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.
  9. If there are no package or import statements, the class declaration must be the first line in the source code file.
  10. import and package statements apply to all classes within a source code file.
  11. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
  12. A file can have more than one nonpublic class. Files with no public classes can have a name that does not match any of the classes in the file.

OOPS (Object oriented programs)


Class : A Template that describes the kinds of state and behavior that object of its type support.
Object : At runtime, when JVM encounters the new keyword it will use the appropriate class to make an object which is an instance of that class. That object will have its own state and access to all of the behavior defined by its class.
Encapsulation : Encapsulation is a process of wrapping up data and methods together into single unit.
Abstraction : Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Inheritance : Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance represent IS-A relationship.
Polymorphism : Polymorphism is the ability of methods to behave differently based on the object calling it.
State (Instance Variables) : Each object will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object’s instance variables make up the object’s state.
Behavior (Methods) : When a programmer creates a class, she creates methods for that class. Methods are where the class logic is stored. Methods are where the real works gets done. They are where algorithms get executed and data gets manipulated.

Legal Identifiers


Definition:
Legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (like underscores).
Rules :
  1. Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ).
  2. Identifiers cannot start with a number!
  3. After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.
  4. In practice, there is no limit to the number of characters an identifier can contain.
  5. You can't use a Java keyword as an identifier.
  6. Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Class Declaration


Definition: The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase. For classes, the names should typically be nouns.
For Examples: Dog, Account, and PrintWriter

Interface Declaration


Definition: The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase. For interface, the names should typically be nouns. For interfaces, the names should typically be adjectives.
For Examples: Runnable, Serializable

Method Declaration


Definition: The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs.
For Examples: getBalance, doCalculation, setCustomerName, addDetails.

Variable Declaration


Definition: Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us.
For Examples: buttonWidth, accountBalance, myString, goodWell.

Constants Declaration


Definition: Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators.
For Examples: MIN_HEIGHT, MAX_HEIGHT, MIN_WIDTH, MAX_WIDTH.

Java Bean


Definition: Java Beans are simple Java classes that follow certain specifications to develop dynamic content. Java Beans are reusable components that separate the business logic from the presentation logic.
Rules:
  1. Class must be public.
  2. Properties (variables) must be private. The setter method’s prefix must be set.s
  3. Class must have a public default constructor.
  4. Each private property should have one public setter and getter methods.
  5. Class has to implement serializable interface.
  6. If the property is not a boolean. The getter method’s prefix must be get.
  7. If the property is a boolean, the getter method’s prefix is either get (or) is.
  8. Setter method signatures must be marked public, with a void return type and an argument that represents the property type.
  9. To complete the name of a getter (or) setter method, change the first letter of the property name to uppercase and then append it to the appropriate prefix.
  10. Getter method signatures must be marked public, take no arguments, and have a return type that matches the argument type of the setter method for that property.

Here's a list of other Escape Sequences in Java:

  1. \t - Insert a tab in the text at this point.
  2. \b - Insert a backspace in the text at this point.
  3. \n - Insert a newline in the text at this point.
  4. \r - Insert a carriage return in the text at this point.
  5. \f - Insert a form feed in the text at this point.
  6. \' - Insert a single quote character in the text at this point.
  7. \" - Insert a double quote character in the text at this point.
  8. \\ - Insert a backslash character in the text at this point.