Intro
All the code files for today: HelloWorld.java; JavaVariables0.java; JavaVariables01.java; JavaVariables02.java; JavaVariables03.java; MultipleVariables.java; MultipleVariablesArray.java; MultidimensionalArray.java.
What's this course about?
In this class we will focus on solving real-world problems using Object-Oriented Programming (OOP). We will: process images/video, characterize social networks, play games, compress files, analyze text... and more. While those examples will be fun (I hope), the goal of the course is to develop expertise in core, widely useful computer science concepts.
Administrative stuff
We'll be using Canvas as the main organizational point for the course. Because it's a bit easier for me, and more importantly because they will then persist publicly beyond the term, the actual course notes will be maintained on my own personal web site (as you can see from this page). There will be some cross-referencing within my web site and between Canvas and my web site.
Stuff to look at now on this course web site:
- Who, when, where
- Requirements: homework (short assignments, problem sets), section meetings (programming drills), exams
- Collaboration policies
- Honor code
- Schedule
- Software
Note that SA-0 is out, just to take care of the preliminaries. In particular, our section meetings will start shortly to help you get up to speed on Java ASAP, so and we need your availability info in order to assign sections.
Lecture notes are here to help you, to keep you from having to write down and type in a lot of things that we go over in class. The course notes, however, aren't necessarily complete, and they're also simple, sometimes terse, not necessarily grammatical, etc. They're working notes, and are not intended to take the place of the lectures or the readings. That being said, additions, corrections, and suggestions are welcome.
I will provide source code for the programs we go through in class, but in many cases much of the class will be spent interactively building them up (or tearing them apart). The best way to learn the material is by doing it (that's why we have homework!), and I will try to simulate that process as best I can within the time constraints and so forth.
Java setup
You all know a programming language. For many of you it is Python. For some of you it is Java, or maybe Matlab and C. Learning a new programming language is much easier than learning a new natural language like English. The first hundred pages in the book go over what we will use in Java rather formally with a lot of detail. (And the Oracle site has even more.)
Lectures are an awful way to communicate such detail, and that's not really the goal of this course either — we want to think mostly about problem solving techniques. I expect you to read the Java details sections and come to me or course staff, or post on Slack, with questions. In lecture I will try to give you a framework on which to hang the details, and a way of approaching and thinking about the language, driven by problem-solving applications. At the end of some lectures, I will have a "Java notes" section summarizing some of the key ideas utilized in the examples (without all the gory details).
The software page has some references for Java and the IntelliJ programming environment that we will use. TAs help will be available if you have issues, and your section meeting this week will cover some fundamentals too. So for now, let's just use the remaining class time to walk through some simple example applications, to get acquainted with Java programming and IntelliJ.
We will develop code using IntelliJ, a powerful IDE (integrated development environment) that supplies all sorts of help like code completion (e.g., you type a variable name and a dot and it shows you all of the variables and methods in the object referred to by that variable, with templates describing the parameters of methods) and refactoring (reorganizing your code, e.g., renaming a variable, where only the references in the current scope are changed, unlike a global search-and-replace operation for the variable name). Unlike Python or Matlab and their interpreted environments, if you want to run a command you have to write a whole program to do it. Spend some time playing in IntelliJ. Check out the menus, practice making a few projects, adding code, and running it. We will use IntelliJ a lot this term so the sooner you feel comfortable in this IDE the better.
Our first program
It's pretty much required to have a "Hello world" program to illustrate a programming language. So here we go:
/**
* Standard 'Hello World' first program
*
* @author Everyone who has ever written about programming and Tim Pierson too,
* Dartmouth CS 10, Winter 2025
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
This course will focus on types of data and ways to manipulate them. Here the type of data is a character string, and the manipulation is simply to print it. This all gets wrapped up in some fairly non-beautiful but standard Java boilerplate that you'll see/write again and again (and IntelliJ helps you write with some shortcut keys). In particular, the
To create this from scratch yourself, in IntelliJ select menu option "File | New | Java Class", and give it a name. In this course, I will always leave the Package empty (a topic we'll briefly discuss soon). To save some typing (you'll soon learn that IntelliJ can do lots of coding for you), type main
and press enter. IntelliJ will then expand main
to a full main
method declaration (line 9 above). You can also type sout
in IntelliJ and it will expand to System.out.println()
(highlighted). These two shortcuts (main
and sout
) can save you a lot of typing!
The main program is wrapped up in a
Variables
In Java we declare variables and give them a data type. This is frequently a strange and confusing concept for Python programmers (but the natural way of the world for C and Java programmers). For example, in Python we can write:
x = 5
print(x)
OUTPUT
5
Here we define a variable named x
and assign it a value of 5. We then print that variable on the second line. As expected, the console output shows 5. Notice, however, that we never told Python what kind of data x
holds. Python inferred x
holds integer data when we assigned it an integer value. We can check Python's determination of x
's data type using the type
command as follows
x = 5
print(x)
print(type(x))
OUTPUT
5
<class 'int'>
We see Python inferred x
holds integer data from the <class 'int'>
output. In Python we can also change a variable's data type. For example:
x = 5
print(x)
print(type(x))
x = "Hello World"
print(x)
print(type(x))
OUTPUT
5
<class 'int'>
Hello World
<class 'str'>
In this example, x
was initially an integer, then changed to a String (shown by output <class 'str'>
) after being assigned a value of "Hello World". Python allows variables to change data type, but Java does not.
In Java we will explicitly state what type of data each variable holds and we will not change variables types. Equivalent Java code would be:
public class JavaVariables {
public static void main(String[] args) {
int x;
x = 5;
System.out.println("x = "+x);
}
}
OUTPUT
x = 5
Here we define variable x
, explicitly telling Java that it holds an integer. We then assign x
a value of 5 and print it using System.out.println(x);
. We cannot change x
's type after it has been declared. The following code would cause an exception (error) in Java:
public class JavaVariables03 {
public static void main(String[] args) {
int x;
x = "Hello world";
System.out.println("x = "+x);
}
}
OUTPUT
JavaVariables.java:4: error: incompatible types: String cannot be converted to int
x = "Hello world";
^
1 error
This code causes an exception because x
was declared to hold integer values (int x;
) , but we tried to change it hold a String (x = "Hello World"
). Java expects each variable to be given a data type when it is declared and does not allow variables to later change data type.
Java defines several x
holds one integer). We will discuss how primitive data types are different from objects in the next class.
Data Type | Size | Description |
---|---|---|
byte |
1 byte | Stores whole numbers from -128 to 127 |
short |
2 bytes | Stores whole numbers from -32,768 to 32,767 |
int |
4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long |
8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float |
4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double |
8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean |
1 bit | Stores true or false values |
char |
2 bytes | Stores a single character/letter or ASCII values |
Each primitive data type uses a predefined amount of memory to hold data. For example, int
's are always 4 bytes (32 bits) wide. In CS10 we will primarily use int, double, boolean,
and char.
Arrays
Like most languages, Java has
public class MultipleVariablesArray {
public static void main(String[] args) {
int numberOfScores = 5;
double[] scores = new double[numberOfScores]; //store quiz scores
scores[0] = 10; //zero indexed in Java
scores[1] = 3.2;
scores[2] = 6.5;
scores[3] = 7.8;
scores[4] = 8.8; //valid indices are 0..4
//scores[5] = 9; //error, index out of bounds!
System.out.println(scores);
System.out.print("[");
for (int i= 0; i < numberOfScores-1; i++) {
System.out.print(scores[i] + ", ");
}
System.out.println(scores[numberOfScores-1] + "]");
}
}
OUTPUT
D@3d075dc0
[10.0, 3.2, 6.5, 7.8, 8.8]
Here scores
is defined as an array of 5 doubles (numeric values with a decimal component). Note the square brackets telling Java scores
is an array. Unlike Matlab which indexes arrays starting at index 1, in Java arrays are indexed starting at index 0, meaning the first element in the array is at index 0, not at index 1. Because the first item in the array is at index 0, the last item is the number of items minus 1 (so index 0 ... 4 above). Trying to access an array element less than 0 or greater than or equal to the number of elements results in an exception because we can't assume the memory immediately before or after the contiguous memory block reserved for the array is available for our use.
Printing an array (System.out.println(scores);
) prints a value based on the array's starting memory address (D@3d075dc0
). We will discuss this behavior in detail next class. Until then, if we want to print the array, we must loop over each element in the array and print it. Here we use a
for (int i= 0; i < numberOfScores-1; i++)
C-style for loops are comprised of three components:
- initialization step:
int i = 0;
where a local variable namedi
is declared to hold integer values. Becausei
is defined in the for loop, it goes out of scope when the for loop ends - conditional step:
i < numberOfScores-1;
tells Java to loop while this condition is true, exit the loop when this condition is false - increment step:
i++
adds one toi
each time the loop executes. In this code the first time through the loopx = 0
. The second time through the loopx = 1
and so forth untilx = numberOfScores-1
(e.g.,x
is not less thannumberOfScores-1
) and the loop exits.
Java also allows multidimensional array. To hold quiz scores for multiple students we might write:
public class MultidimensionalArray {
public static void main(String[] args) {
int numberOfStudents = 10;
int numberOfQuizes = 5;
double scores[][] = new double[numberOfStudents][numberOfQuizes];
//set score for student 3 on quiz 2 (assuming students are number 1..n)
scores[2][1] = 9.2; //remember zero-indexing!
//print all scores
int quiz;
for (int student = 0; student < numberOfStudents; student++) {
for (quiz = 0; quiz < numberOfQuizes-1; quiz++) {
System.out.print(scores[student][quiz] + ", ");
}
System.out.println(scores[student][quiz]);
}
}
}
OUTPUT
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 9.2, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
0.0, 0.0, 0.0, 0.0, 0.0
Here the first dimension of the array is the student number and the second dimension is the quiz number. We print all students and all quizzes with a
Java notes
We'll go over these concepts more in the coming few lectures (and the book has more details), but here are some things from the examples, kind of in the order that we build them into the program.
- class definition
- Java is organized around classes, which provide a blueprint for how to organize objects (aka "instances") in terms of data and methods. The name of the class (e.g., "JavaVariables") should match the name of the file stored on disk ("JavaVariables.java").
- public static void main(String args[])
- This is a special method name and type signature telling Java where to begin execution of the program — it is the main method. We'll be ignoring the args parameter in this course.
- syntax characters
- Note that Java uses curly braces, { and }, to mark blocks of code, bodies of classes, etc. Basically wherever we would indent in Python, we also put curly braces in Java (indentation is then optional, but strongly recommended for readability). Each individual statement (not curly braces) ends with a semicolon.
- output
System.out
handles the basic (console-based) output. One method ofSystem.out
is namedprintln
, and it prints a string and then forces a newline. Useprint
(e.g., noln
at the end) to print without automatically adding a newline character. IntelliJ trick: type "sout" and then press enter to complete it to that long phrase, and save some typing. (IntelliJ has a bunch of such shortcuts.) There's alsoSystem.err
which can be used similarly for error messages.- data type
- The type of data a variable holds. For example,
int
holds integer values, whiledouble
holds numeric values with a decimal component. In Java, variables are assigned a data type when the variable is declared (e.g.,int x;
declares variablex
to be an integer. Java will then allocate 4 bytes (the size of an integer) to hold the value.) - String data type
- A string just holds a bunch of characters. Strings hard-coded in the program are enclosed in double quotes "...". Strings support many operations, including concatenation (gluing together) with +, and many types will "automatically" produce a string representation of themselves in that context.
- variable declaration
- Every variable has a data type. That's true in any language — it tells the machine how a bunch of 0s and 1s should be interpreted. In many compiled languages like Java (and C, C++, etc.) that type must be specified in the source code, while in many interpreted languages like Python and Matlab, it is just maintained at run-time. (Some nice languages are able to infer the type at compile time, and kind of have the best of both worlds.)
- variable assignment
- The equals sign is used to assign a value (right-hand side) to a variable (left-hand). Assignment can be done at the same time as the variable is declared (e.g.,
int x = 5;
), and the value can be changed again later (e.g.,x = 6
). - arrays
- A contiguous block of memory used to store multiple data values. Indexed 0 to
size-1
for single dimensional arrays. Java also has multidimensional arrays. - C-style for loop
- A for loop comprised of three parts: initialization, conditional, increment.
- nested loop
- A for loop inside of another for loop.
- comments
- Multi-line comments start with /* and end with */; single-line comments are marked with //. Comments beginning with /** are used to automatically generate HTML-based program documentation — JavaCocs.
- capitalization
- By convention, class names start with capital letters and variables and methods start with lowercase letters. We capitalize each word (camel-case) in a multi-word name ("studentArray").
- math
- Usual mathematical (+, -, etc.) and logical (& &) operators are available. Some math can be combined with assignment, e.g., ++ (increment by 1), += (add right hand side to the variable on left hand side).