Java was needed as a programming language that could run on a variety of platforms without having to change machine level instructions much.
It was developed for all types of applications majorly web,console,windows(GUI) based apps.
A Java Environment contains following parts
Compile this using "javac FirstApp.java" and execute using "java FirstApp".
If you get output on the screen "This is just a Beginning".You have already begun development.
Compiling a Java Program
When we executed our app we performed 2 operations Compiling and Execution.Lets drill down further a bit into the same.
Java Source code is stored in "*.java" files.So to compile a program we use command
"javac MyFirstApp.java".Here "MyFirstApp.java" is the file name and not the class name.This command assumes that current directory contains your java file and Java Language in file corresponds to current version of Java installed.
However we can overwrite these command-line option "-source" and "-classpath" abbreviated as "-cp".This basically means that command
"javac -source 1.5 -cp . MyfirstApp.java" will execute coding using Java version 1.5 and look for user files in current directory(represented by a period ".").
Once the compilation is successful which means you are not getting any exception messages it will create a file "MyfirstApp.class".
This means we are ready to move into the execution phase.
Executing a Java Application
Navigate to the directory containing the class file and enter command
"java -enableassertions MyProgram"
Remember here "MyProgram" is the name of the class and not the Java code file."enableassertions" is necessary for programs using assertions we can abbreviate the same to "-ea".
In nutshell we can say Compiler looks for the file and Interpreter looks for the class.
Executing Applet
Compiler compiles both applications and applets. However to execute an applet we need to embed it into a "java enabled browser" or an "applet viewer".
To run using applet viewer use "appletviewer MyApplet.html"
Since applets are embedded to html pages we execute them using appletviewer. Adding applet to htmlpage
<applet code="MyFirstApplet.class" width=300 height=300></applet>
Now we can either view the above code in browser or Applet Viewer. Sometimes we may face challenges related to compatibility issues of the browser so it is always better to first test in applet viewer.
It was developed for all types of applications majorly web,console,windows(GUI) based apps.
A Java Environment contains following parts
- Java Virtual Machine(JVM)
- A hypothetical computer that computes Java Programs into byte code.
- Java API
- Set of components that provide facilities for writing java applications.
- Java Compiler
- Converts Source code to binary programs consisting of byte codes.
- Byte codes are machine instructions for JVM.
- Java Interpreter
- Inspects and deciphers the byte codes for JVM, checks for its safety in execution and executes the actions that byte code instructions specify in JVM.
- Java interpreter sits between our program and physical machine.
Java supports following types of applications
- Applets
- Special programs that can embed into webpages and perform special operations.
- Needs special security requirements such as no access to data on client's system.
- Java Server Pages(JSP)
- Provides a special means for building a server application that can dynamically create and download HTML pages to a client.
- Html pages can be customized on type of request(get,post,put) and data sent.
- These pages can also contain applets.
- Window based applications
- Platform independent applications that can run on different operating systems.
- Console based applications
Features of Java Language
- Machine Independent
- Since Java program is in byte code it is completely machine independent.Any machine that has a java environment implemented will handle Java programs.
- Object Oriented
Java Class Loading
- Java uses a class called as Class Loader to perform the job of loading class definitions into memory, from which the JVM accesses static attributes and methods and creates object instances.
- Several Instances of Class Loader are used to perform class Loading.
- One instance is used to load classes from jar files in WEB-INF/lib and class files under WEB-INF/classes.
- Another instance is used to load classes from jar files found in lib directory of Tomcat.
- A class loader locates classes by searching a class path.
- For an Application running in Tomcat the class-path includes following directories.
- Various locations under $JAVA
- ${TOMCAT_HOME}/lib/*.jar
- Java Home
- /WEB-INF/class/**.class
- /WEB-INF/lib/*.jar
- The tomcat libraries are shared by all the applications.
- Java statements must end with a semicolon.
- Code blocks are written within curly braces
- Java is statically-typed language. All variables must be declared before they are used
Development
To start development on Java we need to install Java Development Kit(JDK) which has following structure
Let's now go through each of the directories
Let's now go through each of the directories
- BIN - This directory has Java Compiler,Interpreter and other executable.
- Demo - Sub directories containing Demo Code
- Include - Header files for native code
- Sample - JNLP Samples
- JRE - Java Run time Environment
- BIN - Executable's for Runtime
- LIB - Class Libraries
- LIB - Files used by Executable's
Java Runtime Environment(JRE)
A Java Compiler converts Java Source Code that we write into binary program consisting of Byte Codes.
Byte Codes are Machine instructions for JVM. On run Java Interpreter inspects and deciphers the byte code for the same.Java interpreter ensures that code is not tampered with and is safe to execute.
So any machine which has Java Interpreter installed can run java program we do not need to alter native machine code instructions.
So Now lets start off writing our first Java Program called as FirstApp.java
package firstapp;
/**
*
* @author Gaurav Matta
*/
public class FirstApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("This is just a Beginning");
}
}
Compile this using "javac FirstApp.java" and execute using "java FirstApp".
If you get output on the screen "This is just a Beginning".You have already begun development.
Compiling a Java Program
When we executed our app we performed 2 operations Compiling and Execution.Lets drill down further a bit into the same.
Java Source code is stored in "*.java" files.So to compile a program we use command
"javac MyFirstApp.java".Here "MyFirstApp.java" is the file name and not the class name.This command assumes that current directory contains your java file and Java Language in file corresponds to current version of Java installed.
However we can overwrite these command-line option "-source" and "-classpath" abbreviated as "-cp".This basically means that command
"javac -source 1.5 -cp . MyfirstApp.java" will execute coding using Java version 1.5 and look for user files in current directory(represented by a period ".").
Once the compilation is successful which means you are not getting any exception messages it will create a file "MyfirstApp.class".
This means we are ready to move into the execution phase.
Executing a Java Application
Navigate to the directory containing the class file and enter command
"java -enableassertions MyProgram"
Remember here "MyProgram" is the name of the class and not the Java code file."enableassertions" is necessary for programs using assertions we can abbreviate the same to "-ea".
In nutshell we can say Compiler looks for the file and Interpreter looks for the class.
Executing Applet
Compiler compiles both applications and applets. However to execute an applet we need to embed it into a "java enabled browser" or an "applet viewer".
To run using applet viewer use "appletviewer MyApplet.html"
Since applets are embedded to html pages we execute them using appletviewer. Adding applet to htmlpage
<applet code="MyFirstApplet.class" width=300 height=300></applet>
Now we can either view the above code in browser or Applet Viewer. Sometimes we may face challenges related to compatibility issues of the browser so it is always better to first test in applet viewer.
Comments in Java
- Comments in java can be written in following ways as shown in below class.We can classify comments as follows.
- Multi line comments
- Single line comments
- Comments can be written inline i.e at the end of every line as shown below.
/**
*
* @author Gaurav Matta
*/
public class FirstApp {
/**
* @param args the command line arguments
* Multiline comments
*/
public static void main(String[] args) {
//Single Line comment
System.out.println("This is just a Beginning"); // Inline Comment
}
}
Statements in Java
- Statements in Java are identified by code in braces for example above code shows a statement to output string on console.
Access Modifiers
- Java has following access modifiers and their access levels are provided in sub tabs
- Public
- Class
- Package
- Subclass
- World
- Public identifier makes the members accessible outside the class limited to above levels.
- Protected
- Class
- Package
- Subclass
- Protected Identifier makes the members accessible to derived classes.
- No Modifier(Default Modifier)
- Class
- Package
- Default or No Modifier limits visibility of a member to package level.
- Private
- Class
- Private access modifiers make sure that members of a class are not accessible outside the class of course if they are not accessible they are not modifiable.
- If we look at the below class we find that we have a class has been applied with keyword public, instance variables have been applied with keyword private and member functions with public these are called as access modifiers.
1: public class calc {
2: private final int digit1;
3: private final int digit2;
4: public calc(int digit1,int digit2)
5: {
6: this.digit1=digit1;
7: this.digit2=digit2;
8: }
9: public int add()
10: {
11: return digit1+digit2;
12: }
13: public int subtrct()
14: {
15: return digit1-digit2;
16: }
17: public int divide()
18: {
19: return digit1/digit2;
20: }
21: public int multiply()
22: {
23: return digit1*digit2;
24: }
25: public int remainder()
26: {
27: return digit1%digit2;
28: }
29: }
- POJO
- Class must be public.
- Properties(fields) must be private
- Must have public getter and setter methods
- Default constructor is mandatory.
- Can have optional constructor with arguments.
- Pojo's make the programs more readable and increase reusability.
- Bean
- A bean is a POJO and additionally it must be serializable
- A serializable object is converted to Byte Stream and stored in memory or written to database
- Byte Stream can be deserialized to reconstruct the object.
- Serialization helps in sharing object with multiple systems and caching for faster data retrieval.
- There could be multiple java instances running in an enterprise.
- Take an object from one instance serialize it, share that serialized object with another instance which can deserialize it and use it.
- Serialized objects can be cached for faster data retrival.
- It is faster than recreating the object.
- We serialize it,cache it,and deserialize and use it serve another client which is requesting for the same object.
- We can also achieve persistance by storing intermediate state of an object with seriliazition.
- To make an object serializable the class needs to implement Java.io.Serializable interface