Multimedia Programing
Data Types & Arithmetic Operations
Programming Errors 2
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result
Syntax Errors 3
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
Runtime Errors 4
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
Logic Errors 5
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}
Data Types
• Data type: set of values together with a
set of operations
• Java data types fall into three categories:
• Simple data type (Primitive)
• Complex data type (class, array,...)
6
Primitive Data Types
The primitive data types are the
fundamental data types in Java.
•There are three categories of
primitive data types:
• Integral, which is a data type
that deals with integers, or
numbers without a decimal
part (and characters)
• Floating-point, which is a
data type that deals with
decimal numbers
• Boolean, which is a data type
that deals with logical values
Numerical Data Types
Name Range Storage Size
byte –27 to 27 – 1 (-128 to 127) 8-bit signed
short –215 to 215 – 1 (-32768 to 32767) 16-bit signed
int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed
long –263 to 263 – 1 64-bit signed
(i.e., -9223372036854775808 to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324
Positive range: 8
4.9E-324 to 1.7976931348623157E+308
Non-numeric Literals
String Data Type
The standard Java library provides
a String class type that supports all
operations on text.
Examples:
“Ruba”
“Ahmad”
“1001”
“ 1.5”
10
Variables definition 11
• Variable definition includes two parts
Data type
Variable name ;
int
firstName
double
st_id
String
s1
.
S1M0
.
.
.
.
Naming Conventions
Use meaningful names for variables.
Capitalize the second and subsequent letters, without spaces
between them, e.g. (customerName,customerCreditLimit).
Can use underscore charcater (_) at the beginning or in the middle
or at the end of the variable name.
Can use ($) sign at the beginning of the variable name only
Cannot include any special characters.
Cannot begin with numeric (0..9).
Cannot be a reserved keyword.
Uppercase and lowercase characters since the language is Case-
Sensitive
Converting between String 13
and Numerical Data Types
• Convert numeric values to String
• String.valueOf(....)
• Example:
• Another way( shortcut)
Converting between String 14
and Numerical Data Types
• Convert String values to Integer
• Integer.valueOf(....)
• Example:
• Convert String values to double
• Double.valueOf(....)
• Example:
Basic Mathematical
Operators
• * / % + - are the mathematical operators
• * / % have a higher precedence than + or -
double myVal = a + b % d – c * d / b;
• Is the same as:
double myVal = (a + (b % d)) –
((c * d) / b);