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 modeled 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 exist but instead is simulated at runtime.

2. High-Level Differences with Python

Let’s go over some of the main differences between Java and Python.

2.1. Comments

In Java, there are two different ways to make comments. Single line comments and multi-line comments.

// 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 The maximum value is Integer.MAX_VALUE and the minimum value is Integer.MIN_VALUE
double 8 bytes 3.141592653589793 The maximum value is Double.MAX_VALUE and the minimum value is 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
<, >, <=, >= Relational operators

4.2. Double

Operator Description
+, -, * Basic math operations
/ Real division
= Assignment
== Equivalence (But never do this...)
<, >, <=, >= Relational operators (But beware of equals...)
Math.pow() Power
Math.sqrt() Square Root

4.3. Boolean

Operator Description
== Equality
!= Not equal
! 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 Precedence

The order of operations for Java is (roughly) as follows: