JDK I: Verview Nstallation Nstructions Onfiguration
JDK I: Verview Nstallation Nstructions Onfiguration
JDK I: Verview Nstallation Nstructions Onfiguration
TABLE OF CONTENTS
JDK INSTALLATION
OVERVIEW
INSTALLATION INSTRUCTIONS
CONFIGURATION
MODULE 1 - EXPLAINING JAVA TECHNOLOGY
Brief History of Java
What is Java?
List the three Java technology product groups
Java Development Kit
How does JVM work?
Features of Java
MODULE 2 - ANALYZING JAVA TECHNOLOGY PROGRAM
Java Source File Structure
Java Keywords
Identifiers
Literals
MODULE 3 - DECLARING, INITIALIZING, AND USING VARIABLES
Define the syntax for a variable
Java programming language data types
Primitive Data Type
Reference Data Type
Declare, initialize, and use variables
Primitive Type Declaration
Reference Data Type
Scope of the Variables
MODULE 4 OPERATORS AND ASSIGNMENTS
Arithmetic operators
Object operators
Integer Operators
Floating Point Operators
Boolean Operators
Conditional Boolean Operators
Assignment operators
String operator
Evaluation order Operator Precedence
Expressions
Casting
MODULE 5 FLOW CONTROLS AND ARRAYS
Selection
Iteration
Jump
Array Declaration, Construction, Initialization, Manipulation
By SRK
Page 1 of 35
JDK Installation
Overview
JDK includes tools useful for developing and testing programs written in the Java programming
language and running on the Java platform.
Installation Instructions
1. In your local drive go to D:\WebServices\CoreJava folder.
2. Launch the installer(jdk-7u67-windows-x64.exe). A series of installation windows will be
displayed and you have the option to customize your installation. Customizing your installation
includes choosing the directory where you want to install.
3. Change the default installation directory for the JDK to D:\WebServices\CoreJava \ jdk1.7.0_60
\. Accept the default components.
4. Change the default directory for the JRE to the directory D:\WebServices\CoreJava\
jdk1.7.0_60 \. Accept the default components.
Configuration
After successful installation, add the additional environment variable JAVA_HOME on the list of
system variables. Make sure to indicate the path directory where JDK was installed.
1.
By SRK
Page 2 of 35
1. Select the Advanced tab and click the Environment Variables button.
3.
Click the New button on System Variables. Specify variable name (JAVA_HOME) and variable
value which is the directory of your JDK (Ex. D:\WebServices\CoreJava \ jdk1.7.0_60).
By SRK
Page 3 of 35
In 1990, Sun Microsystems began an internal project known as the Green Project to work on a
new technology.
In 1992, the Green Project was spun off and its interest directed toward building highly
interactive devices for the cable TV industry. This failed to materialize.
In 1994, the focus of the original team was re-targeted, this time to the use of Internet technology.
A small web browser called HotJava was written. Oak was renamed to Java after learning that
Oak had already been trademarked.
In 1995, Java was first publicly released.
James Gosling is generally credited as the inventor of the Java programming language
He was the first designer of Java and implemented its original compiler and virtual machine
JDK(Java Development Kit) 1.0, code named Oak and released on January 23, 1996.
JDK 1.1, released on February 19, 1997.
JDK 1.2, code named Playground and released on December 8, 1998.
JDK 1.3, code named Kestrel and released on May 8, 2000.
JDK 1.4, code named Merlin and released on February 6, 2002 (first release under JCP).
JDK 1.5, code named Tiger and released on September 30, 2004.
JDK 1.6, code named Mustang and released on December 11, 2006.
JDK 1.7, code named Dolphin and released on July 28, 2011, the most widely used version.
JDK 1.8, was released on 18 March 2014. The code name culture is dropped with Java 8 and so
no official code name going forward from Java 8., the latest version.
What is Java?
A multi-platform, network-centric, object-oriented programming language
Multi-platform
By SRK
Page 4 of 35
JDK
JRE
MyProgram.java
Java API
Java Virtual Machine
Hardware - Based Platform
By SRK
Page 5 of 35
Source Code
public class HelloWorld {
public static void main(String args[ ]) {
System.out.println(Hello World!);
}
}
Compiler
Class File
Bytecodes
Features of Java
Network-centric
Can work with resources across a network and multi-tier architectures
Object-oriented
A Java program models a set of objects interacting with each other
Robust
By SRK
Page 6 of 35
Multithreaded
A Java application can run several different processes called threads simultaneously
Security
Can download remote code over a network and run it in a secure environment
Security levels and restrictions are highly configurable
Declaration order
1. Package declaration
Used to organize a collection of
related classes.
2. Import statement
Used to reference classes and
declared in other packages.
3. Class declaration
A Java source file can have
several classes but only one
public class is allowed.
Comments
1. Single Line Comment
// insert comments here
2. Block Comment
/*
* insert comments here
*/
3. Documentation Comment
/**
* insert documentation
*/
White spaces
Tabs
and spaces are ignored by
By
SRK
the compiler. Used to improve
readability of code.
/*
* Created on Dec 10, 2014
* First Java Program
*/
package com.cj.sample;
import java.util.*;
/**
* @author skummitha
*/
public class JavaMain
{
public static void main(String[]
args) {
// print a message
System.out.println("Welcome to
Java!");
}
}
class Extra {
/*
* class body
*/
}
Page 7 of 35
Class
Braces
By SRK
/*
* Created on Dec 10, 2014
* First Java Program
*/
package com.cj.sample;
import java.util.*;
/**
* @author Skummitha
*/
public class JavaMain
{
public static void main(String[]
args) {
// print a message
System.out.println("Welcome to
Java!");
}
}
class Extra {
/*
* class body
*/
}
Page 8 of 35
Class
Braces
/*
* Created on Dec 10, 2014
* First Java Program
*/
package com.cj.sample;
import java.util.*;
/**
* @author Skummitha
*/
public class JavaMain
{
public static void main(String[]
args) {
// print a message
System.out.println("Welcome to
Java!");
}
}
class Extra {
/*
* class body
*/
}
main() method
String args[]
Terminating Character
Semicolon (;) is the terminating
character for any java statement.
default
if
package
synchronized
assert
do
implements
private
this
boolean
double
import
protected
throw
break
else
instanceof
public
throws
byte
extends
int
return
transient
case
false
interface
short
true
catch
final
long
static
try
char
finally
native
strictfp
void
class
float
new
super
volatile
continue
for
null
switch
while
const
By SRK
35
goto
Page 10 of
Identifiers
Incorrect
3strikes
Write&Print
switch
Correct
strikes3
Write_Print
Switch
Literals
A literal is a representation of a value of a particular type
Type
By SRK
35
boolean
true / false
character
a \uFFFF' \777
Integer
floating-point
object
escape sequences
test null
\n \t \b \f \r \ \ \\
Page 11 of
A variable is a named storage location used to represent data that can be changed while the
program is running
A data type determines the values that a variable can contain and the operations that can be
performed on it
Categories of data types:
Type
boolean
Bits
Lowest Value
Highest Value
(n/a) false
true
char
16
'\u0000' [0]
'\uffff' [216-1]
byte
-128 [-27]
+127 [27-1]
short
16
-32,768 [-215]
+32,767 [215-1]
int
32
-2,147,483,648 [-231]
+2,147,483,647 [231-1]
long
64
float
32
1.40129846432481707e-45
3.40282346638528860e+38
double
64
4.94065645841246544e-324
1.79769313486231570e+308
By SRK
35
1. Class
2. Interface
Page 12 of
age =
21;
age;
age
type
Identifier name
17
Initialization / Assignment
age
Identifier name
17;
Value
Stack
By SRK
35
Page 13 of
myCar;
myCar
Identifier
name
myCar
reference
The heap
Bumble Bee
Identifier
name
Car object
}
}
By SRK
35
Page 14 of
Arithmetic operators
Object operators
Integer Operators
Unary operators
Binary operators
Relational operators
Boolean Operators
Unary operators
Binary operators
Bitwise operators
Shift operators
Relational operators
Logical operator
Conditional operator
Assignment operators
String operator
Expressions
Primitive Casting
Arithmetic Operators
Arithmetic operators are used for basic mathematical operations
+ Add
By SRK
35
Subtract
Multiply
Divide
% Modulo, remainder
Page 15 of
Sample code:
Sample Output :
Object Operators
Testing Object Equality
It is a source of great confusion to novice programmers that Java has two ways of thinking about the
equality of objects. When used with object references, the == operator returns true only if both
references are to the same object. This is illustrated in the following code fragment in which we create
and compare some Integer object references:
1. Integer x1 = new Integer( 5 );
2. Integer x2 = x1;
3. Integer x3 = new Integer( 5 );
4. if( x1 == x2 ) System.out.println("x1 eq x2" );
5. if( x2 == x3 ) System.out.println("x2 eq x3" );
Executing this code will print only "x1 eq x2" because both variables refer to the same object. To test for
equality of content, you have to use a method that can compare the content of the objects
The equals Method
In the Java standard library classes, the method that compares content is always named equals and
takes an Object reference as input. For example, the equals() method of the Integer class works like this:
1. public boolean equals(Object obj)
2. {
3.
if( obj == null ) return false;
4.
if( ! ( obj instanceof Integer ) ) return false;
5.
return this.value == ((Integer) obj).intValue();
6. }
Note that the equals() method does not even look at the value of the other object until it has been
determined that the other object reference is not null and that it refers to an Integer object.
The equals() method in the Object class returns true only if
this == obj
Therefore, in the absence of an overriding equals method, the == operator and equals methods inherited
from Object are equivalent.
By SRK
35
Page 16 of
Integer Operators
Unary Operators
Positive sign
Negative sign
class IncDec{
public static void main(String args[]){
int x = 10, y = 20;
System.out.println ("\n Out put...");
System.out.println("
x = " + x );
System.out.println("
y = " + y );
System.out.println("
++x = " + (++x) );
System.out.println("
y++ = " + (y++) );
System.out.println("
x = " + x );
System.out.println("
y = " + y );
System.out.println("
--x = " + (--x) );
System.out.println("
y-- = " + (y--) );
System.out.println("
x = " + x );
System.out.println("
y = " + y );
// Negation Operation
System.out.println("\n\n Negation Operation ");
System.out.println("
x = " + x );
y = -x;
System.out.println("
-x = " + y );
// Bitwise Complement Operation
System.out.println("\n\n Bitwise Complement Operation ");
System.out.println("
x = " + x );
y = ~x;
System.out.println("
~x = " + y );
}
}
By SRK
35
C:\>javac IncDec.java
C:\>java IncDec
Out put...
x = 10
y = 20
++x = 11
y++ = 20
x = 11
y = 21
--x = 10
y-- = 21
x = 10
y = 20
Negation Operation
x = 10
-x = -10
Bitwise Complement Operation
x = 10
~x = -11
Description
Bitwise AND
Bitwise OR
Bitwise XOR
Bitwise Left shift
Bitwise Right shift
Zero-fill right shift
Operator
&
|
^ Page 17 of
<<
>>
>>>
class BasicBinOper{
public static void main(String args[]){
int a = 15, b =16;
int x=12, y = -13;
System.out.println ("\n Out put...");
// Bitwise Operators
System.out.println(" \n\n Bitwise Operation ");
System.out.println("
a&b = " + (a&b) );
System.out.println("
a|b = " + (a|b) );
System.out.println("
a^b = " + (a^b) );
C:\>javac BasicBinOper.java
C:\>java BasicBinOper
Out put...
Bitwise Operation
a&b = 0
a|b = 31
a^b = 31
Out put: applying shift operators...
System.out.println("\n Out put: applying shift operators..."); -----------------------------------System.out.println("--------------------------------------------"); ...Left Shift Operator...
x<<2 = 48
System.out.println("...Left Shift Operator...");
//
//
//
//
//
//
//
y<<2 = -52
-----------------------------------...Right Shift Operator...
x>>2 = 3
y>>2 = -4
-----------------------------------...Zero-fill Right Shift Operator...
x>>>2 = 3
y>>>2 = 1073741820
//
//
//
}
Relational Integer Operator
By SRK
35
Description
Operator
Less than
<
Greater than
>
Page
18
of
Less than or equal to
<=
Greater than or equal to
>=
Equal to
==
Not equal to
!=
class RelOperators
{
public static void main(String args[])
{
int x=5, y = 8;
System.out.println("\n Output : Relational operators...");
System.out.println("--------------------------------------------");
System.out.println("x value is = " + x );
System.out.println("y value is = " + y );
System.out.println("--------------------------------------------");
System.out.println("");
System.out.println("x Less than y : ( x < y ) : " + (x<y));
System.out.println("");
System.out.println("x Greater than y : ( x > y ) : " + (x>y));
System.out.println("");
System.out.println("--------------------------------------------");
System.out.println("");
System.out.println("x less than/equal to y : " + (x<=y));
System.out.println("");
System.out.println("x greater than/equal to y : " + (x>=y));
System.out.println("");
System.out.println("--------------------------------------------");
System.out.println("");
System.out.println("x Equal to y - ( x == y) : " + (x == y));
System.out.println("");
System.out.println("x Not Equal to y - ( x != y):"+(x != y));
}
}
By SRK
35
C:\>javac RelOperators.java
C:\>java RelOperators
Output : Relational operators...
-------------------------------------x value is = 5
y value is = 8
-------------------------------------x Less than y : ( x < y ) : true
x Greater than y : ( x > y ) : false
-------------------------------------x less than/equal to y : true
x greater than/equal to y : false
-------------------------------------x Equal to y - ( x == y) : false
x Not Equal to y - ( x != y):true
Page 19 of
Unary
Binary
Relational
By SRK
35
Description
Increment
Decrement
Negation
Operator
++
-Page 20 of
Description
Addition
Subtraction
Multiplication
Division
Modulus
Operator
+
*
/
%
Boolean Operators
Boolean operators act on boolean type operands and return a boolean value.
Logical operators are used to compare boolean expressions
! inverts a boolean value
Operator
Description
&, | evaluate both operands
&&, || evaluate operands conditionally
!
NOT
By SRK
35
&
AND
OR
XOR
&&
Short-circuit AND
||
Short-circuit OR
Page 21 of
Similar to the Boolean operators, but with an added ability to short-circuit part of the process,
using a couple of mathematical rules:
o
If the left operand of an && operation is false, the result is automatically false, and the right
operand is not evaluated
boolean a = false;
If the left operand of an || operation is true, the result is automatically true, and the right
operand is not evaluated
boolean b = true;
Boolean Complement ( ! ):
o
The ternary operator ( ? : ) provides a handy way to code simple if-else() statements in a single
expression, it is also known as the conditional operator.
If condition is true, then exp1 is returned as the result of operation.
If condition is false, then exp2 is returned as the result of operation.
Can be nested to accommodate chain of conditions.
By SRK
35
Page 22 of
Assignment Operators
Assignment operators are used to set the value of a variable
String Operators
Assign
+=
-=
*=
/=
|=
OR and assign
^=
2
()
-/
>>>
>=
==
3
[]
~
%
<<
<
!=
<=
Page 23 of
8
9
10
11
12
13
14
Bitwise
Short Circuit
Conditional or Iterator
Assignment
&
^
|
&&
||
?:
=
+=
-=
*=
/=
Expressions
You already know about variables, literals and operators. To do something meaningful and useful we
should be able to generate expressions using these. Operators enable you to perform computation or
evaluation on data objects like variables and literals. Operators applied to variables and literals form
expressions.
Ex :
int x, y;
X = 10;
Y = 5 + 2 * x;
In this expression x and y are variables, 5 and 2 are literals and =, + and * are operators.
When these two conditions are met, a widening conversion takes place. Therefore, the numeric types,
including integer and floating-point types, are compatible with each other. However, the numeric types
are not compatible with char or boolean.
Java performs automatic type conversions when storing a literal integer constant into variables of type
byte, short, or long.
Casting Incompatible Types
By SRK
35
Page 24 of
(target-type) value
Here, target-type specifies the desired type to convert the specified value to.
For example, what if you want to assign an int value to a byte variable? This conversion will not be
performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes
called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the
target-type.
Ex :
int a;
byte b;
.
b = (byte) a;
A different type of conversion will occur when a floating point value is assigned to an integer type :
truncation. As you know, integers do not have fractional components. Thus, when a floating point value is
assigned to an integer type variable, the fractional component is lost.
Ex :
if the value 7.23 is assigned to an integer, the resulting value will simply be 7. the 0.23 will have
been truncated.
Ex :
The return type of function f2() may be a character. The return value of f2() has to go as an
argument to another function f1(), which accepts only the integer argument. The solution here is
to type cast the return value of f2() to integer and then use it as argument for function f1().
f1( (int) f2() );
By SRK
35
Page 25 of
Conversion Type
implicit widening conversion
implicit widening conversion
implicit widening conversion
implicit widening conversion
implicit widening conversion (if target is broader )
implicit widening conversion (if formal parameter is broader)
none
none
none
explicit casting (narrowing or widening conversion)
By SRK
35
Page 26 of
Flow Controls
A programming language uses control statements to cause the flow of execution to advance and branch
based on changes to the state of a program.
Javas program control statements can be put into the following categories
Selection
Iteration
jump
Statement label
Selection -
statements allow your program to choose different paths of execution based upon the
outcome of an expression or the state of a variable.
By SRK
35
if() statement
o Nested ifs
o The if-else-if Ladder
Page 27 of
Iteration -
switch() statement
o Nested switch statements
Jump -
for() statement
while() statement
do-while() statement
break statement
continue statement
return statement
Perform statements in
the order they are written
Selection
Iteration
if-else() Statement
By SRK
35
Page 28 of
switch() Statement
for() Loop
By SRK
35
Page 29 of
while() Loop
while() performs statements repeatedly while condition remains true
do-while() Loop
do-while() performs statements repeatedly (at least once) while condition remains true
By SRK
35
Page 30 of
break Statement
break exits loops and switch() statements
continue Statement
continue is used inside loops to start a new iteration
By SRK
35
Page 31 of
return Statement
return branching statement is used to exit from the current method.
Two forms:
return <value>;
return;
Statement label
By SRK
35
Page 32 of
Arrays
What is an Array?
By SRK
35
Page 33 of
By SRK
35
Page 34 of
By SRK
35
Page 35 of