Introduction Java
Introduction Java
Prof AK Bhateja
Java
• Java developed by Sun Microsystems and
released in 1995.
• Java is Write Once, Run Anywhere.
• Every 6 months, Oracle releases out new Java
versions.
• Java SE 12 was released in March 2019 and is
available for free public updates upto
September 2019.
Bytecode
• Bytecode is program code that has been
compiled from source code into low-level code designed for
a software interpreter.
• It may be executed by a virtual machine (such as a JVM) or
further compiled into machine code, which is recognized by
the processor.
• It is possible to write bytecode directly, but difficult than
writing code in a high-level language.
• Java bytecode is contained in a binary file with a .CLASS
files and are generated from source code using a compiler,
like javac.
• Bytecode is similar to assembly language. Both may be
considered "intermediate languages" i.e. between source
code and machine code.
• The primary difference between the two is that bytecode is
generated for a virtual machine (software), while assembly
language is created for a CPU (hardware).
The bytecode format
• Bytecodes are the machine language of the Java virtual
machine.
• When a JVM loads a class file, it gets one stream of
bytecodes for each method in the class.
• The bytecode stream for a method is executed by
interpreter when that method is invoked during the
course of running the program.
• The Java interpreter is actually a part of JVM.
• A method's bytecode stream is a sequence of
instructions for the Java virtual machine. Each
instruction consists of a one- byte opcode followed by
zero or more operands.
• The opcode indicates the action to take.
• The set of instructions for the JVM may differ from
system to system but all can interpret the bytecode.
C++ vs Java
C++ Java
platform-dependent. platform-independent.
supports multiple doesn't support multiple
inheritance. inheritance through class.
supports operator doesn't support operator
overloading. overloading.
supports pointers. supports pointer internally.
Pointer program in Pointer program in java can
C++ can be written. not be written.
C++ vs Java
C++ Java
supports both call supports call by value only.
by value and call by There is no call by reference in
reference. java.
doesn't have built-in has built-in thread support.
support for threads.
supports virtual has no virtual keyword. We can
keyword so that we override all non-static methods
can decide whether by default. In other words, non-
or not override a static methods are virtual by
function. default.
C++ vs Java
C++ Java
an object-oriented also an object-oriented language.
language. However, However, everything (except
in C language, single fundamental types) is an object in Java.
root hierarchy is not It is a single root hierarchy as everything
possible. gets derived from java.lang.Object.
doesn't support supports documentation comment
documentation (/** ... */) for java source code.
comment.
program memory JVM manages memory through a
managed by the process called garbage collection, which
programmer continuously identifies and eliminates
unused memory in Java programs.
C++ vs Java
C++ Java
support header files uses the import keyword to
include different classes and
methods
support default doesn't support default
arguments arguments
supports structures doesn't support structures and
and unions. unions.
JDK, JRE and JVM
Identifier in Java
Def: A Java identifier is a name given to a package, class,
interface, method, or variable.
• Identifiers must be composed of letters (a-z), (A-Z),
numbers (0-9), the underscore (_) and the dollar sign ($).
• Identifiers may only begin with a letter, the underscore or a
dollar sign
class Test {
public static void main(String [] args) {
int x = 10;
}
}
• No. of idenfiers: 5 i.e. Test (class name), main (function
name), String (pre defined java class), args (array name), x
(variable name)
Some Important Features of Java
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
Data Types in Java
• Primitive data types: Primitive datatypes are
predefined by the language and named by a keyword
– There are 8 primitive data types: boolean, char, byte,
short, int, long, float, double.
– boolean (1 bit), byte (1 byte), char (2 bytes),
short (2 bytes), int (4 bytes), long (8 bytes),
float (4 bytes), double (8 bytes)
– char data type in Java is 2 bytes because it uses UNICODE
character set
Unicode is a 16-bit character encoding standard and is
capable to represent almost every character of well-known
languages of the world.
• Non-primitive data types: The non-primitive data
types include Classes, Interfaces, and Arrays.
Data Type
Primitive Non-primitive
String
Boolean Numeric
Array
Character integral etc.
integer Floating-point
default:
code to be executed if all cases are not matched;
}
//Java do-while Loop