1
Class #03: Java Primitives
Software Design I (CS 120): D. Mathias
Two Types of Types
} Java is object-oriented. Classes define objects such as
the Windows we saw in previous video:
1.
Specified via a class definition
2.
Instantiated using: new ConstructorName()
3.
Written by a programmer (you or someone else) using Java
4.
Known as reference types (because the variable identifier has a value that is a reference address to an object in memory)
} The language has some other things as well:
1.
Built into Java without definition as a class
2.
No constructors used for instantiation
3.
Known as primitive types (because the variable identifier stores the basic value we want directly)
Software Design I (CS 120) 2
Primitive Types in Java
Integer types Real number types Other types (to be covered later)
Software Design I (CS 120) 3
byte
- 128...127
short (2 bytes) -32_678...32_677 int (4 bytes) -2_147_483_648...2_147_483_647 long (8 bytes) -9_223_372_036_854_775_808... 9_223_372_036_854_775_807 float (4 bytes) 7 decimal digits of accuracy double (8 bytes) 15 decimal digits of accuracy boolean (1 byte) true/false char (2 bytes) single character of text
Using Primitive Variables
} Declaring an int (or any
- ther primitive type), uses the
same syntax as before: type name;
} When we want to assign a
value to the variable identifier, we can simply assign a given
- r computed value
Software Design I (CS 120) 4 public class Program { public static void main( String[] args ) { int num1; num1 = 10; int num2 = -77; int num3 = 8 / 4; } }