0% found this document useful (0 votes)
16 views

Lect 5 - 6 - Java Data Types With Operators

Uploaded by

alromaysaamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lect 5 - 6 - Java Data Types With Operators

Uploaded by

alromaysaamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Structured Programming

How java works


First App
class Hello{
public static void main(String a[]) {
System.out.println("Hello at MNU");
}
}
Variables
Variables
class Hello{
public static void main(String a[]) {
System.out.println(3 +5);
System.out.println(4*7);
}
}

class Hello{
public static void main(String a[]) {
int num1 = 3;
int num2 = 5;
int result= num1 + num2;
System.out.print(result);
}
}
Data Types
Data types are divided into two groups:
•Primitive data types - includes byte, short, int, long, float, double, boolean and char
•Non-primitive data types - such as String, Arrays and Classes
Data Types-Exercise
Data Types
class Hello{ class Hello{
public static void main(String a[]) { public static void main(String args[])
float num1 = 128.1f; {
int num2 = 5; int x, y, z;
byte x= 5; x = y = z = 50; //One Value to Multiple Variables
short val= 456;
long l = 45l; System.out.println(x + y + z);
boolean attend=true; }
char x2='d'; }
System.out.print(num1);
}
}
Declare Many Variables

class Hello{
public static void main(String args[])
{
String first="Menoufia";
String second= " University";
String fullName= first + second;
System.out.println(fullName);
}
}

class Hello{
public static void main(String args[]) {
String first"Menoufia", second= " University", fullName= first + second;
System.out.println(fullName);
}
}
Final Variables

class Hello{
public static void main(String args[])
{
final int myNum = 15;
myNum=30; // generate an error
System.out.println(myNum);
}
}
Literals
class Hello{
public static void main(String a[]) {
//literals
int num1 = 0b111; // converting from binary to decimal
int num2 = 0xA; // converting from Hexadecimal to decimal
double num3= 34; // double can hold integer
double num4 =2e5; // storing 200000
boolean num5= 1; // gives error
char b ='b';
b++;
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
System.out.println(b);
} }
Type casting and coversion

class Hello{ class Hello{


public static void main(String a[]){ public static void main(String a[]) {
byte d=4; double d=4.56;
int x= 5; int x= (int) d;
d=(byte)x; System.out.println(x);} }
System.out.println(d); }}
Arithmetic operators

Unary
Operators
Arithmetic operators
class Hello{
public static void main(String a[]) {

int x= 4;
// x= x + 1;
// x+=1;
x++; // x--;
System.out.println(x);
}
}
class Hello{
public static void main(String a[]) {

int x= 4;
// int result = ++x; // pre incremen
int result =x++; // post increment
System.out.println(result);
System.out.println(x);
}}
Assignment Operators
Examples
class Hello{
public static void main(String[] args) {
int x=1;
System.out.println("x= "+ x++ +" "+ ++x +" "+ x++ );
System.out.println("fianl val of x = " + x);
}
}

class Hello{
public static void main(String a[]) {
int x= 4; x+=10;
System.out.println(x);
}
}

class Hello{
public static void main(String a[]) {
int k= 5, y=1,z=2;
z+=k-y;
System.out.println(z);
}}
Relational Operators

class Hello{
public static void main(String a[]) {

int x= 15;
int y = 10;
System.out.println(x==y); // false
System.out.println(x !=y); // true
System.out.println(x > y); //true
System.out.println(x>=y); //true
System.out.println(x<y); //false
System.out.println(x<=y); //false
}}
Logical Operators

class Hello{
public static void main(String args[]) {
int x= 7, y = 5, a = 5, b= 9;

System.out.println((x > y) && (a < b)); //true


System.out.println((x < y) || (a > b)); //false
System.out.println(!(x < y)); //true
} }
Short-Circuit Logical Operators
By not evaluating the
second operand, time is
improved and more
efficient code is produced.

