ANDROID
DEVELOPMENT
Lecture No. 2
Usman Bin Fida
NUTSHELL
What Will You Learn
Today?
Java Language
Creating Console Application
Basic Input and Output
Relational and Conditional Operators
Branching (if Statement, if-else Statement, switch Statement)
Iterations (while, do-while, and for loops)
Type Conversions
Java Language
Creating Console Application
A Simple Java Program
package javaapplication1;
public class JavaApplication1
{
public static void main(String[] args)
{
// TODO code application logic here
}
}
A Simple Java Program
package javaapplication1;
public class JavaApplication1
{
public static void main(String[] args)
{
// TODO code application logic here
}
}
Basic Input and Output
public static void main(String[] args)
{
[Link]("Enter your name \t");
Scanner scanner = new Scanner([Link]);
String name = [Link]();
[Link]("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 only if each expression is the same
!= Returns true only if each expression is different
< Returns true if first expression is less than second expression
> Returns true if first expression is greater than second expression
<= 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;
[Link]("a == b = " + (a == b) );
[Link]("a != b = " + (a != b) );
[Link]("a > b = " + (a > b) );
[Link]("a < b = " + (a < b) );
[Link]("b >= a = " + (b >= a) );
[Link]("b <= a = " + (b <= a) );
Branching
■ The if statement
■ The switch statement
The if Statement
public class JavaApplication1
{
public static void main(String[] args)
{
int value = 10/2;
if (value == 5)
{
[Link](true);
}
}
}
The if Statement
public class JavaApplication1
{
public static void main(String[] args)
{
int value = 10/2;
} } if (value == 5)
{
[Link](true);
}
The if-else Statement
if (x > 50)
{
[Link]("Pass");
}
else
{
[Link]("Fail");
}
The if-else Statement
if (x > 50)
{
[Link]("Pass");
}
else
{
[Link]("Fail");
}
The switch Statement
char choice = 'B';
switch(choice)
{
case 'A’:
[Link]("Excellent");
break;
case 'B':
[Link]("Very Good");
break;
default:
[Link]("Invalid Input");
break;
}
The switch Statement
char choice = 'B';
switch(choice)
{
case 'A’:
[Link]("Excellent");
break;
case 'B':
[Link]("Very Good");
break;
default:
[Link]("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)
{
[Link]("N is "+ n);
n++;
}
}
while loop
public static void main(String[] args)
{
while (n < 6) int n = 1;
{
[Link]("N is "+ n);
n++;
}
}
do-while loop
int sum = 0;
int i= 0;
do
{
sum = sum + i;
i++;
} while(i<5);
[Link]("Total Sum: "+ sum);
do-while loop
int sum = 0;
int i= 0;
do
{
sum = sum + i;
i++;
} while(i<5);
[Link]("Total Sum: "+ sum);
for loop
public static void main(String[] args)
{
int sum = 0;
for (int i= 0; i<5; i++)
{
sum += i;
}
}
for loop
public static void main(String[] args)
{
int sum = 0;
}
for (int i= 0; i<5; i++)
{
sum += i;
}
Type Conversions
■ Java is statically-typed at compile time, so after a variable is declared, it cannot be
used to store values of another type unless that type is convertible to the variable's
type
■ However, we might sometimes need to copy a value into a variable or method
parameter which is of another type
■ These kinds of operations are called type conversions
Kinds of Conversions
■ 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;
[Link]("a = "+ a);
[Link]("b = "+ b);
[Link]("c = "+ c);
[Link]("d = "+ d );
}
Explicit Conversions
■ Explicit conversions require a cast operator
■ The source and destination variables are compatible, but there is a risk of data loss
because the type of the destination variable can be a smaller size than that of the
source variable
(destinationType)sourceVariable
(int)dobuleAmount
Explicit Conversions
public static void main(String[] args)
{
String value; int
number, square;
[Link]("Enter a number");
Scanner scanner = new Scanner([Link]);
value = [Link]();
number = [Link](value);
square = number * number;
[Link]("Square is\t"+ square);
}
THANK YOU