How JAVA Programs Will Be Executed: Mnemonics
How JAVA Programs Will Be Executed: Mnemonics
Mnemonics
Compiler, Interpreter Binaries- 0s & 1s
Run1.java
Run1.java java statements.
Compiler - A compiler is a program that takes code written by programmers and translates it
into a language a computer can understand. The end result of this translation is a file that can be
run by the computer. (OR)
Any programming language (H.L.L) that will be converted in to M.L.L later it must be executed.
Interpreter - interpreter interprets line by line.
Mnemonics MOV, ADD, SUB (Assembly level language).
M.L.L
xyz.class
JRE
Coding (xyz.java) Compilation Execution
JDK
Description:
You write your code and save it as a .java file.
------------------
------------------
------------------
------------------
------------------
------------------
------------------
------------------
Translator
A.L.L
C.P.U
-------------
-------------
-------------
-------------
-------------
-------------
-------------
-------------
--
Java
Compiler
Byte Code
------------------
------------------
JVM
C.P.U
The compiler takes your .java file and compiles it into a .class file (the .class file contains
Java byte code).
The interpreter comes in when your program is run. The JVM (or interpreter) takes your
.class file and interprets it. Keep in mind that more and more frequently as JIT becomes
more prevalent it is code is not just interpreted anymore it is actually compiled at run
time into native machine code. Each platform has a JVM written specifically for it so
thatt he JVM for say a Macintosh knows exactly how to interpret the byte codes produced
by the compiler (the .class file) and make them work on Windows JVM.
How to check whether java is installed or not?
To check that you have the right version of Java installed, type the text in boldface
below. You should see something similar to the information printed below. (It's
important that you see the number 1.6or 1.5 for the Java version number, but the rest
is not critical.)
C:\Users\username>java -version
java version "1.6.0_27"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0_27-b07)
Java Hotspot(TM) Client VM (build 1.6.0_27-b13, mixed mode, sharing)
Any Programming language consists of:
1. Keywords
2. Identifiers
3. Literals
Keywords
Reserved or predefined word.
All keywords should be in lower case.
E.g. : int,void,static,public,return,public.
Identifiers
Names provided by a programmer.
No keywords are allowed here.
Support alpha numeric characters(_ is allowed)
E.g. : employee_ name.
Begin with alphabets.
E.g.: Variablename, Classname, Methodname.
Literals
Values assigned
Numeric Literal
String Literal
Character Literal
E.g. : int emp_ID = 10310 ;
K I L
POINTS:
A java program should have class definition block.
Syntax:
Class classname
{-----------
------------ Class body
------------
}
While saving a java program filename should be same as classname.
Inside class,we can code any java statements.
Main method is mandatory to start the execution.
Java file is compiled using the command javac.
Whenever a java file is compiled,if there are any syntax mistakes compiler throws an
error.If no mistakes ,then compiler generates the classfile.
By default,the classfiles are saved in the same location where java file is saved.
Java class can be executed by using a command java.
Whenever a class is executed java starts the execution by running main method.Java
executes all statements of main method sequentially.
If main method is not defined then java throws error.
Main method is required for Execution Purpose.
VARIABLES
A program can have variables to store the data required for the program.
The variable has to declared & initialized.
In java we can create 2 types variables
Primitive variables
Javac filename.java
Java classname
Reference variables
The primitive variables are declared using primitive datatypes.
Syntax :
Datatype variablename
Java supports 8 types of datatypes to declare the primitive variables.
The datatypes are:
Byte
Short Integer values
Int
Long
Float decimal values
Double
Char - character
Boolean - Boolean returns TRUE/FALSE.
Int i = 234; float f = 1.9f;
Long l = 1234l; double d = 2.3;
Types are different based on memory allocation.
Each variable must be initialized using assignment operator.
Syntax:
Datatype variablename = value
Both declaration and initialization can be done in the same line.
Eg : int i = 25;
A variable can be used in any operation by referring the name of the variable.
Before using the variable for any purpose,the variable must be initialized otherwise
compiler Throws error.
We can concatenate the string & the variable value by using concatenating operator
i.e.,+.
METHODS or FUNCTIONS
Syntax:
Modifier returntype methodname(arguments)
{---------
----------- code to implement an operation
Return value;
}
Naming Convention:
E.g.: createAccount(), openBrowser(), createSingleAccount()
Arguments:
Passing inputs to the method by declaring a variable.
Eg: int method1(int arg1)
Returns the output of integer type
Double method2(int arg1,int arg2)
Returns the output of double type but takes argument as integer type
Char method(Boolean arg3)
Returns the output of char type but takes argument as Boolean type which is
either true or false.
Void method4( ) without arguments
Perform calculation but doesnt return the output of either type.
To achieve code reusability we go for developing methods.
We can call methods as many no. of times we can once its developed.
Every method should have a return statement.
If a method is not returning any value then it must be declared as void.
The method statements are executed only when methods are invoked.
While invoking a method, if method needs an argument then we need to pass the value
for the argument.
NOTE 1: In java we can declare 2 types of variables as we said before.
Primitive variables.
Referenced variables.
1. Any variable declared using primitive datatypes are known as primitive variables.
These variables are capable of storing only information.
2. Any variable declared by classname (or) interface is known as Referenced variable.
These variables are capable of storing an address of the object.
NOTE 2: Any variable declared within the context of open & close brackets is known Local
Variables.The scope of local variable is limited to the content.
Any variable declared outside the context of classbody then its known as Global Variable can
be used by any methods of the class.