class SideEffects {
public static void main(String[] args) {
int i=0;
/* Here, i is still incremented even though
the if statement fails. */
if(false && (++i < 100))
System.out.println("this won't be displayed");
System.out.println("if statement executed: " + i); // displays 0
/* In this case, i is not incremented because
the short-circuit operator skips the increment. */
if(false & (++i < 100))
System.out.println("this won't be displayed");
System.out.println("if statement executed: " + i); // displays 1
}}
Operator precedence.
Operator precedence.
class Hello{
public static void main(String[] args) {
int a = 2, b=3, c=5;
double result= (a+b)* c++ / --a;
System.out.println(result);
} }

class Hello{
public static void main(String[] args){
double result= 2*5 / 2-4 / 4+8;
System.out.println(result);
} }

class Hello{
public static void main(String[] args){
int a=12, b=8;
double c= ++a /6.0 + b++ % 3;
System.out.println(c);
} }
Operator precedence.
class Hello{ int x = 5;
public static void main(String[] args) { int y = 10;
System.out.println("1 + 2 = " + 1 + 2); int z = ++x * y--;
System.out.println("1 + 2 = " + (1 + 2));
} }

class Hello{ int x = 10;


public static void main(String[] args) int y = x+++x;
{
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
} }

class Hello{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
boolean c = true;
System.out.println(a == b == c);
} }
Operator precedence.
Exercise
class LogicalOpTable {
public static void main(String[] args) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT !
(p)");
p = true; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = true; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = false; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = false; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));}}
Java user Input
Scanner
import java.util.Scanner;
class Hello{
public static void main(String a[]) {
Scanner input=new Scanner(System.in);
System.out.print("please enter your
number :");
int val=input.nextInt();
System.out.println("User entered: "+val);
}
}
import java.util.Scanner;
class Hello{
public static void main(String a[]) {
Scanner input=new Scanner(System.in);
System.out.print("please enter your first number: ");
int num1=input.nextInt();
System.out.print("please enter your second number: ");
int num2=input.nextInt();
System.out.print("Sum= " + (num1 + num2));
}
}
Scanner- with strings
import java.util.Scanner;
class Hello{
public static void main(String a[]) {
Scanner input=new Scanner(System.in);
System.out.print("please enter your string :");
String stringval=input.next();
System.out.println("stringval= "+stringval);
}
}

import java.util.Scanner;
class Hello{
public static void main(String a[]) {
Scanner input=new Scanner(System.in);
System.out.print("please enter your string :");
String stringval=input.nextLine();
System.out.println("stringval= "+stringval);
}
}
Scanner- complete Example
import java.util.Scanner;
class Hello{
public static void main(String a[]) {
Scanner input=new Scanner(System.in);

System.out.println("What is your name?:");


String name=input.nextLine();

System.out.println("How old are you?: ");


int age=input.nextInt();
input.nextLine();

System.out.println("What is your favorite food?: ");


String food=input.nextLine();

System.out.println("your name is "+ name);


System.out.println("you are "+ age + " years old");
System.out.println("you like " +food);
}
}
Scanner- Adding two floats

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.print("Please enter your first number: ");


float num1= Float.parseFloat(scan.nextLine());

System.out.print("Please enter your second number: ");


float num2= Float.parseFloat(scan.nextLine());

System.out.println(num1 + num2);
}
}
Scanner- Advanced
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean ageflag=false;
float floatage=0.0f;
do {
System.out.print("Please enter your age: ");
try {
floatage=Float.parseFloat(scan.nextLine());
ageflag=true;
}
catch (Exception e) {
System.out.println("you did not enter float :( ");
}
} while (ageflag==false);
System.out.println("your age is "+ floatage + " :)");
}
}
printf
class Hello{
public static void main(String[] args) {
boolean booleanval=true;
char x='b';
String name="ahmed";
float num=60.89f;
System.out.printf("the %b is the value for booleanval %n",booleanval);
System.out.printf("the %C is the value for x %n",x);
System.out.printf("the %S is the value for name %n",name);
System.out.printf("the %f is the value for num",num);

}
}
%b represents a boolean value
%C represents a character value
%S represents a String value
%f represents a float value

You might also like