PIC20A Lecture 3 : Primitive data type
In this section, we will try to understand primitve data types.
After the lectures you should know
- different primitive data types
- how to declare primitive data types and assign values
- how to perform operations on them.
You must read the textbook p69,70,71, 72, 75(ex),76-83 or
Identifier
Identifiers are the names of variables. A valid java idenifiers
- must start with a Unicode letter, underscore, or dollar sign ($)
- The other characters, if any, can be a Unicode letter, underscore, dollar sign, or digit.
By Convention : Variable names begin with a lowercase letter,
and class names begin with an uppercase letter.
If a variable name consists of more than one word, the words are joined together,
and each word after the first begins with an uppercase letter,
like this: isVisible, numOfStudents, maxSpeed. The underscore character (_) is acceptable anywhere in a name,
but by convention is used only to separate words in constants
(because constants are all caps by convention and thus cannot be case-delimited).
What is Unicode code?
- It can be used to represent international characters (Like Chinese characters or Russian characters).
- It is representated by , example \u1234 (\uXXXX, here XXXX are 2 hexadecimal numbers)
Question:
Circle the correct identifiers
| myVar | myVar.1 | $myVar | _myVar |
| 1$myVar | _a | int | anInt |
| thing1 | 1thing | ONE-HUNDRED | ONE_HUNDRED |
| something2do | buy1get1free | 2go | one/two |
Primitve Data Types
| Keyword |
Description |
Size |
| (integers) |
byte |
Byte-length integer |
8-bit (-2^7 to 2^7-1) |
short |
Short integer |
16-bit (-2^15 to 2^15-1) |
int |
Integer |
32-bit (-2^31 to 2^31-1) |
long |
Long integer |
64-bit (-2^63 to 2^63-1) |
| (real numbers) |
float |
Single-precision floating point |
32-bit |
double |
Double-precision floating point |
64-bit |
| (other types) |
char |
A single character |
16-bit Unicode character |
boolean |
A boolean value (true
or false) |
true or false |
Integer
You can skip the parts about octal numbers and hexadecimal numbers.
|
Example
|
Description
|
decimal
|
int dec = 28;
|
|
octal
|
int oct = 034;
|
Starts with 0
|
hexadecimal
|
int hex1 = 0x1c, hex2= 0X1c, hex3 = 0x1C, hex4
= 0X1C;
|
start with 0x, case insenstive,
a represents 10, b represents 11...
......
|
Example 1
int x = 27;
int y = 035, z = 011; // octal number 29 , 9
int w = 0x2a, u = 0X33; // hexadecimal 42, 51
Example 2
// compile this program by javac IntTest
// Run this program by java IntTest
/* output is
x is 27
y is 29
z is 9
w is 42
u is 51
*/
public class IntTest {
public static void main(String[] args) {
int x = 27;
int y = 035, z = 011; // octal number 29 , 9
int w = 0x2a, u = 0X33; // hexadecimal 42, 51
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("z is " + z);
System.out.println("w is " + w);
System.out.println("u is " + u);
}
}
Float and Double
Double has 64 bits. Float has 32 bits. Double is more precious than
float.
Example
double x = 25.64;
double y = 2.564e1; // 2.564*10^1
double z = 25.64d; // notice the d
at the end
float s = 25.64f ; // don't forget the
f .
// float s = 25.64 ; // this line will cause a
compiling error
// 25.64 is a double, change it to float
will loss information,
// this is not allowed in java
unless you explicity cast it.
float t = 2.564e1f;
Long
Long is also an integer type.
Example :
long x = 1234567L; // notice the L
at the end
long x = 1234567l; // the last character is
l (lower case of L)not 1.
Operators
Arithmetic Operators
The Java programming language supports various arithmetic operators for
all floating-point and integer numbers.
These operators are + (addition), - (subtraction),
* (multiplication), / (division), and
% (modulo).
The following table summarizes the binary arithmetic operations
in the Java programming language.
| Operator |
Use |
Description |
+ |
op1 + op2 |
Adds op1 and
op2 |
- |
op1 - op2 |
Subtracts op2 from
op1 |
* |
op1 * op2 |
Multiplies op1 by
op2 |
/ |
op1 / op2 |
Divides op1 by
op2 |
% |
op1 % op2 |
Computes the remainder of dividing op1
by op2 |
Here's an example program,
ArithmeticDemo, that defines two integers and two double-precision floating-point
numbers and uses the five arithmetic operators to perform different arithmetic
operations. This program also uses + to concatenate strings.
The arithmetic operations are shown in bold:
public class ArithmeticDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//computing the remainder resulting from dividing numbers
System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
//mixing types
System.out.println("Mixing types...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
}
}
The output from this program is:
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing types...
j + y = 49.22
i * x = 1016.58
Note that when an integer and a floating-point number are used as operands
to a single arithmetic operation, the result is floating point. The integer
is implicitly converted to a floating-point number before the operation takes
place. The following table summarizes the data type returned by the arithmetic
operators, based on the data type of the operands. The necessary conversions
take place before the operation is performed.
The basic rule is :
When the operands are of 2 different
types, the more precies type will be returned.
| Data Type of Result |
Data Type of Operands |
long |
Neither operand is a float or a double
(integer arithmetic); at least one operand is a long
. |
int |
Neither operand is a float or a double
(integer arithmetic); neither operand is a long
. |
double |
At least one operand is a double . |
float |
At least one operand is a float ; neither
operand is a double . |
Other operators
| Operator |
Use |
Equivalent to |
+= |
op1 += op2 |
op1 = op1 + op2 |
-= |
op1 -= op2 |
op1 = op1 - op2 |
*= |
op1 *= op2 |
op1 = op1 * op2 |
/= |
op1 /= op2 |
op1 = op1 / op2 |
%= |
op1 %= op2 |
op1 = op1 % op2 |
Example
int a = 1;
a+=1;
a++;
a--;
++a;
--a;
Character
Character is a single character, it is a 16-bit Unicode.
Example
char ch = 'C'; // a simple character
char lastName = '\u1A34'; (from '\u0000' to '\uFFFF') // unicode
Boolean
boolean a = true;
boolean b = false;
Relational and Conditional Operators
A relational operator compares two values and determines the relationship
between them.
For example, != returns true if the
two operands are unequal.
This table summarizes the relational operators:
| Operator |
Use |
Returns true if |
> |
op1 > op2 |
op1 is greater than op2 |
>= |
op1 >= op2 |
op1 is greater than or equal to op2 |
< |
op1 < op2 |
op1 is less than op2 |
<= |
op1 <= op2 |
op1 is less than or equal to op2 |
== |
op1 == op2 |
op1 and op2 are equal |
!= |
op1 != op2 |
op1 and op2 are not equal |
public class RelationalDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
int k = 42;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" k = " + k);
//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j)); //false
System.out.println(" j > i = " + (j > i)); //true
System.out.println(" k > j = " + (k > j)); //false, they are equal
//greater than or equal to
System.out.println("Greater than or equal to...");
System.out.println(" i >= j = " + (i >= j)); //false
System.out.println(" j >= i = " + (j >= i)); //true
System.out.println(" k >= j = " + (k >= j)); //true
//less than
System.out.println("Less than...");
System.out.println(" i < j = " + (i < j)); //true
System.out.println(" j < i = " + (j < i)); //false
System.out.println(" k < j = " + (k < j)); //false
//less than or equal to
System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j)); //true
System.out.println(" j <= i = " + (j <= i)); //false
System.out.println(" k <= j = " + (k <= j)); //true
//equal to
System.out.println("Equal to...");
System.out.println(" i == j = " + (i == j)); //false
System.out.println(" k == j = " + (k == j)); //true
//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j)); //true
System.out.println(" k != j = " + (k != j)); //false
}
}
Here's the output from this program:
Variable values...
i = 37
j = 42
k = 42
Greater than...
i > j = false
j > i = true
k > j = false
Greater than or equal to...
i >= j = false
j >= i = true
k >= j = true
Less than...
i < j = true
j < i = false
k < j = false
Less than or equal to...
i <= j = true
j <= i = false
k <= j = true
Equal to...
i == j = false
k == j = true
Not equal to...
i != j = true
k != j = false
Conditional operators
|
Use
|
Returns true if
|
|
op1 && op2
|
op1 and op2
are both true
|
|
op1 || op2
|
either op1 or op2
is true
|
|
! op
|
op is false
|