Beruflich Dokumente
Kultur Dokumente
1
Contents for Today’s Lecture
2
Identifier, Variable, Constant
• Identifier is a name that we associate with some program
entity (class name, variable name, parameter name, etc.)
• Java Identifier Rule:
– May consist of letters (‘a’ - ‘z’, ‘A’ - ‘Z’), digit characters (‘0’ - ‘9’),
underscore (_) and dollar sign ($)
– Cannot begin with a digit character
• Variable is used to store data in a program
– A variable must be declared with a specific data type
– E.g.: int countDays, $total, _total, total5;
double priceOfItem;
4
Comments
5
Primitive Data Types
• A data type is essentially a name given to certain kind of data.
• Java has two fundamental kinds of data types:
– Primitive Data Types:
• Numeric Data Types
– Integral Data Types (byte, char, short, int, long)
– Floating Point Data Types (float, double)
• Boolean Data Type
– boolean
– Reference Data Types:
• Classes
• Interfaces
• Enums
• String
6
Primitive Data Types
Primitive data types are designed to be used when you are
working with raw data such as integers, floating point numbers,
characters, and booleans.
Java (by Java, I mean, the Java compiler and the Java Virtual
Machine) inherently knows what these data types mean, how
much space they take up, and what can be done with them. You
don’t need to explain anything about them to Java.
Primitive data types are the most basic building blocks of a Java
program. You combine primitive data types together in a class to
build more complicated data types.
7
Reference Data Types
Reference data types, on the other hand, are designed to be used
when you are working with data that has a special and unique
meaning for code that Java has no knowledge of.
For example, if you are developing an application for student
management, you might define entities such as Student, Course,
and Grade. Java has no knowledge of what Student, Course, and
Grade mean. It doesn’t know how much space they take, what
operations they support, or what properties they have. Java will
expect you to define all these things. Once you define them, you
can use them to implement the business logic of your
application. When you write a class, interface, or enum, you
are essentially defining a reference data type.
Reference data types are built by combining primitive data types
and other reference data types.
8
Data Types
Data Type Default Size Range Default Value
boolean 1 bit true or false false
char 2 bytes 0 to 216-1 or '\u0000‘ to '\uffff‘ '\u0000'
i.e. 0 to 65,535
byte 1 byte -27 to 27-1 or -128 to 127 0
short 2 bytes -215 to 215-1 or -32,768 to 32,767 0
int 4 bytes -231 to 231-1 0
long 8 bytes -263 to 263-1 0L
float 4 bytes approximately 0.0f
±3.40282347E+38F
(6-7 significant decimal digits)
double 8 bytes approximately 0.0d
±1.79769313486231570E+308
(15 significant decimal digits)
String - null
9
A word on void
void is a keyword in Java and it means “nothing”.
It is used as a return type of a method to signify that the method
never returns anything.
In that sense, void is a type specification and not a data type in
itself.
That is why, even though you can declare a method as returning
void but you cannot declare a variable of type void.
10
Difference between null and void
null is also a keyword in Java and means “nothing”. However,
null is a value. It is used to signify that a reference variable is
currently not pointing to any object. It is not a data type and so, it
is not possible to declare a variable of type null.
Note that null is a valid value for a reference variable while void
is not. In other words, return null; can be a valid return statement
for a method but return void; is never valid.
A method that declares void as its return type, can either omit the
return statement altogether from its body or have an empty return
statement, i.e., return;
11
Difference between reference variables and
primitive variables
12
Difference between reference variables and primitive variables
public class Student{
int id;
}
13
Uninitialized variables and Default values
Try compiling the following code:
public class TestClass{
static int i;
int y;
public static void main(String[] name){
int p;
}
}
It compiles fine without any issues. It will run fine as well but
will not produce any output.
Java doesn’t have a problem if you have uninitialized variables
as long as you don’t try to use them. That is why the above code
compiles even though the variables have not been initialized.
14
Uninitialized variables and Default values
Now, try the same code with a print statement that prints i and y.
public class TestClass{
static int i;
int y;
public static void main(String[] name){
int p;
System.out.println(i+" "+new TestClass().y);
}
}
15
Uninitialized variables and Default values
Now, try the following code that tries to print p.
public class TestClass{
static int i;
int y;
public static void main(String[] name){
int p;
System.out.println(p);
}
}
17
Type Casting
Conversion mistake
18
Automatic Type Conversion or Widening
Casting
Widening casting is done automatically when
passing a smaller size type to a larger size type:
19
Narrowing Casting
Narrowing casting must be done manually by
placing the type in parentheses in front of the
value:
20
Casting Example
// Demonstrate casts
class MyClass {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
b = (byte) i;
System.out.println(i + " " + b);
i = (int) d;
System.out.println(d + " " + i);
b = (byte) d;
System.out.println(d + " " + b);
}
}
21
Casting Example
This program generates the following output:
257 1
323.142 323
323.142 67
Let’s look at each conversion.
When the value 257 is cast into a byte variable, the result is the
remainder of the division of 257 by 256 (the range of a byte),
which is 1 in this case.
When the d is converted to an int, its fractional component is
lost.
When d is converted to a byte, its fractional component is lost,
and the value is reduced modulo 256, which in this case is 67.
22
Automatic Type Promotion in Expressions
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
24
Automatic Type Promotion in Expressions
byte b = 50;
b = (byte)(b * 2);
25
The Type Promotion Rules
26
The Type Promotion Rules
The following program demonstrates how each value in the
expression gets promoted to match the second argument to each
binary operator:
class Promote{
public static void main(String args[]){
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println("result = " + result);
}
}
27
The Type Promotion Rules
Let’s look closely at the type promotions that occur in this line
from the program:
double result = (f * b) + (i / c) - (d * s);
In the first subexpression, f * b, b is promoted to a float and the
result of the subexpression is float.
Next, in the subexpression i / c, c is promoted to int, and the
result is of type int.
Then, in d * s, the value of s is promoted to double, and the type
of the subexpression is double.
Finally, these three intermediate values, float, int, and double,
are considered. The outcome of float plus an int is a float. Then
the resultant float minus the last double is promoted to
double, which is the type for the final result of the expression.
28
Review Exercises
Exercise 1. Suppose we have the following declarations:
int i = 3, j = 4, k = 5;
float x = 34.5f, y = 12.25f;
29
Review Exercises
Exercise 2. Suppose we have the following declarations:
int m, n, i = 3, j = 4, k = 5;
float v, w, x = 34.5f, y = 12.25f;
Determine the value assigned to the variable in each of the
following assignment statements, or explain why it is not a valid
assignment.
a. v = x / i;
b. n = (int) x / y * i / 2;
c. -x * -y * (i + j) / k
d. m = n + i * j;
e. n = k /(j * i) * x + y;
h. i = i + 1;
i. w = float(x + i);
j. x = x / i / y / j;
30
Programming Exercises
Exercise 1. Write a program to convert centimeters (input) to feet
and inches (output).
1 in = 2.54 cm.
Exercise 2.
Write a program that inputs temperature in degrees Celsius and
prints out the temperature in degrees Fahrenheit. The formula to
convert degrees Celsius to equivalent degrees Fahrenheit is
Fahrenheit = 1.8 × Celsius + 32
Exercise 3. Write a program that accepts a person’s weight and
displays the number of
calories the person needs in one day. A person needs 19 calories
per pound of body weight, so the formula expressed in Java is
calories = bodyWeight * 19;
(Note: We are not distinguishing between genders.)
31
Programming Exercises
32