Vous êtes sur la page 1sur 31

CSE 110: Programming

Language I

Matin Saad Abdullah


mabdullah@bracuniversity.net
UB 1222
Data types and variables

Data types - simple to complex


int - for integers or whole numbers
double - for numbers with fractional parts
String - for text
Button - a button on a GUI
Point - for representing points in a plane

Variables store data in named


locations
every variable must have a declared type
Primitive types

Java has eight primitive types:


byte, short, int, long, float, double, char,
boolean

Primitive types have literal values


and can be manipulated with built-
in operators. E.g.
2 + 3
Eight Simple Data Types

byte, short, char, int, long, float, and


double
char is 16 bit unsigned
Unicode, not ASCII (range 0 - 65536)
fully international character set, represent all
of the characters found in all human languages

example:
char ch = X ;
Integer Types

integer(no unsigned types)


8-bit byte
16-bit short
32-bit int
64-bit long
Ranges

Name Width Range


byte 8 -128 to 127
short 16 -32,768 to 32,767
float 32 3.4e-038 to
3.4e+038
double 64 1.7e-308 to
1.7e+308
Floating - Point Types

Floating Point type


float 32-bit 3.4 e-038 to 3.4
e+038
double 64-bit 1.7 e-308 to 1.7
e+308
Boolean Types

standard type for only logical values


has values true and false
no conversion between int and
boolean
Declaring Variables

All variables must be declared before can


be used
type identifier [= value] ;

int count, total;


String sentence;
boolean done;
Initializing Variables

Initializing is mandatory before accessing a


variable
int count = 10, total = 0;
String sentence = "Hello there.";
boolean done = false;
Floating point literals in Java
default to double
Initial Java Program

public class Test1 {


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

// StringVsId.java
// contrast strings and identifiers
class StringVsId {
public static void main(String[] args) {
String hello = "Hello, world!";
String stringVary;
stringVary = hello;
System.out.println(stringVary);
stringVary = "hello";
System.out.println(stringVary);
}
}
Scope of variables

Java allows variables to be declared within


a block
A block defines Scope
Each time stating a new block, new scope created
Generally two types of scopes
defined by classes & methods
Variables declared inside a scope are not
visible outside.
Scope can be nested.
Scope of variables (cont..)

1. public class Scope {


2. public static void main(String[] args) {
3. int x;
4. x = 10;
5. {
6. int y = 20; // known only in this block
7. System.out.println (x + y);
8. }
9. y = 100; // ERROR !! y not known here
10. x = 200; // x is still known here
11. }
12.}
Scope of variables (cont..)

1. public class Scope2 {


2. public static void main(String[] args) {
3. int x = 0;
4. {
5. int y = 100;
6. System.out.println (x + y);
7. y = 10;
8. int y = 200; // Compile Error- already defined
9. }
10. int y ; // creates new variables y
11. }
12.}
Type Conversion

Javas automatic conversion occurs when -


Two types are compatible
e.g numeric types(int, float) are not compatible with
char or boolean
char and boolean are not compatible with each
other.
Destination type as larger than source type
e.g. int type is always large enough to hold byte
type
This type of conversion is widening
conversion
Incompatible Type Casting

If it necessary to assign an int to a byte


This conversion will not perform automatically,
as byte is smaller than int
This type of conversion is called narrowing
conversion

A cast has general form


(target-type) value
Type casting (example)

byte b;
int i = 257; // i = 257
(100000001)2
double d = 323.142; // 323 (101000011)2
b = (byte) i;
System.out.println(b); // b = 1
i = (int) d;
System.out.println(i); // i = 323
b = (byte) d;
System.out.println(b); // b = 67 (01000011)2
Arithmetic operations

Operation Symbol in C
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
In Mathematics In C
a+b a+b
A-b A-b
ab a*b
ab a/g
Integer arithmetic

If the operands are integer, then integer


arithmetic is performed to yield an integer.
For example,
5+3 gives 8
5*3 gives 15
5-3 gives 2
In integer division, fractional part is deleted
5/4 gives 1
4/5 gives 0
-7/2 gives -3
Floating-point arithmetic

If the operands are floating-point, then


floating-point arithmetic is performed to
yield a floating-point value.
5.+3. yields 8.
5.*3. yields 15.
5.-3. yields 2.
Real division is similar to ordinary division
5./4. yields 1.25
4./5. yields 0.8
-7./2. yields -3.5
Arithmetic operations

Mixed mode operation


5.0-3 gives 2.
4./5 gives 0.8
4/5. gives 0.8
4*3. gives 12.
The remainder operator (%) returns the
integer remainder of the result of dividing
its first operand by its second. For example
the value of 7%2 is 1
299%100 is 99
Assignment statements

An assignment statement has the following


form:
variable=expression;
Example:
px=2+3;
sal=5/2-(3*5/2)/2; yes
a=b+c;
b=b+b+b;
b+c=a; No
a+b=c+d;
Why Remainder

The number of hours, minutes, and


seconds in a given number of seconds. For
example, how many hours, minutes, and
seconds are there in 10,000 seconds? First
let us develop some basic identities:
60 seconds = 1 minute
60 minutes = 1 hour
This means that one hour has 6060
seconds, or 3,600 seconds.
How many hours, minutes and seconds in
10000 seconds.
String Concatenation

Strings can be spliced together (a


process known as concatenation)
using the + operator.
The compiler and interpreter will
automatically convert a primitive
type into a human-readable string
when required.
+ Operator is Overloaded

When both operands are numbers, +


performs familiar arithmetic addition.
When one or both of its operands is a
String, + converts the non-String
operand to a String if necessary and
then performs string concatenation.
Math Class

Very useful. Provides many methods


in a pre-built class.
The following are available in the
Math class
Trigonometry functions:
sin, cos, tan, acos, atan, asin
Exponent Methods

exp raise e to a power


sqrt returns the square root
pow raise a number to a power
log natural log of a number
Rounding in Math Class

ceil round up to nearest integer


floor round down to nearest integer
Helpful Math

random Returns a random number


greater than or equal to 0.0 and less
than 1.0
abs return absolute value of a
number
min return minimum of two
numbers
max return max of two numbers
Mathematical Library
Functions
In Mathematics In Java
? Math.sqrt(x)
? Math.abs(y)
? Math.pow(x,y)
? Math.exp(x)
sinx Math.sin(x)
cosx Math.cos(x)
tany Math.tan(y)
? Math.log(x)
? Math.log10(y) where x and y are
of type double.

Vous aimerez peut-être aussi