http://kerneltraining.com/core-java/
Learn the basics of Java and gain practical experience that is required to begin your career in java programming. Kernel Training has designed classroom and online course to upgrade your knowledge and skills in core Java.
Course Curriculum:
Introduction to Java
JDK and practical applications
Java Basic Data Types and Operators
Programming Constructs, Classes and Methods, constructor and Finalizer
Polymorphism
Inheritance
Method overriding and abstract classes
Packages and Interfaces
Strings and Arrays
Enums
2. What Is Java?
Java
Java is not just a programming language but it is a complete
platform for object oriented programming.
JRE
Java standard class libraries which provide Application
Programming Interface and JVM together form JRE (Java Runtime
Environment).
JDK
JDK (Java development kit) provides all the needed support for
software development in Java.
KernelTraining.com/core-java
5. How Java works ?
Java compilers convert your code from human readable to
something called “byte code” in the Java world.
“Byte code” is interpreted by a JVM, which operates much
like a physical CPU to actually execute the compiled code.
Just-in-time (JIT) compiler is a program that turns Java byte
code into instructions that can be sent directly to the
processor.
KernelTraining.com/core-java
6. History
James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
Hot Java
The first Java-enabled Web browser
JDK Evolutions
J2SE, J2ME, and J2EE
KernelTraining.com/core-java
7. Characteristics of Java
Platform Independent
Portable
Object Oriented
Robust & Secure
Distributed
Simple & Small
Multi Threaded
Dynamic
Compile & Interpreted
High Performance
KernelTraining.com/core-java
9. JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications
such as Java servlets and Java Server Pages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
KernelTraining.com/core-java
10. Java IDE Tools
Forte by Sun Microsystems
Borland JBuilder
Microsoft Visual J++
WebGain Café
IBM Visual Age for Java
KernelTraining.com/core-java
11. Getting Started with Java Programming
A Simple Java Application
Compiling Programs
Executing Applications
KernelTraining.com/core-java
12. A Simple Application
Example 1.1
//This application program prints Welcome
//to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
RunSource
NOTE: To run the program, install slide files on hard disk.
KernelTraining.com/core-java
13. Creating and Compiling Programs
On command line
javac file.java
Source Code
Create/Modify Source Code
Compile Source Code
i.e. javac Welcome.java
Bytecode
Run Byteode
i.e. java Welcome
Result
If compilation errors
If runtime errors or incorrect result
KernelTraining.com/core-java
15. Comments
In Java, comments are preceded by two slashes (//) in
a line,
or enclosed between /* and */ in one or multiple lines.
When the compiler sees //, it ignores all text after // in
the same line.
When it sees /*, it scans for the next */ and ignores any
text between /* and */.
KernelTraining.com/core-java
17. Program demonstrating
DataTypes
class Second {
public static void main (String args[ ]) {
int x - 90;
short y = 4;
float z = 10.87f;
String name = "Sarti";
System.Out.println( "The integer value is " + x) ;
System.out.println ("The short value is " + y);
System.out.println ("The float value is " + z);
System.out.println ("The string value is " + name);
}
}
KernelTraining.com/core-java
18. Save this file as Second. java and compile using javac Second.java at
DOS prompt.
On successful compilation, execute the program using Java Second
The output is displayed as :
The integer value is 90
The short value is 4
The float value is 10.87
The string value is Sarti
The lines 3-6 depict the declaration, initialization and naming of
various data types. The values of the declared variables are printed
from lines 7-10. The + operator is used here as a concatenation
operator .
KernelTraining.com/core-java
19. Operators
Operators are special symbols used in expressions.
They include arithmetic operators, assignment
operators, increment and decrement operators,
logical operators, bitwise operators and comparison
operators.
KernelTraining.com/core-java
20. Arithmetic Operators
Java has five basic arithmetic operators. Each of
these operators takes two operands, one on
either side of the operator. The list of arithmetic
operators is given below:Operator Meaning Example
+ Addition 8+10
- Subtraction 10-8
* Multiplication 20*84
/ Division 10/5
% Modulus 10% 6
KernelTraining.com/core-java
21. Example illustrates the usage of various arithmetic operators.
class Three {
public static void main (String args[ ]) {
int x = 10;
int y = 20;
float z = 25.98f;
System.out.println ("The value of x + y is " + (x + y));
System. out. println ("The value of z - y is " + (z - y) );
System.out.println ("The value of x * y is " + (x * y));
System.out.println ("The value of z / y is " + (z / y));
System.out.println ("The value of z % y is " + (z % y));
}
}
KernelTraining.com/core-java
22. Save this file as Three.java and compile using javac
Three.java at DOS prompt.
On successful compilation, execute the source code using:
Java Three
The output appears as shown below:
The value of x + y is 30
The value of z - y is 5.98
The value of x * y is 200
The value of z / y is 1.299
The value of z % y is 5.98
Notice the usage of the various arithmetic operators from
lines 6-10.
KernelTraining.com/core-java
23. Assignment Operators
The assignment operators used in C and C++ are also
used in Java. A selection of assignment operators is
given.
Expression Meaning
x += y x = x + y
x -+ y x = x-y
x *= y x = x * y
x /= y x = x /y
x=y x=y
KernelTraining.com/core-java
24. Example demonstrates the various operator assignments in
action.
class assign{
public static void main(String args[ ]) {
int a=1;
int b=2;
int c=3;
a+=5;
b*=4;
c+=a*b;
c%=6;
System.out.println ("a=" +a);
System.out.println("b=" +b);
System.out.println("c=" +c);
}
}
KernelTraining.com/core-java
25. Save this file as assign.java and compile using javac
assign.java at DOS prompt.
On successful compilation, execute the source code
using: Java assign
The output appears as shown below:
a=6
b=8
c=3
KernelTraining.com/core-java
26. Incrementing andDecrementing
To increment or decrement a value by one, the ++ operator and
— operator are used respectively. The increment and the
decrement operators can be prefixed or post fixed. The different
increment and decrement operators can be used as given below:
++a (Pre increment operator) -Increment a by 1, then use the
new value of a in the expression in which a resides.
a++ (Post increment operator) - Use the current value of a in
the expression in which a resides and then increment a by 1.
- -b (Pre decrement operator) - Decrement b by 1, then use the
new value of b in the expression in which b resides.
b- -(Post decrement operator) - Use the current value of b in the
expression in which b resides and then decrement b by 1.
KernelTraining.com/core-java
27. Example demonstrates the usage of the increment and decrement
operators.
class IncDec {
public static void main (String args[ ] ) {
int a =1;
int b=2;
int c=++b;
int d=a++;
C++;
System.out.println ("a =" +a);
System.out.println ("b =" +b);
System.out.println ("c =" +c);
System.out.println ("d =" +d) ;
}
}
KernelTraining.com/core-java
28. Save this file as IncDec.java and compile using javac
IncDec.java at DOS prompt.
On successful compilation, execute the source code
using : Java IncDec
The output appears as given below:
a=2
b=3
c=4
d=l
KernelTraining.com/core-java
29. Comparison Operators
There are several expressions for testing equality
and magnitude. All these expressions return a
Boolean value. Table 1.5 enlists the comparison
operators.Operator Meaning Example
== Equal u==45
! = Not equal u!=75
< Less than u<85
> Greater than u>68
<= Less than or equal to u <= 53
>= Greater than or equal to u >= 64
KernelTraining.com/core-java
30. Logical Operators
Logical operators available are AND, OR, XOR and NOT.
The AND operator (& or &&) returns true only if both sides in
an expression are true. If any one side fails, the operator returns
false. For example, consider the following statement
gender ==1 && age >=65
This condition is true if and only if both the simple conditions
are true. In the && operator, if the left side of the expression
returns false, the whole expression returns false and the right
side is totally neglected. The & operator evaluates both sides
irrespective of outcome.
KernelTraining.com/core-java
31. The I or II is used for OR combination. This returns true if any of
the expressions is true. Only if all the conditions are false, the
expression is false.Consider the following statement
semesterAverage>=90 | | finalExam >=90
The above condition is true if either of the two conditions is true.
The II evaluates the left expression first and if it returns true,
never evaluates the right side expression. A single I evaluates
both the expressions regardless of the outcome.
The XOR operator indicated by ^, returns true only if its operands
are different. If both its operands are similar (true-true, false-
false), it returns false.
The ! operator is used for NOT. The value of NOT is negation of
the expression.
KernelTraining.com/core-java
32. Bitwise Operators
The bitwise operators are inherited from C and
C++. They are used to perform operations on
individual bits in integers. A list of the bitwise
operators available is given
Operator Meaning
& Bitwise AND
I Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right Shift
>>>> Zero fill right shift
~ Bitwise Complement
<<= Left Shift assignment
>>= Right Shift assignment
>>>= Zero fill right shift assignment
x&=y AND assignment
x l=y OR assignment
x ^= y XOR assignment
KernelTraining.com/core-java
33. The following example demonstrates the usage of bitwise operators.
class BitOp {
public static void main (String args [ ]) {
int a=1;
int b=2;
int c=3;
a=a|4;
b>>=1;
c<<=1;
a=a^c;
System.out.println ("a=" +a);
System. out.println ("b=" +b);
System.out.println ("c=" +c);
}
}
KernelTraining.com/core-java
34. Save this file as BitOp.java and compile using javac BitOp.java at DOS
prompt.
On successful compilation, execute the source code using : Java BitOp
The output appears as shown.
a=3
b=1
c=6
KernelTraining.com/core-java
35. Programming Constructs
Programming constructs in Java are the if construct and ternary
operator, switch statement, the while, do while and for loops.
The If Construct
The if else construct executes different bits of code based on
successful completion of a condition, that returns only boolean
values. If the execution is unsuccessful, the sets of instructions
under else are executed.
if (condition)
body of loop;
else
body of loop;
KernelTraining.com/core-java
36. The following example illustrates usage of the if else construct and logical operators.
class Four{
public static void main (String args[ ]) {
int month = 4;
String season;
if (month==12 || month = =1 || month = =2) {
seasons="Winter";
}
else if (month = =3 || month = =4 || month = =5){
season="Spring";
}
else if (month = =6 | | month = =7 | | month = =8){
season="Summer";
}
else if (month = =9 | | month = =10 | | month = =11) {
season="Autunm";
}
KernelTraining.com/core-java
37. else{
season="Invalid month";
}
System.out-println ("April is in " + season + ".");
}
}
Save this as Four.java and compile using javac Four .Java at DOS
prompt.
On successful compilation execute, using Java Four.
The output of the program is given below:
April is in Spring
KernelTraining.com/core-java
38. Conditional Operator
The conditional operator is otherwise known as the
ternary operator and is considered to be an
alternative to the if else construct. It returns a value
and the syntax is:
test ? pass : fail
If the value of test is true, the conditional operator
returns pass, else it returns fail. The following
example delineates the usage of the ternary operator.
KernelTraining.com/core-java
39. class Ternary {
public static void main (String args[ ]) {
int i = 10;
int j = 78;
int z = 0;
z=i < j ? i : j;
System.out.println ("The value assigned is " + z);
}
}
KernelTraining.com/core-java
40. Save this file as Five.java and compile using javac
Five.java at DOS prompt.
On successful compilation execute using: Java Five.
The output is given below:
The value assigned is 10
Note the usage of conditional operator at line number
6. If the value of the variable i is less than j, then z
contains the value of i, else it contains the value-of j.
KernelTraining.com/core-java
41. The while loop
The while loop executes a set of code repeatedly until
the condition returns false. The syntax of the while
loop is given below:
while (condition ) {
body of loop;
}
The do while loop is similar to the while loop except
that the condition to be evaluated is given at the end.
Hence the loop is executed at least once even when the
condition is false.
KernelTraining.com/core-java
42. Example depicts the usage of the while loop. class Fibo {
public static void main (String args[ ]) {
int max = 25;
int prev =0;
int next = 1;
int sum;
while (next <= max)
{
System.out.println ("The Fibonacci series is "+next);
sum = prev + next;
prev = next;
next = sum;
}
}
}KernelTraining.com/core-java
43. Save this file as Fibo.java and compile using javac Fibo.java at DOS prompt.
On successful compilation, execute using Java Fibo.
The output appears as given below:
The Fibonacci series is 1
The Fibonacci series is 1
The Fibonacci series is 2
The Fibonacci series is 3
The Fibonacci series is 5
The Fibonacci series is 8
The Fibonacci series is 13
The Fibonacci series is 21
The while loop, shown from line 7 - 13, is executed as long as the value of the
variable next is less than or equal to 25. This program generates the Fibonacci
series
KernelTraining.com/core-java
44. The for loop
The for loop repeats a set of statements a certain number of
times until a condition is matched. It is commonly used for
simple iteration. The for loop appears as shown.
for (initialization; test; expression) )
set of statements;
}
In the first part a variable is initialized to a value. The second
part consists of a test condition that returns only a Boolean. The
last part is an expression, evaluated every time the loop is
executed.
The following example depicts the usage of the for loop.
KernelTraining.com/core-java
45. class ForDemo {
public static void main (String args[ ]) {
int i;
for (i = 0;i < 10;i=i+2)
{
if ((i%2) = = 0)
System.out.println ("The number"+1+"is a even number");
}
}
}
KernelTraining.com/core-java
46. Save this file as ForDemo.java and compile using javac
ForDemo.java at DOS prompt.
On successful compilation, execute using Java ForDemo.
The output appears as given below:
The number 0 is an even number
The number 2 is an even number
The number 4 is an even number
The number 6 is an even number
The number 8 is an even number
This example generates even numbers that are less than 8.
Notice the usage of the for loop from line numbers 4-8.
KernelTraining.com/core-java
47. Switch Statement
The switch statement dispatches control to the
different parts of the code based on the value of a
single variable or expression. The value of
expression is compared with each of the literal
values in the case statements. If they match, the
following code sequence is executed. Otherwise
an optional default statement is executed. The
general form of switch statement is given below.
KernelTraining.com/core-java
48. switch (test) {
case valueone:
resultone;
break;
case valuetwo:
resulttwo;
break;
default :
defaultresult;
}
An illustration of the switch statement's usage is given below in Example
KernelTraining.com/core-java
49. class season {
public static void. main (String args[ ]) {
int v = 4;
switch(v) {
case 1:
case 2:
case 3:
System.out.println ("Spring is around the corner");
break;
case 4:
case 5:
case 6:
System.out.println ("Summer is scorching its way through");
KernelTraining.com/core-java
50. break;
case 7:
case 8:
case 9:
System.out.println ("Autumn leaves are abundant");
break;
case 10:
case 11:
case 12:
System.out.println ("Winter is freezing");
break;
}
}
}
KernelTraining.com/core-java
51. Save this file as season.java and compile using javac season.java at
DOS prompt.
On successful compilation, execute using Java season.
The output appears as given below:
Summer is scorching its way through
Note the usage of the switch statement between lines 4 - 25.
KernelTraining.com/core-java
52. Break and Continue
The break keyword halts the execution of the
current loop and forces control out of the loop.
The term break refers to the act of breaking out
of a block of code. It tells the runtime to pick up
execution past the end of the named block. In
order to refer to a block by name, Java has a label
construct that assigns a name to every block.
The following example demonstrates the usage of
break statement.
KernelTraining.com/core-java
53. class breakdemo {
public static void main (String args[ ] ) {
boolean t=true;
{
{
{
System.out.println ("Before the break");
if (t)
break b;
System.out.println ("This will not execute");
}
System.out.println ("This will not execute");
}
System.out.println ("This is after b");
}
}
}
KernelTraining.com/core-java
54. Save this file as breakdemo.java and compile using javac
breakdemo.java at DOS prompt.
On successful compilation, execute using Java breakdemo.
The output appears as given below:
Before the break
This is after b
continue is similar to break, except that instead of halting the
execution of the loop, it starts the next iteration.
The following example demonstrates the usage of continue
statement.
KernelTraining.com/core-java
55. class continuedemo {
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 (" ");
}
}
}
KernelTraining.com/core-java
56. Save this file as continuedemo.java and compile using
javac continuedemo.java at DOS prompt.
On successful compilation, execute using Java
continuedemo.
The output appears as given below:
0 1
2 3
4 5
6 7
8 9
KernelTraining.com/core-java