Lessons 1.3 in both CodeHS and CS Awesome cover many of these same ideas, but these notes go into a bit more formal detail. There are good practice problems in both of those tools!
At the lowest level, all informaton (or data) on a computer is just a string of 0’s and 1’s. It is very important that a string of 0’s or 1’s will be interpreted correctly - does it represent a number? A word? A command?
One of the ways our computer programs know how to interpret data in the context of a programming language is by the data type of the data, which is set in the program. This defines the specific type of information held in that piece of data.
In a program, there are three things that have types:
2
or "abc"
that are used only once and represent a value of a certain type directly.A variable is basically a name that refers to a spot in your computer’s memory where you can hold data of a specific type. The value can be changed later.
In order to use a variable in java, you need to declare the variable by typing first the type of the variable, then its name, then an OPTIONAL section where you set its initial value with an equals sign (called initializing the variable). Once a variable has been named an initialized, you can change its value by assigning a new value to it, for example with the code a = 3;
which would change the value of the variable a
to 3.
To make the variable a constant, so it cannot be changed, you use the keyword final first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int numApples;
// makes a new variable named numApples that can hold an integer.
// but it is not currently initialized
numApples = 3; // now it is initialized to 3!
int numMangoes = 17; // create and initialized in one line!
final int numOranges = 6;
// makes a new variable called numOranges that can hold an integer.
// initializes the value to 6
// declares that we can't change it.
numApples = 5; // assigns the value 5 to the already existing numApples
numMangoes = numApples; //now numMangoes is 5 too!
numOranges = 7; // ERROR! Can't change a final value.
int numApples = 9;
// ERROR! numApples already exists! Can't declare again with int.
numApples = 1.3;
// ERROR! numApples is an integer, and can't hold decimals.
There are some rules when it comes to naming variables in java (most languages have similar rules)
The first character in the name must be a letter, $
, or _
.
the rest of the name can only be letters, numbers, or _
words used elsewhere in the programming language are called reserved words, and also can’t be used. This includes words like int
, public
, class
, etc - words you see inside the program itself. You will get an error if you accidentally try to use a reserved word - just change the name in that case.
There are also some conventions that we usually follow when naming a variable. These won’t create an error - java itself doesn’t care if you follow these - but it improves readability and is strongly encouraged!
numApples
, if it represents the player’s height, call it playerHeight
. It should almost read like natural language when you read it. “If the number of apples is more than 4” becomes if (numApples > 4)
.If your variable has multiple words, use Camel Case, where the first word starts with a lowercase letter and every subsequent word is uppercase. Avoid using underscores, even though they are allowed. Use numApples
, not NumApples
, or numapples
, or num_apples
.
final int PI = 3.14159262;
Be careful - once you name a variable, you have to use EXACTLY that name, including lowercase and uppercase, for the rest of the program!
General Type | java-specific usage | Description |
---|---|---|
Boolean | boolean isTired = false; |
The value of a boolean is either true or false . These are most commonly used for comparison and decision-making. |
Integer |
int count = 11; or long n2 = 32039403094;
|
Holds a positive or negative whole integer (whole number). You would use the long type if you might need to hold values above 2 billion or so (or below negative 2 billion), but int is more common, as such large integers are rarely needed. |
Floating Point value |
float f = 1.230; or double d = 2.3e5;
|
Holds a decimal value, or a value in scientific notation. In this course we will usually use the double form as it is more accurate (but takes up more space). Because there is always rounding and therefore possible errors involved with decimal math, it is preferred to use integers when possible. |
Character | char c = 'a'; |
A character is a single piece of text data, ready to print to the screen, such as the character 'a' or '8' or 'â' or '안' . In java, a character (or char ) literal is always surrounded by SINGLE quotes. Not every possible character is supported by java - for example, emojis are not natively supported. |
String | String s = "abc"; |
A string is a series of characters joined together. In java, we use the String type to hold these values (notice the capital, very important!) and we use double quotes to surround a string literal. |
What type of data is the value 234
in java?
This is an integer, declared with int
or long
(usually int
)
You want to write the code j = "bob"
. What data type must j
be declared as for this to not give an error?
String
The following java code gives an error. Why?
1
2
String j;
j = 123;
j
is a String but you are trying to assign it an integer
What does an int
literal look like?
A number with no decimal point or scientific notation, like 3
, 8708
, or -34021
What does a double
literal look like?
A number a decimal point or scientific notation, like 3.2
, -1.2
, or 3.2e5
What does a String
literal look like?
Any number of characters inside double quotes. Like "hi"
, or "123!!!"
, or "Have a nice day!"
The following java code gives an error. Why?
1
2
int j;
j = "123";
j is an integer, but you are assigning it a string.
The following code gives an error. Why?
1
2
int numCookies = 3;
int numCookies = 2;
the first line declares numCookies by using the type int
. It cannot be declared again. This programmer could drop the keyword int
and simply do numCookies = 2;
on the third line to avoid the error.
The following code gives an error. Why?
1
string theWord="cookie";
There is no type string
- you need to capitalize it to make a String
!
Write the line of code to declare (but not initialize) a variable named “count” that can hold a whole number.
1
int count;
Write the line of code to declare and initialize a variable that holds a first name, in camel case. Initialize it to “David”.
1
String firstName = "David";
Write the line of code that creates a constant named MAXSIZE with the value 100;
1
final int MAXSIZE = 100;
Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for java on the back.