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)
{
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 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;
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
■ The switch statement
The if Statement
public class JavaApplication1
{
public static void main(String[] args)
{
int value = 10/2;
if (value == 5)
{
System.out.println(true);
}
}
}
The if Statement
public class JavaApplication1
{
public static void main(String[] args)
{
int value = 10/2;
} } if (value == 5)
{
System.out.println(true);
}
The if-else Statement
if (x > 50)
{
System.out.println("Pass");
}
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);
System.out.println("Total Sum: "+ sum);
do-while loop
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;
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;
System.out.println("a = "+ a);
System.out.println("b = "+ b);
System.out.println("c = "+ c);
System.out.println("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;
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