Lecture 2 Primitive Data types in Java
Lecture 2 Primitive Data types in Java
IN2203
Fundamentals of
Programming (Java)
Instructor
Dr. Muhammad Waqar
[email protected]
An Overview of Lecture 1
We discussed the following in lecture1
Tests
• Computer Hardware, software and languages
• Software development model
• A comparison of Java with other languages
• Advantages of Java
• Using NetBeans to compile java programs
• Discussed different parts of a simple program
1
5/24/2020
A Simple Program
public class Example2 {
Tests
public static void main(String args[]) {
System.out.println("John Smith");
}
}
Output of this program is
John Smith
Practice Problems
1- Write a program which prints your name vertically
Tests
2- Write a program that displays your name on two lines inside a
rectangle of asterisks as
follows:
******************
* First name *
* Surname *
******************
Make sure the asterisks are properly aligned. To do this you will need
to insert spaces (blanks) in the right place.
2
5/24/2020
3
5/24/2020
• Integers: This group includes which are for whole valued numbers
• Floating point numbers: This group includes float and double which
represent numbers with fractional precision
Integers
• Java has four integer types: byte, int, short and long
• All of these are signed, positive and Tests
negative values
• Java does not support positive only, unsigned integers
4
5/24/2020
byte b, c;
short s;
short t;
10
Integer
• The most commonly used integer type is int. It is a signed 32-bit type
that has a range from –2,147,483,648 to 2,147,483,647.
Tests
• In addition to other uses, variables of type int are commonly
employed to control loops and to index arrays.
• When byte and short values are used in an expression they are
promoted to int when the expression is evaluated. Therefore, int is
often the best choice when an integer is needed.
int a, b;
10
5
5/24/2020
11
long
• The long is a signed 64-bit type and is useful for those occasions
Tests
where an int type is not large enough to hold the desired value.
• The range of a long is quite large. This makes it useful when big,
whole numbers are needed.
11
12
Variables
• The variable is the basic unit of storage in a Java program. A variable
is defined by the combination of an identifier, a type, and an optional
initializer. Tests
12
6
5/24/2020
13
Variables
type identifier = value, identifier = value ;
13
14
Variables Examples
int a, b, c; // declares three ints, a, b, and c.
Tests
a=5; // assigns a value 5 to already declared int variable a (initiliazation)
14
7
5/24/2020
15
int number = 5;
System.out.println("Square of 5 is: " + (number*number));
}
}
15
16
16
8
5/24/2020
17
Program Code
public class Example {
public static void main(String args[]) {
Tests
int hours, minutes, total_minutes, total_seconds;
hours = 3;
minutes = 25;
total_minutes = hours * 60 + minutes;
total_seconds = total_minutes * 60;
System.out.println("3 hours and 25 minutes = : " + total_seconds + "
seconds");
}
}
17
18
Floating-Point Types
• Floating-point numbers, also known as real numbers, are used when
evaluating expressions that require fractional precision.
• Calculations such as square root, sineTests
and cosine, result in a value
whose precision requires a floating-point type.
18
9
5/24/2020
19
A simple program
Write a program which converts 25 miles into kilometers.
Tests
Steps:
1- Define a double variable ‘miles’ which has value equal to 25. (stores
miles)
2- Dynamically initialize another double variable ‘Kilo_meters’ by
converting 25 miles into kilometers. (Hint: 1 mile = 1.609 km)
3- Print value of Kilo_meters
19
20
Code
public class conversion {
Tests
public static void main(String args[]) {
}
}
20
10
5/24/2020
21
21
22
Code
// Compute the area of a circle.
Tests
class Area {
public static void main(String args[]) {
double pi, radius, area;
radius = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
area = pi * radius * radius; // compute area
System.out.println("Area of circle is: " + area);
22
11
5/24/2020
23
Characters
• In Java, the data type used to store characters is char.
• char in Java is not the same as char in C or C++.
• In C/C++, char is 8 bits wide. Tests
23
24
24
12
5/24/2020
25
Exercise Program
In previous program you printed ‘X’ by using its Unicode. Now try to get
this out put i.e.
Tests
ch1 and ch2: X x
But this time X must be saved in ch1 as character using single quotes
and x (lowercase) must be saved in ch2 using its Unicode.
25
26
booleans
• Java has a primitive type, called boolean, for logical values.
• It can have only one of two possible values, true or false.
Tests
• This is the type returned by all relational operators, as in the case of
a < b.
• boolean is also the type required by the conditional expressions that
govern the control statements such as if and for.
• The true literal in Java does not equal 1, nor does the false literal
equal 0.
• Below are examples of boolean variables.
bolean a = true;
boolean b =false;
26
13
5/24/2020
27
Sample Program
Public class booltest{
public static void main(String args[]) {
int number1 = 10; Tests
27
28
Strings
• String literals in Java are specified like they are in most other
languages—by enclosing a sequence of characters between a pair of
double quotes. Tests
28
14
5/24/2020
29
Escape sequences
• Escape sequences are used for special character insertion or for
special action
Tests
29
30
• \r and \n behave same in most windows platforms i.e. any text after
these will move to next line
30
15
5/24/2020
31
Practice programs
• Write a program that displays your name vertically. Use print()
statement only once. (Hine: use escape sequence)
Tests
31
32
32
16
5/24/2020
33
33
34
34
17
5/24/2020
35
35
36
• float to double
36
18
5/24/2020
37
37
38
38
19
5/24/2020
39
Example of Casting
public class int_to_byte {
Tests
public static void main(String args[]) {
int integer1 = 100;
byte byte1 = (byte) integer1;
System.out.println("Value of integer and byte is " + integer1 + " " +
byte1);
}
}
39
40
Truncation
• A different type of conversion will occur when a floating-point value is
assigned to an integer type: truncation.
• Since integers do not have fractional components,
Tests when a floating-
point value is assigned to an integer type, the fractional component is
lost.
• For example, if the value 1.23 is assigned to an integer, the resulting
value will simply be 1.The 0.23 will have been truncated.
• If the size of the whole number component is too large to fit into the
target integer type, then that value will be reduced modulo the target
type’s range.
40
20
5/24/2020
41
Truncation example
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257; Tests
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
41
42
42
21
5/24/2020
43
• In this case when the expression was evaluated, the result has also
been promoted to int. Thus, the result of the expression is now of type
int, which cannot be assigned to a byte without the use of a cast.
• This is true even if, as in this particular case, the value being
assigned would still fit in the target type.
• If you still want to save result into a byte, casting would be required.
43
44
44
22
5/24/2020
45
45
Thank You
46
23