Android Development: Usman Bin Fida
Android Development: Usman Bin Fida
DEVELOPMENT
Lecture No. 2
Type Conversions
Java Language
Creating Console Application
A Simple Java Program
package javaapplication1;
}
A Simple Java Program
package javaapplication1;
}
Basic Input and Output
public static void main(String[] args)
{
System.out.print("Enter your name \t");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("You entered \t\t"+name);
}
Variables
■ In Java, there are different types of variables, for example:
■ String - stores text, such as "Hello". String values are surrounded by double quotes.
■ int - stores integers (whole numbers), without decimals, such as 123 or -123.
■ float - stores floating point numbers, with decimals, such as 19.99 or -19.99.
■ char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes.
■ boolean - stores values with two states: true or false
Variables Example
int number ;
String countryName= "Pakistan";
float cgpa;
double area =
3.142325677; char c = 'A';
boolean flag = true;
Relational Operators
Operator Meaning
<= Returns true if first expression is less than or equal to second expression
>= Returns true if first expression is greater than or equal to second expression
Relational Operator Example
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
Branching
■ The if statement
else
{
System.out.println("Fail");
}
The if-else Statement
if (x > 50)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
The switch Statement
char choice = 'B';
switch(choice)
{
case 'A’:
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
default:
System.out.println("Invalid Input");
break;
}
The switch Statement
char choice = 'B';
switch(choice)
{
case 'A’:
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
default:
System.out.println("Invalid Input");
break;
}
Iterations
■ while loops
■ do-while loops
■ for loops
while loop
public static void main(String[] args)
{
int n = 1;
while (n < 6)
{
System.out.println("N is "+ n);
n++;
}
}
while loop
public static void main(String[] args)
{
while (n < 6) int n = 1;
{
System.out.println("N is "+ n);
n++;
}
}
do-while loop
int sum = 0;
int i= 0;
do
{
sum = sum + i;
i++;
} while(i<5);
int sum = 0;
int i= 0;
do
{
sum = sum + i;
i++;
} while(i<5);
System.out.println("Total Sum: "+ sum);
for loop
public static void main(String[] args)
{
int sum = 0;
■ Implicit conversions
■ Explicit conversions (casts)
Implicit Conversions
■ No special syntax is required because the conversion is type safe and no data will be
lost
■ Examples include conversions from smaller to larger integral types, and conversions
from derived classes to base classes
Implicit Conversions
public static void main(String[] args)
{
int a = 20056; long b = a; float c =
24567.45f; double d = c;
System.out.println("a = "+ a);
System.out.println("b = "+ b);
System.out.println("c = "+ c);
System.out.println("d = "+ d );
}
Explicit Conversions
(destinationType)sourceVariable
(int)dobuleAmount
Explicit Conversions
public static void main(String[] args)
{
String value; int
number, square;
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
value = scanner.nextLine();
number = Integer.parseInt(value);
square = number * number;
System.out.println("Square is\t"+ square);
}
THANK YOU