Java Programming
Java Language Features
• Simple.
• Object-Oriented.
• Portable.
• Platform independent.
• Secured.
• Robust.
• Architecture neutral.
• Interpreted.
Java History
James Arthur Gosling, OC (born May 19, 1955) is a
Canadian computer scientist, best known as the
founder and lead designer behind the Java
programming language.
He is generally credited with having invented the
Java programming language in 1994. He created
the original design of Java and implemented the
language's original compiler and virtual machine
Java Environment
• JDK (Java SE Development Kit) - For Java
Developers. Includes a complete JRE(Java
Runtime Environment) plus tools for
developing, debugging, and monitoring Java
applications.
IDE for Java
• NetBeans
• Eclipse
• IntelliJ IDEA Community Edition
• Android Studio
• Enide Studio 2014
• BlueJ
• jEdit
• jGRASP
• JSource
• JDeveloper
• DrJava
Simple Java Program Structure
public class first
{
public static void main(String[] args)
{
[Link]("Hello World");
}
Java Data Types
Try these statements
Operators in Java
Operator Type Category Precedence
postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
= += -= *= /= %= &= ^= |= <<= >>=
Assignment assignment
>>>=
Exercise
• Print the addition of two numbers in Java
• Print the data type of a variable
• Print the size of a data type in Java
Reading Input from User
import [Link].*;
public class first {
public static void main(String[] args) {
// TODO Auto-generated method stub
[Link]("Enter your name");
Scanner test= new Scanner([Link]);
String name=[Link]();
[Link](name);
}
Exercise
• Create a poll for the user with a question and
option 1 to be yes and option 2 to be false.
• If the user enters 1 then add 1 to correct answers.
• If user enters 2 then add 1 to incorrect answers.
The user should be able to submit the poll as
many number of times required unit the user
wants to exit.
• After the user exits print the total number of
correct and incorrect submissions
Exercises
• Check if a given number is odd or even number
• Read the radius and print the area of a circle
• Read year and check if the given year is a leap year
• Display Subject Name based on room number. If the user
enters 823 then display Java Programming , If the user
enters 824 then display Python programming for any
other input display Invalid input to the user
• Print the sum of first n numbers. If N is 3 then print the
sum of 1+2+3 to the user. Get n from the user
• Print the sum of the series 12+22+32 up to n terms
• Print the multiplication table by getting the n from the
user.
Nested Loops
• The placing of one loop inside the body of
another loop is called nesting.
• When you "nest" two loops, the outer loop
takes control of the number of complete
repetitions of the inner loop.
• While all types of loops may be nested, the
most commonly nested loops are for loops.
Example
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](“hello”);
}
}
Nested Loops
Print this pattern for n lines 5 //nested for loop
*
**
***
****
*****
Print this pattern for n lines from the
user
1 //nested for loops
12
123
1234
12345
123456
1234567
Print this pattern
****
***
**
*
Print this pattern for n lines
1234
123
12
1
Print this pattern
1
12
123
1234
123
12
1
If Statement
if (condition1)
{
Statement 1
}
else if (condition2)
{
Statement 2
}
else
{
Statement 3
}
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
..
case valueN:
statementN;
break;
default:
statementDefault;
}
Break Statement
The break statement will terminate the loop and the
control gets transferred to the next statement in the
program.
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
[Link](i);
}
Exercise
• Read 5 numbers from the user. If the user
enters 13 then display you are a lucky winner.
• You should not get any further input once
when the user hits the lucky number.
• If the user is not able to hit the lucky number
for all the five input numbers then print you
are unlucky.
Continue
• The continue statement makes the loop to immediately
move to the next iteration or breaks one iteration.
• The statements following continue will be skipped
when the continue statement executes.
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
[Link](i);
}
Exercise
• Read 5 numbers from the user . Sum only the
odd numbers. If the user enters an even
number then get the next number.