Professional Documents
Culture Documents
Introduction
Introduction
9/15/2011 17
WORA(Write Once Run Anywhere)
18
JVM
• JVM (Java Virtual Machine) is an abstract machine that enables your
computer to run a Java program.
• When you run the Java program, Java compiler first compiles your Java
code to bytecode. Then, the JVM translates bytecode into native machine
code
• When you run the Java program, Java compiler first compiles your Java
code to bytecode. Then, the JVM translates bytecode into native machine
code.
class body
}
Setting Variable Environment
Temporary:
To set the temporary path of JDK, you need to follow the following
steps:
• Open the command prompt
• Copy the path of the JDK/bin directory
• Write in command prompt: set path=copied_path
set path=C:\Program Files\Java\jdk1.8.0_191\bin
Permanent:
For setting the permanent path of JDK, you need to follow these
steps:
• Go to MyComputer properties -> advanced tab -> environment
variables -> new tab of system variable -> write path in variable
name -> write path of bin folder in variable value -> ok -> ok -> ok
19
How is Java different from C…
• C Language:
– Major difference is that C is a structure oriented language and
Java is an object oriented language and has mechanism to define
classes and objects.
– Java does not support an explicit pointer type
– Java does not have preprocessor, so we cant use #define,
#include and #ifdef statements.
– Java does not include structures, unions data types.
– Java does not include keywords like goto, sizeof and typedef.
– Java adds labeled break and continue statements.
– Java adds many features required for object
oriented programming.
How is Java different from C++…
• C++ language
Features removed in java:
Java doesn’t support pointers to avoid unauthorized access
of memory locations.
Java does not include structures, unions data types.
Java does not support operator over loading.
Preprocessor plays less important role in C++
and so eliminated entirely in java.
Java does not perform automatic type
conversions that
result in loss of precision.
Cont…
Java does not support global variables. Every
method and variable is declared within a class and
forms part of that class.
Java does not allow default arguments.
Java does not support inheritance of multiple super
classes by a sub class (i.e., multiple inheritance). This is
accomplished by using ‘interface’ concept.
It is not possible to declare unsigned integers in java.
In java objects are passed by reference only. In C++
objects may be passed by value or reference.
Cont …
New features added in Java:
Though C++ and java supports Boolean data type, C++ takes
any nonzero value as true and zero as false. True and false in
java are predefined literals that are values for a boolean
expression.
Java has replaced the destructor function with a finalize()
function.
C++ supports exception handling that is similar to java's.
However, in C++ there is no requirement that a thrown
exception be caught.
Comments
Comments in a program are called inline documentation
They should be included to explain the purpose of the
program and describe processing steps
They do not affect how a program works
Java comments can take three forms:
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = 1.1234;
// The Expression
Type promotion in
//Java program to illustrate Type promotion in Expressions
class Test
byte b = 42;
Expressions
System.out.println("result = " + result);
}
} //In this, it will directly convert it into double as a larger data type.
Types of operators:
Java language offers many types of operators. They are,
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Increment/decrement operators
Special operators
Arithmetic Operators in Java:
Arithmetic operators are used to perform mathematical
calculations like addition, subtraction, multiplication, division
and modulus in Java programs.
1 + Addition A+B
2 – Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B
Assignment Operators in Java
In Java programs, values for the variables are assigned using
assignment operators.
S.
Operators Name Example Description
no
logical
1 && (x>5)&&(y<5) It returns true when both conditions are true
AND
Example:
public class SwitchStringExample
{
public static void main(String[] args)
{
String levelString="Expert"; int level=0;
switch(levelString)
Decision and control structure
• In programming languages, loops are used to
execute a set of instructions/functions repeatedly
when some conditions become true.
• There are three types of loops in java.
for loop
while loop
do-while loop
Java For Loop vs While Loop vs Do While Loop
Comparison for loop while loop do while loop
Introduction The Java for loop is a control The Java while loop is a The Java do while loop is a control
flow statement that iterates a control flow statement that flow statement that executes a part
part of the programs multiple executes a part of the of the programs at least once and
times. programs repeatedly on the the further execution depends
basis of given boolean upon the given boolean condition.
condition.
When to use If the number of iteration is If the number of iteration is If the number of iteration is not
fixed, it is recommended to use not fixed, it is recommended fixed and you must have to execute
for loop. to use while loop. the loop at least once, it is
recommended to use the do-while
loop.
Syntax for infinitive for(;;){ //code to be executed } while(true){ //code to be do{ //code to be
loop executed } executed }while(true);
Command-line argument
The java command-line argument is an argument i.e. passed
at the time of running the java program.
It works on elements basis not index. It returns element one by one in the
defined variable.
Syntax:
for(Type var:array){
//code to be executed
}
Example:
int arr[]={12,23,44,56,78};
//Printing array using for-each loop
for(int i:arr){
System.out.println(i);
}
Limitations of For-each loop
Limitations of For-each loop