CMUQ 15-121 Notes: Intro to Java



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. This could be modelled as…

Source Code -> Byte Code -> Execution

In this case, byte code is a low-level programming language designed to provide basic instructions to a virtual machine, which simply means that Java programs are compiled to execute on a type of computer that doesn’t actually exist, but instead is simulated at runtime.

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.

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 to a String

3. Primitive Types

3.1. The Types

Java has eight primitive types, four of which you will use on a regular basis.

Type Size Example Other Note
int 4 bytes 1567 Max value: Integer.MAX_VALUE
Min value: Integer.MIN_VALUE
double 8 bytes 3.141592653589793 Max value: Double.MAX_VALUE
Min value: Double.MIN_VALUE
boolean unspecified true 2 values: true or false
char 2 bytes 'g'
byte 1 byte 125 Not commonly used
short 2 bytes 2036 Not commonly used
long 8 bytes 9223372036854775801 Like an int, but longer
float 4 bytes 3.1415927 Like a double, but less precise

3.2. Bits to Values

For integers, longs, bytes and shorts, n-bits can be used to store \(2^n\) values. (8-bits can store 256 values)

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:

  1. Postfix (expression++, expression--)
  2. Unary/Prefix (++expression, --expression, -expression, !expression
  3. *, /, %
  4. +, -
  5. <, >, <=, >=
  6. ==, !=
  7. &&
  8. ||
  9. ? : (Don’t worry about this one…)
  10. Assignment (=)