JAVA PROGRAMMING -
-SYED ASHIK NAINA S M
Topics covered in this session
Programming concepts
• Declaring variables with data types in JAVA.
• IF… ELSE conditions
• For loops
• While loops
• Arrays(1 dimensional and multi dimensional)
Programs
• Hello World
• Check a number if it is a positive number or negative number
• Fibonacci series (with and without user input)(both in “for loop” and “while loop”)
• Transpose of a matrix
Basic information about JAVA
JAVA is a powerful object oriented programming
language(OOPS) and is similar to c++ in many aspects.
It was developed by James Gosling from sun microsystems
in 1991. It was released in 1995.
Java is case sensitive.
Class names must always start with capital letter
Hello world program
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Data types in Java
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal
digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Syntax and Example for variable entry
Syntax
type variable = value;
Example
String name = "John";
System.out.println(name);
Syntax for IF… ELSE loops
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Example for IF… Else loop
Syntax for “For loop"
For loop
for (initialization; condition;increment/decrement) {
// code block to be executed
}
For-each loop
for (type variableName : arrayName) {
// code block to be executed
}
Example for “for loop”
Syntax for while loops
While loop
while (condition) {
// code block to be executed
}
Do-While loop
do {
// code block to be executed
} while (condition);
Example for while loop
Syntax and example for Arrays
Syntax
datatype[] variablename = {e1, e2, e3, e4};
Example
int[] myNum = {10, 20, 30, 40};
Example for multidimensional arrays
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
Program with arrays
public class MatrixTransposeExample{
public static void main(String args[]){
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
int transpose[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println("Printing Transpose Matrix:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
}
}
}
Home Work
To check whether a
string is palindrome or
not
THANK YOU