CMUQ 15-121 Notes: Intro to Java
- 1. Execution Model
- 2. High-Level Differences with Python
- 3. Primitive Types
- 4. Primitive Operators
- 5. Operator Precendence
1. Execution Model
The main language you have been exposed to up until now is Python.
Python is an interpreted language. That means that your program is read and translated at runtime. You could model this as…
Source Code -> Execution
Java, on the other hand, is a compiled language, meaning that the entire program is analyzed and converted to machine code before it ever runs.
Source Code -> Byte Code -> Execution
In this case, byte code is a low-level programming language designed to provide basic instructions to a simple machine.
2. High-Level Differences with Python
2.1. Comments
Comments in Java are as follows:
// This is a single line comment. // Everything after // on a line is ignored. /* This is a multi-line comment. You can write whatever you want after the /* and it will all be ignored until the comment closes as follows... */ /* You can use the multiline comment on a single line if you want. */
2.2. Errors
In Python, all errors occur at runtime. In Java, some errors occur at compile time and others at runtime.
- Syntax errors (such as mispelling whlie in a while loop) occur at compile time.
- Runtime errors (such as divide by 0) always cause a crash.
- Logical errors (such as writing a program based on a bad idea) don’t trigger errors from Java. (Or any language, really…)
2.3. Static Typing
In Java, you must explicitly declare the type of a variable and you can’t change it later. This is called static typing.
int a = 5; // a is an integer and its initial value is 5 a = 10; // This is no problem a = "Ten"; // This won't work, because a is an integer and you can't change it later
3. Primitive Types
3.1. The Types
Java has eight primitive types, four of which you will use on a regular basis.
Type | Size | Other Note |
---|---|---|
int |
4 bytes | Max value: Integer.MAX_VALUE Min value: Integer.MIN_VALUE |
double |
8 bytes | Max value: Double.MAX_VALUE Min value: Double.MIN_VALUE |
boolean |
unspecified | 2 values: true or false |
char |
2 bytes | |
byte |
1 byte | Not commonly used |
short |
2 bytes | Not commonly used |
long |
8 bytes | Like an int, but longer |
float |
4 bytes | Like a double, but shorter |
3.2. Bits to Values
- n-bits can be used to store \(2^n\) values. (8-bits can store 256 values)
- Unsigned values range from \(0\) to \(2^n -1\)
- Signed integers and longs range from \(-2^{n-1}\) through \(2^{n-1}-1\).
So, 4-byte (32-bit) integers range from -2,147,483,648 to 2,147,483,647.
4. Primitive Operators
4.1. Integer
Operator | Description |
---|---|
+, -, * |
Basic math operations |
/ |
Integer division |
% |
Modulus |
++ |
Increment |
-- |
Decrement |
= |
Assignment |
== |
Equivalence |
4.2. Double
+, -, * |
Basic math operations |
/ |
Real division |
= |
Assignment |
== |
Equivalence (But never do this...) |
Math.pow() |
Power |
Math.sqrt() |
Square Root |
4.3. Boolean
== |
Equality |
!= |
Not equal |
<, >, <=, >= |
Relational operators |
! |
Logical: Not |
&& |
Logical: And |
|| |
Logical: Or |
4.4. Character
Many of the same operators that work on integers work on characters. But make sure you know what you are doing before using them…
5. Operator Precendence
The order of operations for Java is (roughly) as follows:
- Postfix (
expression++
,expression--
) - Unary/Prefix (
++expression
,--expression
,-expression
,!expression
*, /, %
+, -
<, >, <=, >=
==, !=
&&
||
? :
(Don’t worry about this one…)- Assignment (
=
)