KEMBAR78
Java Basics for Beginners : Start Coding Now | PPTX
INTRODUCTION TO JAVA
DR SUTHA K,
ASSISTANT PROFESSOR,
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS,
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, RAMAPURAM.
WHAT IS……...?
• PROGRAM
• SOFTWARE
• PROGRAMMING LANGUAGE
• SYNTAX
JAVA DEVELOPERS
JAVA was developed by Sun Microsystems Inc in 1991
Patrick Naughton
JAMES GOSLING
James Gosling
a famous Canadian software developer
Working in Sun Microsystems since 1984
considered as father of Java
programming language.
did the original design of Java and
implemented its original compiler and virtual
machine
INTRODUCTION TO JAVA
• Java is a powerful Object-oriented Programming Language
• Easy to learn, especially for programmers already familiar with C or C++.
• Write once, run anywhere, because java programs are compiled into machine-independent byte
codes
• Java does not include the C unique statement keywords goto, sizeof, and typedef.
• Java does not contain the data types struct, union and enum.
• Java does not define the type modifiers keywords auto, extern, register, signed and unsigned.
• Java does not support pointers.
• Java does not have a preprocessor and therefore we cannot use # define, #include, and # ifdef
statements.
• Java does not support operator overloading and Multiple Inheritance
JAVA VIRTUAL MACHINE
Each operating system has different JVM
•The target of java is to write a program once and then
run this program on multiple operating systems.
•Phases of Java program execution:
1)writing of the program in the editor.
2) compilation of program is done by javac compiler.
3) In third phase, Java Virtual Machine executes the
bytecode generated by compiler.
•JVM is a engine that converts java bytecode into
machines language.JVM is a part of Java Run
Environment (JRE).
EVOLUTION OF JAVA
The Java Developing Kit
provides a collection of
tools that are used for
developing and running
Java programs.
JAVA FEATURES
1) Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand. Eg : Pointers
and Operator Overloading are not in Java but were an important part of C++.
2) Object Oriented
In java, everything is an object which has some data and behaviour. Java can be easily
extended as it is based on Object Model. Following are some basic concept of OOP's.
3) Robust
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time
error checking and runtime checking. But the main areas which Java improved were Memory
Management and mishandled Exceptions by introducing automatic Garbage
Collector and Exception Handling.
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
4) Platform Independent
Java is guaranteed to be write-once, run-anywhere language.
On compilation Java program is compiled into bytecode. This bytecode is platform
independent and can be run on any machine, and this bytecode format also provide security.
Any machine with Java Runtime Environment can run Java Programs.
5) Secure
Java secure features it enable us to develop virus free, temper free system. Java program
always runs in Java runtime environment with almost null interaction with system OS, hence it
is more secure.
6) Multi Threading
Java multithreading feature makes it possible to write program that can do many tasks
simultaneously. Benefit of multithreading is that it utilizes same memory and other resources
to execute multiple threads at the same time.
7) Architectural Neutral
Java compiler generates bytecodes, which have nothing to do with a particular computer
architecture, hence a Java program is easy to interpret on any machine.
8) Portable
Java Byte code can be executed on any platform. There is no implementation dependent
features. Everything related to storage is predefined, example: size of primitive data types
9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C+
+. But, Java enables high performance with the use of just-in-time compiler.
10) Distributed
Java is also a distributed language. Programs can be designed to run on computer networks.
Java has a special class library for communicating using TCP/IP protocols. Creating network
connections is very much easy in Java as compared to C/C++.
PLATFORM INDEPENDENT:
• Java programs use the java virtual machine as abstraction and do not
access the operating system directly. This makes java programs highly
portable.
OBJECT-ORIENTATED PROGRAMMING LANGUAGE:
• Java supports the four principles of OOPs: Encapsulation, Inheritance,
Abstraction and Polymorphism.
STRONGLY-TYPED PROGRAMMING LANGUAGE:
• Java is strongly-typed, e.g., The types of the used variables must be pre-defined
and conversion to other objects is relatively strict, e.G., Must be done in most
cases by the programmer.
INTERPRETED AND COMPILED LANGUAGE:
• Java source code is initially compiled by javac compiler
• The Bytecode instructions are then interpreted by the java virtual machine
(jvm).
AUTOMATIC MEMORY MANAGEMENT:
• Java manages the memory allocation and de-allocation for creating
new objects.
• The program does not have direct access to the memory.
• The so-called garbage collector automatically deletes objects to which
no active pointer exists.
BASIC CONCEPTS OF OOPS
Object-oriented programming (OOPs) is a
programming model based on the concept of
“objects” that contain data and methods.
The primary purpose of object-oriented
programming is to increase the flexibility
and maintainability of programs.
OBJECT
• Any entity that has state and behavior is known as an object. For example, a chair, pen,
table, keyboard, bike, etc. It can be physical or logical.
• An object can be defined as an instance of a class. An object contains an address and takes
up some space in memory.
• Objects can communicate without knowing the
CLASS
• Classes and objects are basic concepts of object oriented programming which revolve around
the real life entities.
• A class is a user defined blueprint or prototype from which objects are created.
• It represents the set of properties or methods that are common to all objects of one type.
• A Class is a group of homogeneous entity and each entity of the group is called an object.
Eg. Class of Employees and Single Employee is an object.
ENCAPSULATION
• Encapsulation in java is a process of wrapping code and data together into a single
unit.
• It is a way to achieve data hiding in java because other class will not be able to
access the data through the private data members.
INHERITANCE
• When one object acquires all the properties and behaviors of a parent object, it is
known as inheritance.
• It provides code reusability.
POLYMORPHISM
• Polymorphism refers to the ability of oops programming languages to differentiate
between entities with the same name efficiently.
• In java, we use method overloading and method overriding to achieve
polymorphism.
ABSTRACTION
• Data abstraction is the property by which only the essential details are displayed to
the user.
• The non-essentials units are not displayed to the user.
• Ex: A car is viewed as a car rather than its individual components.
• In Java, abstraction is achieved by interfaces and abstract classes.
JAVA TOKENS
•Java tokens are smallest elements of a program which are identified by the compiler.
•Tokens in java include identifiers, keywords, literals, operators and, separators.
COMMENTS IN JAVA
Comments are description about the aim and features of the program. Comments
increase readability of a program.
Comments are not executed by the compiler and interpreter. It can be used to
provide information or explanation about the variable, method, class or any statement.
Three types of comments
Single line comments: These comments start with //
e.g.: // this is comment line·
Multi line comments: These comments start with /* and end with */
e.g.: /* this is comment line*/·
Java documentation comments: These comments start with /** and end with */
These comments are useful to create a HTML file called API (application programming
Interface) document. This file contains description of all the features of software.
JAVA STATEMENTS
CONTROL STATEMENT
Control statements are the statements which controls the order of
execution of the instructions
Java provides several control statements, and they are classified as
follows.
• Selection control statements ( decision making statements )
• Iterative control statements ( looping statements )
• Jump statements
DECISION MAKING STATEMENT
•In java, the selection statements are also known as decision making statements or branching
statements.
•The selection statements are used to select a part of the program to be executed based on a
condition.
•The Java if statement is used to test the condition. It checks boolean condition: true or false.
There are various types of if statement in Java.
if statement
if-else statement
if-else-if ladder
nested if statement
IF STATEMENT
The Java if statement tests the condition.
It executes the if block if condition is true.
Syntax:
if(condition)
{
//code to be executed
}
IF STATEMENT
IF – ELSE STATEMENT
The if-else statement tests the condition.
It executes the if block if condition is true
otherwise else block is executed.
Syntax:
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Example:
if(n%2==0)
System.out.println( n+” is an even number);
else
System.out.println( n+” is an odd number);
IF – ELSE IF STATEMENT
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//
code to be executed if all the conditions are fal
se
}
•The if-else-if statement is used when
there is a need to check multiple
conditions.
•Here, if statements are executed from
the top towards the bottom.
•As soon as the test expression is true,
codes inside the body of that
the if statement is executed.
• Then, the control of the program
jumps outside the if-else-if ladder.
•If all test expressions are false, codes
inside the body of else is executed.
IF – ELSE IF STATEMENT
class Ladder
{
public static void main(String[] args)
{
int number = 0;
// checks if number is greater than 0
if (number > 0)
{
System.out.println("The number is positive."); }
// checks if number is less than 0
else if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is 0.");
}
}
}
IF – ELSE IF STATEMENT
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//
code to be executed if all the conditions are fal
se
}
•The if-else-if statement is used when
there is a need to check multiple
conditions.
•Here, if statements are executed from
the top towards the bottom.
•As soon as the test expression is true,
codes inside the body of that
the if statement is executed.
• Then, the control of the program
jumps outside the if-else-if ladder.
•If all test expressions are false, codes
inside the body of else is executed.
NESTED IF STATEMENT
Syntax:
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
The nested if statement represents the if
block within another if block. The inner if
block condition executes only when outer
if block condition is true.
NESTED IF STATEMENT
public class NestedIfExample
{
public static void main(String args[])
{
int num=70;
if( num < 100 )
{
System.out.println("number is less than 100");
if(num > 50)
{
System.out.println("number is greater than
50"); }
}
}
}
SWITCH STATEMENT
Syntax:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matche
d;
}
Java switch statement executes one
statement from multiple conditions.
It is like if-else-if ladder statement.
The switch statement works with byte,
short, int, long, enum types, String and
some wrapper types like Byte, Short, Int,
and Long.
The case value must be literal or constant.
Each case statement can have a break
statement which is optional. When control
reaches to the break statement, it jumps the
control after the switch expression. If a
break statement is not found, it executes the
next case.
SWITCH STATEMENT
public class SwitchExample
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10:
System.out.println("10");
break;
case 20:
System.out.println("20");
break;
case 30:
System.out.println("30");
break;
default:
System.out.println(“Invalid No");
}
}
}
ITERATIVE CONTROL STATEMENTS
• In java, the iterative statements are also known as looping statements or
repetitive statements.
• The iterative statements are used to execute a part of the program repeatedly as
long as the given condition is true.
• Using iterative statements reduces the size of the code, reduces the code
complexity, makes it more efficient, and increases the execution speed.
• Java provides the following iterative statements.
While statement
Do-while statement
For statement
For-each statement
LOOPING STATEMENTS
LOOPING STATEMENT
For / While Loop do / while Loop
FLOW DIAGRAM
JUMP STATEMENT
• In java, the jump statements are used to terminate a block or take the
execution control to the next iteration.
• Java provides the following jump statements.
• break
• continue
• return
JAVA CONSTANTS
A constant is a variable whose value cannot
change once it has been assigned
Syntax:
static final datatype identifier_name = constant;
The static modifier causes the variable to be
available without an instance of it’s defining class
being loaded
The final modifier makes the variable
unchangeable
VARIABLES
• A variable holds a value that can be changed
during program execution.
• It is a name of a memory location.
• There are three types of variables in java:
Local, Instance and Static.
Syntax for declaring variables:
datatype variable = value;
Example:
int num = 15;
String name = “John";
NAMING CONVENTIONS
• Variable name cannot contain white spaces. For example: int num ber = 100; is invalid.
• Variable name can begin with special characters such as $ and _
• Variable name should begin with a lower case letter, for example int num.
(variables names that has more than one words: int smallNumber; int bigNumber; (start
the second word with capital letter).
• Variable names are case sensitive in java.
DATA TYPES IN JAVA
Data types are divided into two groups
• Primitive data types
byte, short, int, long, float, double,
boolean and char
• Non-primitive data types
String, Arrays and Classes
A data type is a type of data
EXAMPLES
• int mynum = 5; // integer (whole number)
• float myfloatnum = 5.99f; // floating point number
• char myletter = 'd'; // character
• boolean mybool = true; // boolean
• string mytext = "hello"; // string
TYPE CASTING
The process of converting an object or a variable of one type to another type is known as Typecasting
Two types of casting
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
EXAMPLE
Widening Casting (Implicit Type casting)
class Main
{
public static void main(String[] args)
{
// create int type variable
int num = 10;
System.out.println("The integer value: " + num);
// convert into double type
double data = num;
System.out.println("The double value: " + data);
}
}
Output
The integer value: 10
The double value: 10.0
NARROWING CASTING
Syntax
dataType variableName = (dataType)
variableToConvert;
Example
double Mark = 87.6;
int Grade;
Grade = (int) Mark; //will assign 87
Narrowing Casting (Explicit Type casting)
class Main
{
public static void main(String[] args)
{
// create double type variable
double num = 10.99;
System.out.println("The double value: " + num); //
convert into int type
int data = (int)num;
System.out.println("The integer value: " + data);
}
}
Output
The double value: 10.99
The integer value: 10
OPERATORS
 Java provides a rich set of
operators to manipulate variables.
 Java operators are divided into the
following groups
Arithmetic Operators
Relational Operators
Logical Operators
Ternary Operator
Bitwise Operators
Assignment Operators
Unary Operators
Operators are special symbols (characters) that carry out operations on operands
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations
like addition, subtraction, multiplication, etc.
EXAMPLE
class ArithmeticOperator
{
public static void main(String[] args)
{
double number1 = 12.5, number2 = 3.5, result;
// Using addition operator
result = number1 + number2;
System.out.println("number1 + number2 = " + result);
// Using subtraction operator result = number1 - number2;
System.out.println("number1 - number2 = " + result);
// Using multiplication operator
result = number1 * number2;
System.out.println("number1 * number2 = " + result);
// Using division operator result = number1 / number2;
System.out.println("number1 / number2 = " + result);
// Using remainder operator
result = number1 % number2;
System.out.println("number1 % number2 = " + result);
}
}
Output:
number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 =
3.5714285714285716
number1 % number2 = 2.0
The + operator can also be
used to concatenate two or
more strings.
RELATIONAL OPERATORS
Relational operators are used to compare any two values
EXAMPLE
class RelationalOperator
{
public static void main(String[] args)
{
int number1 = 5, number2 = 6;
if (number1 > number2)
{
System.out.println("number1 is greater than number2.");
}
else
{
System.out.println("number2 is greater than number1.");
}
}
}
Output:
number2 is greater than
number1.
LOGICAL OPERATORS
A logical operator is used to connect two or more relational
expressions
EXAMPLE
class LogicalOperator
{
public static void main(String[] args)
{
int number1 = 1, number2 = 2, number3 = 9;
boolean result;
result = (number1 > number2) || (number3 >
number1);
System.out.println(result);
result = (number1 > number2) && (number3 >
number1); System.out.println(result);
}
}
Output:
true
false
TERNARY OPERATOR
Syntax
variable = Expression ? expression1 : expression2
•If the Expression is true, expression1 is assigned to
the variable.
•If the Expression is false, expression2 is assigned
to the variable.
Ternary Operator is the conditional operator and It is shorthand for the if-then-else statement.
class ConditionalOperator
{
public static void main(String[] args)
{
int febDays = 29;
String r;
r = (febDays == 28) ? "Not a leap year" : "Leap
year";
System.out.println(r);
}
}
Output:
Leap year
BITWISE OPERATORS
Bitwise operators are used to perform binary logic with the bits of an integer or long
integer.
ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables.
UNARY OPERATORS
The unary operator performs operations on only one operand.
EXAMPLE
class UnaryOperator
{
public static void main(String[] args)
{
double number = 5.2;
boolean flag = false;
System.out.println("+number = " + +number);
System.out.println("-number = " + -number);
Output:
+number = 5.2
-number = -5.2
number = 6.2
number = 5.2
!flag = true
System.out.println("number = " + ++number);
System.out.println("number = " + --number);
System.out.println("!flag = " + !flag);
}
}

Java Basics for Beginners : Start Coding Now

  • 1.
    INTRODUCTION TO JAVA DRSUTHA K, ASSISTANT PROFESSOR, DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS, SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, RAMAPURAM.
  • 2.
    WHAT IS……...? • PROGRAM •SOFTWARE • PROGRAMMING LANGUAGE • SYNTAX
  • 3.
    JAVA DEVELOPERS JAVA wasdeveloped by Sun Microsystems Inc in 1991 Patrick Naughton JAMES GOSLING James Gosling a famous Canadian software developer Working in Sun Microsystems since 1984 considered as father of Java programming language. did the original design of Java and implemented its original compiler and virtual machine
  • 4.
    INTRODUCTION TO JAVA •Java is a powerful Object-oriented Programming Language • Easy to learn, especially for programmers already familiar with C or C++. • Write once, run anywhere, because java programs are compiled into machine-independent byte codes • Java does not include the C unique statement keywords goto, sizeof, and typedef. • Java does not contain the data types struct, union and enum. • Java does not define the type modifiers keywords auto, extern, register, signed and unsigned. • Java does not support pointers. • Java does not have a preprocessor and therefore we cannot use # define, #include, and # ifdef statements. • Java does not support operator overloading and Multiple Inheritance
  • 5.
    JAVA VIRTUAL MACHINE Eachoperating system has different JVM •The target of java is to write a program once and then run this program on multiple operating systems. •Phases of Java program execution: 1)writing of the program in the editor. 2) compilation of program is done by javac compiler. 3) In third phase, Java Virtual Machine executes the bytecode generated by compiler. •JVM is a engine that converts java bytecode into machines language.JVM is a part of Java Run Environment (JRE).
  • 6.
    EVOLUTION OF JAVA TheJava Developing Kit provides a collection of tools that are used for developing and running Java programs.
  • 7.
  • 8.
    1) Simple Java iseasy to learn and its syntax is quite simple, clean and easy to understand. Eg : Pointers and Operator Overloading are not in Java but were an important part of C++. 2) Object Oriented In java, everything is an object which has some data and behaviour. Java can be easily extended as it is based on Object Model. Following are some basic concept of OOP's. 3) Robust Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling. • Object • Class • Inheritance • Polymorphism • Abstraction • Encapsulation
  • 9.
    4) Platform Independent Javais guaranteed to be write-once, run-anywhere language. On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, and this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs. 5) Secure Java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure. 6) Multi Threading Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time.
  • 10.
    7) Architectural Neutral Javacompiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to interpret on any machine. 8) Portable Java Byte code can be executed on any platform. There is no implementation dependent features. Everything related to storage is predefined, example: size of primitive data types 9) High Performance Java is an interpreted language, so it will never be as fast as a compiled language like C or C+ +. But, Java enables high performance with the use of just-in-time compiler. 10) Distributed Java is also a distributed language. Programs can be designed to run on computer networks. Java has a special class library for communicating using TCP/IP protocols. Creating network connections is very much easy in Java as compared to C/C++.
  • 11.
    PLATFORM INDEPENDENT: • Javaprograms use the java virtual machine as abstraction and do not access the operating system directly. This makes java programs highly portable. OBJECT-ORIENTATED PROGRAMMING LANGUAGE: • Java supports the four principles of OOPs: Encapsulation, Inheritance, Abstraction and Polymorphism.
  • 12.
    STRONGLY-TYPED PROGRAMMING LANGUAGE: •Java is strongly-typed, e.g., The types of the used variables must be pre-defined and conversion to other objects is relatively strict, e.G., Must be done in most cases by the programmer. INTERPRETED AND COMPILED LANGUAGE: • Java source code is initially compiled by javac compiler • The Bytecode instructions are then interpreted by the java virtual machine (jvm).
  • 13.
    AUTOMATIC MEMORY MANAGEMENT: •Java manages the memory allocation and de-allocation for creating new objects. • The program does not have direct access to the memory. • The so-called garbage collector automatically deletes objects to which no active pointer exists.
  • 14.
    BASIC CONCEPTS OFOOPS Object-oriented programming (OOPs) is a programming model based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs.
  • 15.
    OBJECT • Any entitythat has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical. • An object can be defined as an instance of a class. An object contains an address and takes up some space in memory. • Objects can communicate without knowing the CLASS • Classes and objects are basic concepts of object oriented programming which revolve around the real life entities. • A class is a user defined blueprint or prototype from which objects are created. • It represents the set of properties or methods that are common to all objects of one type. • A Class is a group of homogeneous entity and each entity of the group is called an object. Eg. Class of Employees and Single Employee is an object.
  • 16.
    ENCAPSULATION • Encapsulation injava is a process of wrapping code and data together into a single unit. • It is a way to achieve data hiding in java because other class will not be able to access the data through the private data members. INHERITANCE • When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. • It provides code reusability.
  • 17.
    POLYMORPHISM • Polymorphism refersto the ability of oops programming languages to differentiate between entities with the same name efficiently. • In java, we use method overloading and method overriding to achieve polymorphism. ABSTRACTION • Data abstraction is the property by which only the essential details are displayed to the user. • The non-essentials units are not displayed to the user. • Ex: A car is viewed as a car rather than its individual components. • In Java, abstraction is achieved by interfaces and abstract classes.
  • 18.
    JAVA TOKENS •Java tokensare smallest elements of a program which are identified by the compiler. •Tokens in java include identifiers, keywords, literals, operators and, separators.
  • 20.
    COMMENTS IN JAVA Commentsare description about the aim and features of the program. Comments increase readability of a program. Comments are not executed by the compiler and interpreter. It can be used to provide information or explanation about the variable, method, class or any statement. Three types of comments Single line comments: These comments start with // e.g.: // this is comment line· Multi line comments: These comments start with /* and end with */ e.g.: /* this is comment line*/· Java documentation comments: These comments start with /** and end with */ These comments are useful to create a HTML file called API (application programming Interface) document. This file contains description of all the features of software.
  • 21.
  • 22.
    CONTROL STATEMENT Control statementsare the statements which controls the order of execution of the instructions Java provides several control statements, and they are classified as follows. • Selection control statements ( decision making statements ) • Iterative control statements ( looping statements ) • Jump statements
  • 23.
    DECISION MAKING STATEMENT •Injava, the selection statements are also known as decision making statements or branching statements. •The selection statements are used to select a part of the program to be executed based on a condition. •The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java. if statement if-else statement if-else-if ladder nested if statement
  • 24.
    IF STATEMENT The Javaif statement tests the condition. It executes the if block if condition is true. Syntax: if(condition) { //code to be executed }
  • 25.
  • 26.
    IF – ELSESTATEMENT The if-else statement tests the condition. It executes the if block if condition is true otherwise else block is executed. Syntax: if(condition) { //code if condition is true } else { //code if condition is false } Example: if(n%2==0) System.out.println( n+” is an even number); else System.out.println( n+” is an odd number);
  • 27.
    IF – ELSEIF STATEMENT Syntax: if(condition1) { //code to be executed if condition1 is true } else if(condition2) { //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ // code to be executed if all the conditions are fal se } •The if-else-if statement is used when there is a need to check multiple conditions. •Here, if statements are executed from the top towards the bottom. •As soon as the test expression is true, codes inside the body of that the if statement is executed. • Then, the control of the program jumps outside the if-else-if ladder. •If all test expressions are false, codes inside the body of else is executed.
  • 28.
    IF – ELSEIF STATEMENT class Ladder { public static void main(String[] args) { int number = 0; // checks if number is greater than 0 if (number > 0) { System.out.println("The number is positive."); } // checks if number is less than 0 else if (number < 0) { System.out.println("The number is negative."); } else { System.out.println("The number is 0."); } } }
  • 29.
    IF – ELSEIF STATEMENT Syntax: if(condition1) { //code to be executed if condition1 is true } else if(condition2) { //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ // code to be executed if all the conditions are fal se } •The if-else-if statement is used when there is a need to check multiple conditions. •Here, if statements are executed from the top towards the bottom. •As soon as the test expression is true, codes inside the body of that the if statement is executed. • Then, the control of the program jumps outside the if-else-if ladder. •If all test expressions are false, codes inside the body of else is executed.
  • 30.
    NESTED IF STATEMENT Syntax: if(condition) { //codeto be executed if(condition) { //code to be executed } } The nested if statement represents the if block within another if block. The inner if block condition executes only when outer if block condition is true.
  • 31.
    NESTED IF STATEMENT publicclass NestedIfExample { public static void main(String args[]) { int num=70; if( num < 100 ) { System.out.println("number is less than 100"); if(num > 50) { System.out.println("number is greater than 50"); } } } }
  • 32.
    SWITCH STATEMENT Syntax: switch(expression) { case value1: //codeto be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matche d; } Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. The case value must be literal or constant. Each case statement can have a break statement which is optional. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
  • 33.
    SWITCH STATEMENT public classSwitchExample { public static void main(String[] args) { int number=20; switch(number) { case 10: System.out.println("10"); break; case 20: System.out.println("20"); break; case 30: System.out.println("30"); break; default: System.out.println(“Invalid No"); } } }
  • 34.
    ITERATIVE CONTROL STATEMENTS •In java, the iterative statements are also known as looping statements or repetitive statements. • The iterative statements are used to execute a part of the program repeatedly as long as the given condition is true. • Using iterative statements reduces the size of the code, reduces the code complexity, makes it more efficient, and increases the execution speed. • Java provides the following iterative statements. While statement Do-while statement For statement For-each statement
  • 35.
  • 36.
  • 37.
    For / WhileLoop do / while Loop FLOW DIAGRAM
  • 38.
    JUMP STATEMENT • Injava, the jump statements are used to terminate a block or take the execution control to the next iteration. • Java provides the following jump statements. • break • continue • return
  • 40.
    JAVA CONSTANTS A constantis a variable whose value cannot change once it has been assigned Syntax: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it’s defining class being loaded The final modifier makes the variable unchangeable
  • 42.
    VARIABLES • A variableholds a value that can be changed during program execution. • It is a name of a memory location. • There are three types of variables in java: Local, Instance and Static. Syntax for declaring variables: datatype variable = value; Example: int num = 15; String name = “John";
  • 43.
    NAMING CONVENTIONS • Variablename cannot contain white spaces. For example: int num ber = 100; is invalid. • Variable name can begin with special characters such as $ and _ • Variable name should begin with a lower case letter, for example int num. (variables names that has more than one words: int smallNumber; int bigNumber; (start the second word with capital letter). • Variable names are case sensitive in java.
  • 44.
    DATA TYPES INJAVA Data types are divided into two groups • Primitive data types byte, short, int, long, float, double, boolean and char • Non-primitive data types String, Arrays and Classes A data type is a type of data
  • 45.
    EXAMPLES • int mynum= 5; // integer (whole number) • float myfloatnum = 5.99f; // floating point number • char myletter = 'd'; // character • boolean mybool = true; // boolean • string mytext = "hello"; // string
  • 46.
    TYPE CASTING The processof converting an object or a variable of one type to another type is known as Typecasting Two types of casting Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte
  • 47.
    EXAMPLE Widening Casting (ImplicitType casting) class Main { public static void main(String[] args) { // create int type variable int num = 10; System.out.println("The integer value: " + num); // convert into double type double data = num; System.out.println("The double value: " + data); } } Output The integer value: 10 The double value: 10.0
  • 48.
    NARROWING CASTING Syntax dataType variableName= (dataType) variableToConvert; Example double Mark = 87.6; int Grade; Grade = (int) Mark; //will assign 87 Narrowing Casting (Explicit Type casting) class Main { public static void main(String[] args) { // create double type variable double num = 10.99; System.out.println("The double value: " + num); // convert into int type int data = (int)num; System.out.println("The integer value: " + data); } } Output The double value: 10.99 The integer value: 10
  • 49.
    OPERATORS  Java providesa rich set of operators to manipulate variables.  Java operators are divided into the following groups Arithmetic Operators Relational Operators Logical Operators Ternary Operator Bitwise Operators Assignment Operators Unary Operators Operators are special symbols (characters) that carry out operations on operands Arithmetic Operators Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
  • 50.
    EXAMPLE class ArithmeticOperator { public staticvoid main(String[] args) { double number1 = 12.5, number2 = 3.5, result; // Using addition operator result = number1 + number2; System.out.println("number1 + number2 = " + result); // Using subtraction operator result = number1 - number2; System.out.println("number1 - number2 = " + result); // Using multiplication operator result = number1 * number2; System.out.println("number1 * number2 = " + result); // Using division operator result = number1 / number2; System.out.println("number1 / number2 = " + result); // Using remainder operator result = number1 % number2; System.out.println("number1 % number2 = " + result); } } Output: number1 + number2 = 16.0 number1 - number2 = 9.0 number1 * number2 = 43.75 number1 / number2 = 3.5714285714285716 number1 % number2 = 2.0 The + operator can also be used to concatenate two or more strings.
  • 51.
    RELATIONAL OPERATORS Relational operatorsare used to compare any two values
  • 52.
    EXAMPLE class RelationalOperator { public staticvoid main(String[] args) { int number1 = 5, number2 = 6; if (number1 > number2) { System.out.println("number1 is greater than number2."); } else { System.out.println("number2 is greater than number1."); } } } Output: number2 is greater than number1.
  • 53.
    LOGICAL OPERATORS A logicaloperator is used to connect two or more relational expressions
  • 54.
    EXAMPLE class LogicalOperator { public staticvoid main(String[] args) { int number1 = 1, number2 = 2, number3 = 9; boolean result; result = (number1 > number2) || (number3 > number1); System.out.println(result); result = (number1 > number2) && (number3 > number1); System.out.println(result); } } Output: true false
  • 55.
    TERNARY OPERATOR Syntax variable =Expression ? expression1 : expression2 •If the Expression is true, expression1 is assigned to the variable. •If the Expression is false, expression2 is assigned to the variable. Ternary Operator is the conditional operator and It is shorthand for the if-then-else statement. class ConditionalOperator { public static void main(String[] args) { int febDays = 29; String r; r = (febDays == 28) ? "Not a leap year" : "Leap year"; System.out.println(r); } } Output: Leap year
  • 56.
    BITWISE OPERATORS Bitwise operatorsare used to perform binary logic with the bits of an integer or long integer.
  • 57.
    ASSIGNMENT OPERATORS Assignment operatorsare used to assign values to variables.
  • 58.
    UNARY OPERATORS The unaryoperator performs operations on only one operand.
  • 59.
    EXAMPLE class UnaryOperator { public staticvoid main(String[] args) { double number = 5.2; boolean flag = false; System.out.println("+number = " + +number); System.out.println("-number = " + -number); Output: +number = 5.2 -number = -5.2 number = 6.2 number = 5.2 !flag = true System.out.println("number = " + ++number); System.out.println("number = " + --number); System.out.println("!flag = " + !flag); } }