Professional Documents
Culture Documents
&
Type Conversion in JAVA
Conditional Statements
• Simple if
• else..if
• Nested else…if
• Switch case statements
2
Simple if statement
if(Boolean expression)
statement-block;
Other statements;
Simple if statement
/* This is an example of simple statement */
Public class SampleTest
{
public static void main(String args[])
{
int a = 4;
int b = 20;
if( a < b)
{
System.out.println(“This is if statement”);
}
}
} 4
if….else statement
• The is an extension of simple if statement
• syntax :
if (Boolean expression)
{
True-block statements;
}
else
{
False – block statements;
}
5
Other statement;
if….else statement
/*Example of if else statement */
public class Sample Test
{
public static void main(Strings args[])
{
Scanner sin=new Scanner(System.in);
int age = sin.nextInt();
if(age>40)
{
System.out.println(“Eligible to Covid Vaccinate”)
}
else
{
System.out.println(“ Not Eligible to Covid Vaccinate “);
}
}
6
}
Conditional Operator
• Conditional operator is an one line alternative for if else condition.
• syntax :
condition? true statements : false statements:
Example:
String results =age>40 ? “eligible” : “not eligible”;
7
Cascading (Nested) if….else
Syntax:
if (condition1)
{
statement – 1
}
.
.
.
else if(condition)
{
statement – n
}
default statement
}
other statement 8
Cascading if….else Example
public class CascadeTest
{
public static void main(Strings args[])
{
Scanner sin=new Scanner(System.in);
int month = sin.nextInt();
if(month == 12 || month == 1 || month ==2)
System.out.println(“Winter”);
else if(month == 3|| month == 4 || month == 5)
System.out.printls(“Springs”);
else if(month == 6 || month == 7 || month == 8)
System.out.println(“Summer”)
else if(month == 9 || month == 10 || month == 11)
System.out.println(“Autumn”);
else
System.out.println(“invalid month”);
}
9
}
Switch Case
• The break statement will terminate the iteration or switch case block during the
execution of program,
• When a break statement is encountered in a loop, the loop exit and the program
continues with the statements immediately following the loop
• When the loops are nested, the break will only terminate the corresponding loop
body
12
Quiz
class QuizExample {
Public static void main(Strings s[] {
If( 100> 145 ) {
System.out.println(“ 100 is greater than 145 “);
}
Else
System.out.println(“ 145 is greater than 100 “);
}
}
13
Type Casting in JAVA
• Type casting is converting a value of one primitive data type to another type during
any operation.
• Two types of casting:
• Widening Casting (automatic) – converting a smaller size data type to a larger size
data type
byte->short->char->int->long->float->double
• Narrowing Casting (manual) – converting a larger size data type to a smaller size data
type.
double->float->long->int->char->short->byte
14
Widening Type Casting (automatic)
Public class Typecast1
{
public static void main(String[] args)
{
int myInt = 12;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 12
System.out.println(myDouble); // Outputs 12.0
}
} 15
Narrowing or Explicit Type Casting (manual)
• Assigns a value of larger data type to a smaller data type.
• Useful for incompatible data types where automatic conversion cannot be done.
• Target data type have to be represented in ( ) next to the = symbol.
20