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.
Let’s go over some of the main differences between Java and Python.
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. */
In Python, all errors occur at runtime. In Java, some errors occur at compile-time and others at runtime.
Syntax errors (such as misspelling 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…)
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
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 |
For integers, longs, bytes, and shorts, 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\).
Operator | Description |
---|---|
+, -, * |
Basic math operations |
/ |
Integer division |
% |
Modulus |
++ |
Increment |
-- |
Decrement |
= |
Assignment |
== |
Equivalence |
<, >, <=, >= |
Relational operators |
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 |
Operator | Description |
---|---|
== |
Equality |
!= |
Not equal |
! |
Logical: Not |
&& |
Logical: And |
|| |
Logical: Or |
Many of the same operators that work on integers work on characters. But make sure you know what you are doing before using them…
The order of operations for Java is (roughly) as follows:
expression++
, expression--
(Postfix)
++expression
, --expression
, -expression
, !expression
(Unary/Prefix)
*, /, %
+, -
<, >, <=, >=
==, !=
&&
||
? :
(Don’t worry about this one…)
=
(Assignment)