2. INTRODUCTION TO JAVA
● Java programming language was developed by Sun Microsystems which was
initiated by James Gosling and released in 1995.
● Multiple configurations were built to suit various types of platforms
● For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE, and Java ME
respectively.
● Java is platform independent.
3. JAVA EDITIONS
● J2SE(Java 2 Standard Edition) - to develop client-side standalone applications or
applets.
● J2ME(Java 2 Micro Edition ) - to develop applications for mobile devices such as
cell phones.
● J2EE(Java 2 Enterprise Edition ) - to develop server-side applications such as
Java servlets and Java ServerPages.
4. IMPORTANCE OF JAVA FOR THE INTERNET
★ Java Applets
● Applet is a special type of java program that is embedded in the webpage to
generate the dynamic content and executed by a java compatible web
browser.
● It runs inside the browser and works at client side.
Advantage of Applet
● It works at client side so less response time.
● Secured
● It can be executed by browsers running under many platforms, including
Linux, Windows, Mac Os etc.
5. Drawback of Applet
● Plugin is required at client browser to execute applet
★ Security
● Java achieved this protection by confining an applet to the Java
execution environment and not allowing it access to other parts of the
computer
★ Portability
● Portability refers to the ability to run a program on different machines.
.
6. JAVA VIRTUAL MACHINE (JVM)
● A virtual machine that runs java bytecodes.
● It is a specification that provides runtime environment in which java bytecode
can be executed.
● The JVM performs following main tasks:
❖ Loads code
❖ Verifies code
❖ Executes code
❖ Provides runtime environment
8. JAVA RUNTIME ENVIRONMENT (JRE) cont...
● The Java Runtime Environment (JRE) provides the libraries, the Java Virtual
Machine, and other components to run applets and applications written in the
Java programming language.
● Two key deployment technologies are part of the JRE: Java Plug-in, which
enables applets to run in popular browsers; and Java Web Start, which
deploys standalone applications over a network.
● The JRE does not contain tools and utilities such as compilers or debuggers
for developing applets and applications.
10. JAVA DEVELOPMENT KIT (JDK) cont...
● The JDK is a superset of the JRE, and contains everything that is in the JRE,
plus tools such as the compilers and debuggers necessary for developing
applets and applications.
12. BYTE CODE AND ITS FEATURES Cont...
● Bytecode is a highly optimized set of instructions designed to be executed by
JVM.
● Bytecodes are the machine language of the Java virtual machine.
14. SERVLETS: Java on the Server Side
● A servlet is a small program that executes on the server
● Just as applets dynamically extend the functionality of a web browser,
servlets dynamically extend the functionality of a web server
● Java Servlets are programs that run on a Web or Application server and act
as a middle layer between a requests coming from a Web browser or other
HTTP client and databases or applications on the HTTP server.
● Using Servlets, you can collect input from users through web page forms,
present records from a database or another source, and create web pages
dynamically.
Advantages
● Performance is significantly better.
● Servlets are platform-independent because they are written in Java.
15. SERVLETS Cont...
● Servlets execute within the address space of a Web server. It is not
necessary to create a separate process to handle each client request.
● Java security manager on the server enforces a set of restrictions to protect
the resources on a server machine. So servlets are trusted.
● The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and
RMI mechanisms that you have seen already.
16. JAVA : DIFFERENCES FROM C
● C is a structure oriented language and Java is an object oriented language
● Java does not support an explicit pointer type
● Java does not have preprocessor, so we can’ t use #define, #include and
#ifdef statements
● Java does not include structures, unions and enum data types.
● Java does not include keywords like goto, sizeof and typedef.
● Java adds many features required for object oriented programming
17. JAVA : DIFFERENCES FROM C++
● Java doesn’t support pointers to avoid unauthorized access of memory
locations.
● Java does not include structures, unions and enum data types.
● Java does not support operator overloading.
● Preprocessor is eliminated entirely in java.
● Java does not perform automatic type conversions that result in loss of
precision.
● Java does not support global variables. Every method and variable is
declared within a class and forms part of that class.
18. JAVA : DIFFERENCES FROM C++
● Java does not allow default arguments.
● Java does not support inheritance of multiple super classes by a subclass
(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
19. NEW FEATURES ADDED IN JAVA
● Multithreading, that allows two or more pieces of the same program to
execute concurrently.
● C++ has a set of library functions that use a common header file. But java
replaces it with its own set of API classes.
● It adds packages and interfaces.
● Java supports automatic garbage collection.
● break and continue statements have been enhanced in java to accept labels
as targets.
● The use of unicode characters ensures portability.
● Java has replaced the destructor function with a finalize() function.
20. NEW FEATURES ADDED IN JAVA Cont...
● 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.
● C++ supports exception handling that is similar to java's. However, in C++
there is no requirement that a thrown exception be caught.
CHARACTERISTICS OF JAVA
● Java is the Internet programming language.
● Java enables users to develop and deploy applications on the Internet for
servers, desktop computers, and small hand-held devices.
21. CHARACTERISTICS OF JAVA Cont...
● Java Is Simple
● Java Is Object-Oriented
● Java Is Distributed
● Java Is Interpreted
● Java Is Robust
● Java Is Secure
● Java Is Architecture-Neutral
● Java Is Portable
● Java Is Multithreaded
● Java Is Dynamic
● Java’s High Performance
22. JAVA IS SIMPLE
● Java is partially modeled on C++, but simplified and improved.
● Some people refer to Java as "C++--" because it is like C++ but with
more functionality
● Java is designed to be easy to learn. If you understand the basic
concept of OOP Java, it would be easy to master.
JAVA IS OBJECT ORIENTED
● In Java, everything is an Object. Java can be easily extended since it is
based on the Object model.
23. JAVA IS DISTRIBUTED
● Distributed computing involves several computers working together on a
network.
● Java is designed to make distributed computing easy. Since networking
capability is inherently integrated into Java, writing network programs is
like sending and receiving data to and from a file.
JAVA IS INTERPRETED
● Java bytecode is translated to native machine instructions and is not
stored anywhere.
● You need an interpreter to run Java programs. The programs are
compiled into the Java Virtual Machine code called bytecode.
● The bytecode is machine-independent and can run on any machine that
has a Java interpreter, which is part of the Java Virtual Machine (JVM).
24. JAVA IS ROBUST
● Java makes an effort to eliminate error prone situations by emphasizing
mainly on compile time error checking and runtime checking
● Java compilers can detect many problems that would first show up at
execution time in other languages.
● Java has a runtime exception-handling feature to provide programming
support for robustness.
JAVA IS SECURE
● Java implements several security mechanisms to protect your system
against harm caused by stray programs.
● With Java's secure feature it enables to develop virus-free systems.
Authentication techniques are based on public-key encryption
25. JAVA IS ARCHITECTURE-NEUTRAL
● Write once, run anywhere
● With a Java Virtual Machine (JVM),you can write one program that will
run on any platform.
● Java compiler generates an architecture-neutral object file format, which
makes the compiled code executable on many processors, with the
presence of Java runtime system.
JAVA IS PORTABLE
● Because Java is architecture neutral, Java programs are portable.
● They can be run on any platform without being recompiled.
● Being architecture-neutral and having no implementation dependent
aspects of the specification makes Java portable.
26. JAVA IS MULTITHREADED
● Multithreaded programming is smoothly integrated in Java, whereas in
other languages you have to call procedures specific to the operating
system to enable
● With Java's multithreaded feature it is possible to write programs that
can perform many tasks simultaneously. This design feature allows the
developers to construct interactive applications that can run smoothly.
JAVA IS DYNAMIC
● Java was designed to adapt to an evolving environment.
● There is no need for developers to create, and for users to install, major
new software versions.
● New features can be incorporated transparently as needed.
27. JAVA’S HIGH PERFORMANCE
● Because Java is architecture neutral, Java programs are portable.They
can be run on any platform without being recompiled.
● With the use of Just-In-Time compilers, Java enables high performance.
28. JAVA PROGRAM STRUCTURE
● When we consider a Java program, it can be defined as a collection of
objects that communicate via invoking each other's methods
● Structure of a java program is the standard format released by
Language developer to the Industry programmer.
● Sun Micro System has prescribed the following structure for the java
programmers for developing java application.
30. JAVA PROGRAM STRUCTURE Cont...
● A packages a collection of classes, interfaces and sub-packages.
● Class is keyword used for developing user defined data type and every
java program must start with a concept of class.
● "ClassName" represent a java valid variable name treated as a name of
the class each and every class name in java is treated as user-defined
data type.
● Data member represents either instance or static; they will be selected
based on the name of the class.
● User-defined methods represents either instance or static they are
meant for performing the operations either once or each and every time.
● Each and every java program starts execution from the main() method.
And hence main() method is known as program driver.
31. JAVA PROGRAM STRUCTURE Cont...
● Since main() method of java is not returning any value and hence its return
type must be void.
● Since main() method of java executes only once throughout the java
program execution and hence its nature must be static.
● Since main() method must be accessed by every java programmer and
hence whose access specifier must be public.
● main() method of java must take array of objects of String.
● Block of statements represents set of executable statements which are
in term calling user-defined methods are containing business-logic.
● The file naming conversion in the java programming is that whichever
class is containing main() method, that class name must be given as a
file name with an extension .java.
32. FIRST JAVA PROGRAM
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public class MyFirstJavaProgram {
public static void main(String [] args) {
System.out.println("Hello World"); // prints Hello World
}
}
33. ● Open notepad and add the code as above.
● Save the file as: MyFirstJavaProgram.java.
● For most computer languages, the name of the file that holds the source
code to a program is immaterial.
● However, this is not the case with Java. The first thing that you must learn
about Java is that the name you give to a source file is very important.
● For this example, the name of the source file should be
MyFirstJavaProgram.java.
ENTERING THE PROGRAM
34. ● Why ? In Java, a source file is officially called a compilation unit. It is a text
file that contains one or more class definitions. The Java compiler requires
that a source file use the .java filename extension.
● In Java, all code must reside inside a class.
● By convention, the name of that class should match the name of the file that
holds the program.
● You should also make sure that the capitalization of the filename matches
the class name.
ENTERING THE PROGRAM
35. COMPILING THE PROGRAM
● Open a command prompt window and go to the directory where you saved
the class. Assume it's C:.
● Type 'javac MyFirstJavaProgram.java' and press enter to compile your code.
If there are no errors in your code, the command prompt will take you to the
next line (Assumption : The path variable is set).
● The javac compiler creates a file called Example.class that contains the
bytecode version of the program.
● Now, type ' java MyFirstJavaProgram ' to run your program.
● You will be able to see ' Hello World ' printed on the window.
C:> javac MyFirstJavaProgram.java
C:> java MyFirstJavaProgram
36. ● The Java bytecode is the intermediate representation of your program
that contains instructions the Java Virtual Machine will execute.
● Thus, the output of javac is not code that can be directly executed
● To actually run the program, you must use the Java application launcher,
called java.
● To do so, pass the class name Example as a command-line argument,
as shown here:
■ C:>java Example
● The descriptions that follow use the standard Java SE 6 Development Kit
(JDK 6), which is available from Sun Microsystems. If you are using a
different Java development environment, then you may need to follow a
different procedure for compiling and executing Java programs.
COMPILING THE PROGRAM Cont...
37. A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
The program begins with the following lines:
/* This is my first java program.
This will print 'Hello World' as the output
*/
This is a comment
COMMENT
● Java lets you enter a remark into a program’s source file.
● The contents of a comment are ignored by the compiler. Instead, a comment
describes or explains the operation of the program to anyone who is reading
its source code.
● In this case, the comment describes the program and reminds you that the
source file should be called Example.java.
38. A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
COMMENT
● In real applications, comments generally explain how some part of the
program works or what a specific feature does.
● Java supports three styles of comments.
1. Multiline Comment :
● This type of comment must begin with /* and end with */.
● Anything between these two comment symbols is ignored by the compiler.
● As the name suggests, a multiline comment may be several lines long
39. A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
2. Single-line comment
● // Your program begins with a call to main().
● A single-line comment begins with a // and ends at the end of the line.
● As a general rule, programmers use multiline comments for longer remarks
and single-line comments for brief, line-by-line descriptions.
3. Documentation comment
/** documentation */
● This is a documentation comment and in general its called doc comment.
40. ● The next line of code in the program is shown here:
■ public class MyFirstJavaProgram {
● This line uses the keyword class to declare that a new class is being defined.
MyFirstJavaProgram is an identifier that is the name of the class.
● The entire class definition, including all of its members, will be between the
opening curly brace ({) and the closing curly brace (}).
● The next line of code is shown here:
■ public static void main(String args[]) {
● This line begins the main( ) method. This is the line at which the program will
begin executing. All Java applications begin execution by calling main( ).
A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
41. ● The public keyword is an access specifier, which allows the programmer to
control the visibility of class members.
● When a class member is preceded by public, then that member may be
accessed by code outside the class in which it is declared.
● In this case, main( ) must be declared as public, since it must be called by
code outside of its class when the program is started.
● The keyword static allows main( ) to be called without having to instantiate a
particular instance of the class. This is necessary since main( ) is called by
the Java Virtual Machine before any objects are made.sstatic is a reserved
keyword which means that a method is accessible and usable even though
no objects of the class exist
A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
42. ● The keyword void simply tells the compiler that main( ) does not return a
value
● main( ) is the method called when a Java application begins.
● Java is case-sensitive. Thus, Main is different from main.
● The Java compiler will compile classes that do not contain a main( ) method.
But java has no way to run these classes.
● Any information that you need to pass to a method is received by variables
specified within the set of parentheses that follow the name of the method.
These variables are called parameters.
● In main( ), there is only one parameter, albeit a complicated one.
● String args[ ] declares a parameter named args, which is an array of
instances of the class String.
A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
43. ● Arrays are collections of similar objects. Objects of type String store
character strings
● A complex program will have dozens of classes, only one of which will need
to have a main( ) method to get things started.
● When you begin creating applets—Java programs that are embedded in web
browsers—you won’t use main( ) at all, since the web browser uses a
different means of starting the execution of applets.
● System: It is the name of Java utility class.
● out:It is an object which belongs to System class.
● println: It is utility method name which is used to send any String to the
console
A CLOSER LOOK AT THE FIRST SAMPLE PROGRAM
Cont...
44. BASIC SYNTAX
● Case Sensitivity - Java is case sensitive, which means identifier Hello and
hello would have different meaning in Java.
● Class Names - For all class names the first letter should be in Upper Case. If
several words are used to form a name of the class, each inner word's first
letter should be in Upper Case.
■ Example: class MyFirstJavaClass
● Method Names - All method names should start with a Lower Case letter. If
several words are used to form the name of the method, then each inner
word's first letter should be in Upper Case.
■ Example: public void myMethodName()
● Program File Name - Name of the program file should exactly match the
class name.
45. BASIC SYNTAX Cont...
● When saving the file, you should save it using the class name (Remember
Java is case sensitive) and append '.java' to the end of the name (if the file
name and the class name do not match, your program will not compile).
■ Example: Assume 'MyFirstJavaProgram' is the class name. Then
the file should be saved as 'MyFirstJavaProgram.java'
● public static void main(String args[]) - Java program processing starts
from the main() method which is a mandatory part of every Java program.
46. JAVA IDENTIFIERS
● All Java components require names. Names used for classes, variables, and
methods are called identifiers.
● In Java, there are several points to remember about identifiers. They are as
follows:
● All identifiers should begin with a letter (A to Z or a to z), currency character
($) or an underscore (_).
● After the first character, identifiers can have any combination of characters.
● A key word cannot be used as an identifier.
● Most importantly, identifiers are case sensitive.
● Examples of legal identifiers: age, $salary, _value, __1_value.
● Examples of illegal identifiers: 123abc, -salary.
47. JAVA VARIABLES
● All variables must be declared before they can be used.
● Following is the basic form of a variable declaration −
● data type variable [ = value][, variable [ = value] ...] ;
● Here data type is one of Java's datatypes and variable is the name of the
variable. To declare more than one variable of the specified type, you can
use a comma-separated list.
■ int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'
48. JAVA VARIABLES Cont...
● Following are the types of variables in Java:
1. Local Variables-A variable which is declared inside the method is called
local variable.
2. Instance Variables (Non-static Variables)-A variable which is declared
inside the class but outside the method, is called instance variable . It is
not declared as static.
3. Class Variables (Static Variables)-A variable that is declared as static is
called static variable. It cannot be local.
Example
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
49. JAVA VARIABLES Cont...
Local Variables
● Local variables are declared in methods, constructors, or blocks.
● Local variables are created when the method, constructor or block is entered
and the variable will be destroyed once it exits the method, constructor, or
block.
● Access modifiers cannot be used for local variables.
● Local variables are visible only within the declared method, constructor, or
block.
● There is no default value for local variables, so local variables should be
declared and an initial value should be assigned before the first use.
50. JAVA VARIABLES Cont...
Local Variables Example
public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]) {
Test test = new Test();
test.pupAge();
}
}
Here, age is a local variable. This is defined inside pupAge() method and its scope is
limited to only this method.
51. JAVA VARIABLES Cont...
Instance Variables
● Instance variables are declared in a class, but outside a method, constructor or
any block.
● Instance variables are created when an object is created with the use of the
keyword 'new' and destroyed when the object is destroyed.
● Instance variables hold values that must be referenced by more than one
method, constructor or block, or essential parts of an object's state that must be
present throughout the class.
● Instance variables can be declared in class level before or after use
● Instance variables can be accessed directly by calling the variable name inside
the class.
● Access modifiers can be given for instance variables.
52. JAVA VARIABLES Cont...
Instance Variables
● The instance variables are visible for all methods, constructors and block in
the class. Normally, it is recommended to make these variables private
(access level).
● Instance variables have default values. For numbers, the default value is 0,
for Booleans it is false, and for object references it is null. Values can be
assigned during the declaration or within the constructor.
53. JAVA VARIABLES Cont...
Instance Variables - Example
import java.io.*;
public class Employee {
// this instance variable is visible for any child class.
public String name;
// salary variable is visible in Employee class only.
private double salary;
// The name variable is assigned in the constructor.
public Employee (String empName) {
name = empName;
}
54. JAVA VARIABLES -Instance Variables - Example Cont...
// The salary variable is assigned a value.
public void setSalary(double empSal) {
salary = empSal;
}
// This method prints the employee details.
public void printEmp() {
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]) {
Employee empOne = new Employee("Ransika");
empOne.setSalary(1000);
empOne.printEmp();
}
}
55. JAVA VARIABLES Cont...
Class/Static Variables
● Class variables also known as static variables are declared with the static
keyword in a class, but outside a method, constructor or a block.
● There would only be one copy of each class variable per class, regardless of
how many objects are created from it.
● Static variables are rarely used other than being declared as constants.
● Static variables are created when the program starts and destroyed when the
program stops.
56. JAVA VARIABLES - Class/Static Variables cont...
● Visibility is similar to instance variables. However, most static variables are
declared public since they must be available for users of the class.
● Default values are same as instance variables. For numbers, the default
value is 0; for Booleans, it is false; and for object references, it is null.
Values can be assigned during the declaration or within the constructor.
Additionally, values can be assigned in special static initializer blocks.
● Static variables can be accessed by calling with the class name
ClassName.VariableName.
● When declaring class variables as public static final, then variable names
(constants) are all in upper case. If the static variables are not public and
final, the naming syntax is the same as instance and local variables.
57. JAVA VARIABLES - Class/Static Variables cont...
Example
import java.io.*;
public class Employee {
// salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";
public static void main(String args[]) {
salary = 1000;
System.out.println(DEPARTMENT + "average salary:" + salary);
}
}
58. COMMAND LINE ARGUMENTS
● The command line argument is the argument passed to a program at the time when
you run it. To access the command-line argument inside a java program is quite easy,
they are stored as string in String array passed to the args parameter of main()
method.
Example1 :
class CommandLine
{
public static void main(String[] args)
{
for(int i=0;i< args.length;i++)
{
System.out.println(args[i]);
}
}
}
59. COMMAND LINE ARGUMENTS Cont...
Example1 :
class CommandLine
{
public static void main(String[] args)
{
system.out.println(args[0]);
system.out.println(args[1]);
}
}
60. DATA TYPES
Data types represent the different values to be stored in the variable. In java,
there are two types of data types:
● Primitive data types
● Non-primitive data types
61. PRIMITIVE TYPES
Integers : This group includes byte, short, int, and long, which are for whole-valued
signed numbers.
Floating-point numbers: This group includes float and double, which represent
numbers with fractional precision
Characters: This group includes char, which represents symbols in a character set,
like letters and numbers.
Boolean: This group includes boolean, which is a special type for representing
true/false values.
62. INTEGERS
● Java defines four integer types: byte, short, int, and long. All of these are signed,
positive and negative values.
● Java does not support unsigned, positive-only integers.
● byte :The smallest integer type is byte.This is a signed 8-bit type
■ Eg: byte b, c;
● short : short is a signed 16-bit typeIt is probably the least-used Java type.
■ Eg : short s;
● int The most commonly used integer type is int.It is a signed 32-bit type
■ Eg : int a;
● long : long is a signed 64-bit type and is useful for those occasions where an int
type is not large enough to hold the desired value. The range of a long is quite
large.
■ Eg: long lightspeed;
63. FLOATING -POINT TYPES
● Floating-point numbers, also known as real numbers, are used when evaluating
expressions that require fractional precision
● float The type float specifies a single-precision value that uses 32 bits of storage
■ Eg : float hightemp, lowtemp;
● double : Double precision, as denoted by the double keyword, uses 64 bits to
store a value
■ Eg: double pi;
BOOLEANS
● boolean :Java has a primitive type, called boolean, for logical values. It can have
only one of two possible values, true or false.
■ Eg :boolean b;
64. CHARATERS
● In Java, the data type used to store characters is char.
● char in Java is not the same as char in C or C++. In C/C++, char is 8 bits
wide. This is not the case in Java. Instead, Java uses Unicode to represent
characters. Unicode defines a fully international character set that can
represent all of the characters found in all human languages.
● Java char is a 16-bit type
Example:
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
65. JAVA-BASIC OPERATORS
● Java provides a rich set of operators to manipulate variables. We can divide all
the Java operators into the following groups:
■ Arithmetic Operators
■ Relational Operators
■ Bitwise Operators
■ Logical Operators
■ Assignment Operators
■ Misc Operators
66. ARITHMETIC OPERATORS
● + ( Addition ) Adds values on either side of the operator
● - ( Subtraction ) Subtracts right-hand operand from left-hand operand
● * ( Multiplication ) Multiplies values on either side of the operator
● / (Division) Divides left-hand operand by right-hand operand
● % (Modulus) Divides left-hand operand by right-hand operand and returns
remainder
● ++ (Increment) Increases the value of operand by 1
● -- ( Decrement ) Decreases the value of operand by 1
67. RELATIONAL OPERATORS
● == (equal to) Checks if the values of two operands are equal or not, if yes then
condition becomes true.
● != (not equal to) Checks if the values of two operands are equal or not, if values
are not equal then condition becomes true.
● > (greater than) Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
● < (less than) Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
● >= (greater than or equal to) Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then condition becomes true.
● <= (less than or equal to) Checks if the value of left operand is less than or equal
to the value of right operand, if yes then condition becomes true.
68. BITWISE OPEARATORS
● & (bitwise and) Binary AND Operator copies a bit to the result if it exists in both
operands
● | (bitwise or) Binary OR Operator copies a bit if it exists in either operand.
● ^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one operand but
not both
● ~ (bitwise compliment) Binary Ones Complement Operator is unary and has the
effect of 'flipping' bits.
● << (left shift) Binary Left Shift Operator. The left operands value is moved left by
the number of bits specified by the right operand
● >> (right shift) Binary Right Shift Operator. The left operands value is moved right
by the number of bits specified by the right operand.
● >>> (zero fill right shift) Shift right zero fill operator. The left operands value is
moved right by the number of bits specified by the right operand and shifted
values are filled up with zeros.
69. LOGICAL OPERATORS
● && (logical and) Called Logical AND operator. If both the operands are non-zero,
then the condition becomes true.
● || (logical or) Called Logical OR Operator. If any of the two operands are non-zero,
then the condition becomes true.
● ! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its
operand. If a condition is true then Logical NOT operator will make false.
ASSIGNMENT OPERATORS
● = Simple assignment operator. Assigns values from right side operands to left
side operand.
● += Add AND assignment operator. It adds right operand to the left operand and
assign the result to left operand.
■ Example: C += A is equivalent to C = C + A
70. ASSIGNMENT OPERATORS Cont...
● -= Subtract AND assignment operator. It subtracts right operand from the left
operand and assign the result to left operand.
● *= Multiply AND assignment operator. It multiplies right operand with the left
operand and assign the result to left operand.
● /= Divide AND assignment operator. It divides left operand with the right operand
and assign the result to left operand.
● %=Modulus AND assignment operator. It takes modulus using two operands and
assign the result to left operand.
● <<= Left shift AND assignment operator.
● >>= Right shift AND assignment operator
● &= Bitwise AND assignment operator.
● ^= bitwise exclusive OR and assignment operator
● |= bitwise inclusive OR and assignment operator.
71. MISCELLANEOUS OPERATORS
● Conditional Operator ( ? : ) Conditional operator is also known as the ternary
operator.
■ variable x = (expression) ? value if true : value if false
■ Eg: b = (a == 1) ? 20: 30;
● instanceof Operator This operator is used only for object reference variables.
The operator checks whether the object is of a particular type (class type or
interface type).
■ ( Object reference variable ) instanceof (class/interface type)
72. CONTROL STATEMENTS
● Control statements can be put into the following categories: selection, iteration,
and jump
SELECTION STATEMENTS :
● Selection statements allow your program to choose different paths of execution
based upon the outcome of an expression or the state of a variable
● Java supports two selection statements: if and switch.
If
if (condition) statement1;
else statement2;
Eg : int a, b;
// ...
if(a < b) a = 0;
else b = 0;
73. Nested ifs
● A nested if is an if statement that is the target of another if or else
Eg: if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
if-else-if Ladder
● A common programming construct that is based upon a sequence of nested ifs is
the if-else-if ladder
75. Switch
● The switch statement is Java’s multiway branch statement.
switch (expression) {
case value1:
// statement sequence
Break;
case value2:
// statement sequence
Break;
…
case valueN:
// statement sequence
Break;
Default:
// default statement sequence
}
76. ITERATION STATEMENTS
● Java’s iteration statements are for, while, and do-while.
● These statements create loops.
while
while(condition) {
// body of loop
}
do-while
do {
// body of loop
} while(condition);
78. break
1. Using break to Exit a Loop
// Using break to exit a loop.
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
79. Break Cont...
2. Using break as a Form of Goto
The general form of the labeled break statement is break label;
Eg: class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
80. continue
● A continue statement causes control to be transferred directly to the conditional
expression that controls the loop.
Eg: class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
}
}
}
81. return
● The return statement is used to explicitly return from a method.
Eg : class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}