0% found this document useful (0 votes)
141 views8 pages

Java PDF

Java is an object-oriented programming language developed by Sun Microsystems. It has a variety of data types including primitives, arrays, and classes. Java programs undergo compilation, loading, verification and execution. Control structures include if/else statements, loops like while and for, and switch statements. Java supports object-oriented features like classes, objects, methods, inheritance and polymorphism. Events and applets allow Java programs to interact with users.

Uploaded by

Karthik Nsp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
141 views8 pages

Java PDF

Java is an object-oriented programming language developed by Sun Microsystems. It has a variety of data types including primitives, arrays, and classes. Java programs undergo compilation, loading, verification and execution. Control structures include if/else statements, loops like while and for, and switch statements. Java supports object-oriented features like classes, objects, methods, inheritance and polymorphism. Events and applets allow Java programs to interact with users.

Uploaded by

Karthik Nsp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

What is Java:

Java is a programming language and a platform. Java is a high level, robust, object-oriented
and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year
1995. James Gosling is known as the father of Java. Before Java, its name was Oak.
History of Java:
1. History of Java
2. Java Version History

The history of Java is very interesting. Java was originally designed for interactive
television, but it was too advanced technology for the digital cable television industry at
the time. The history of Java starts with the Green Team.

Basics of a Typical Java Environment :


Java programs normally undergo five phases

1. Edit
Programmer writes program (and stores program on disk)
2. Compile
Compiler creates bytecodes from program
3. Load
Class loader stores bytecodes in memory
4. Verify
Verifier ensures bytecodes do not violate security requirements
5. Execute
Interpreter translates bytecodes into machine language

Operators in Java:
Java provides many types of operators which can be used according to the need. They are
classified based on the functionality they provide. Some of the types are-
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra.
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement
Assignment Operators
Java provides special operators that can be used to combine an arithmetic operation with
an assignment. As you probably know, statements like the following are quite common in
programming:
a = a + 4;
Increment and Decrement
The ++ and the – – are Java’s increment and decrement operators.
// Demonstrate ++.
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
Bitwise Operators
Java defines several bitwise operators that can be applied to the integer types, long, int, short,
char, and byte.

~ Bitwise unary NOT


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Relational Operators
The relational operators determine the relationship that one operand has to the other.
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Logical Operators
The Boolean logical operators shown here operate only on boolean operands.
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to

Assignment Operator
The assignment operator is the single equal sign, =
var = expression;
Conditional Operator
Java includes a special ternary (three-way) operator that can replace certain types of if-then-
else
statements. This operator is the ?.
expression1 ? expression2 : expression3

Control structure:
If statement:

 The if statement was introduced in Chapter 2. It is examined in detail here. The if


statement
 is Java’s conditional branch statement. It can be used to route program execution
through
 two different paths. Here is the general form of the if statement:
syntax:
if (condition) statement1;
else statement2;
Nested if:
Anested if is an if statement that is the target of another if or else.
The if-else-if Ladder:
A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder.
Syntax:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
switch:
The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch
execution to different parts of your code based on the value of an expression. As such, it often
provides a better alternative than a large series of if-else-if statements.
Syntax:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
// statement sequence
break;
default:
// default statement sequence
}
A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
LOOPING STRUCTURE:
While statement:
The while loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression is true. Here is its general form:
Syntax:
while(condition) {
// body of loop
}
The condition can be any Boolean expression.
do-while statement:
As you just saw, if the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all.
Syntax
do {
// body of loop
} while (condition);
FOR loop statement:
The first is the traditional form that has been in use since the original version of Java.
for(initialization; condition; iteration) {
// body
}
DATA TYPE:
The Primitive Types
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
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.
Arrays:
An array is a group of like-typed variables that are referred to by a common name. Arrays of
any type can be created and may have one or more dimensions.
One-Dimensional Arrays
Aone-dimensional array is, essentially, a list of like-typed variables. To create an array, you
first
must create an array variable of the desired type. The general form of a one-dimensional
array declaration is
type var-name[ ];
Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look
and act like regular multidimensional arrays.
int twoD[][] = new int[4][5];
Program:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
This program generates the following output:
01234
56789
10 11 12 13 14
15 16 17 18 19
Constructor:
 Constructor is a special method that gets invoked “automatically” at the time of object
creation.
 Constructor is normally used for initializing objects with default values unless different
values are supplied.
 Constructor has the same name as the class name.
 Constructor cannot return values.
 A class can have more than one constructor as long as they have different signature
(i.e., different input arguments syntax).

public class ClassName {


// Data Fields…
// Constructor
public ClassName()
{
// Method Body Statements initialising Data Fields
}
//Methods to manipulate data fields
}

Method Overloading:
 Constructors all have the same name.
 Methods are distinguished by their signature:
 name
 number of arguments
 type of arguments
 position of arguments
 That means, a class can also have multiple usual methods with the same name.
 Not to confuse with method overriding (coming up), method overloading:

Event Handling:
It can be generated as a consequence of a person interacting with the elements in a graphical user
interface.
Event Classes:
The classes that represent events are at the core of Java’s event handling mechanism.
EventObject(Object src)
The ActionEvent Class:

An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a


menu item is selected.
Applet:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
This applet begins with two import statements. The first imports the Abstract Window Toolkit
(AWT) classes. Applets interact with the user through the AWT, not through the console-based
I/O classes.
 Executing the applet within a Java-compatible web browser.
 Using an applet viewer, such as the standard tool, appletviewer. An applet viewer
executes your applet in a window. This is generally the fastest and easiest way to
test your applet.

You might also like