Main Method: the line that goes directly after the class line (public class <name>):
public static void main(String[] args) // you’ll learn what each of these things are
later. for now, just memorize it!
{
… // your code goes here!
}
What is a method?
A set of instructions that are named and have a specific purpose. For example, the
main method is the set of instructions used most for code to go in. For the sake of
this unit, all methods will start with public static void <name of method>, { … }.
You’ll learn what each of the words mean later!
Example: public static void printHello() // the function of this method is to print out
the word “Hello”
What are comments?
Exactly what the name is! They are created by typing two backslashes, and
anything after them in the same line is a comment. This won’t impact how your
code runs whatsoever, so use it for organization when needed.
How do you call a method?
Use its name. For example: printHello(); // make sure that you include the () and
the ; (semicolon), or it will result in an error.
What are the datatypes?
1) int (integer): Numeric type for storing distinct counts of things. They can be
negative.
2) double (decimal): Numeric type for storing decimal information (ex. 3.14159,
dollars, etc.)
3) boolean (true/false): a data type that represents yes/no or true/false
questions. You can only have one of two values.
4) String (text): Common type used to represent sequence of characters or text
(closed in double quotes, and always use a capital ‘S’ in String)
,Int, double, and boolean are primitives, which means that they start with a lower-
case letter and are small and fast.
Strings are nonprimitives, and they start with uppercase letters.
What are expressions?
Using basic math operators on datatypes where it makes sense. * + - /
Example: (4+7-1)/2)). Yes, you can solve math equations with code!
Applying PEMDAS to code:
If you haven’t heard of PEMDAS, it’s a math concept that shows you the order of
solving parts of an equation. It stands for: Parentheses, Exponent, Multiplication,
Division, Addition, and Subtraction. In code, we have something very similar. It’s
called P(MMD)(AS)! This one stands for: Parentheses, (Modulus, Multiplication,
Division), (Addition, Subtraction). The grouped parts have equal importance, so do
them in the order of what comes first in the equation.
What is truncation?
When Java throws away the decimal part of, well, a decimal. An example might
make this clearer: Java throws away the ‘.14’ part of ‘3.14’. The concept also applies
to integers. If you divide two integers, you won’t get a decimal, contrary to what
you might expect. You will get another integer. (example: prints out 9, not
9.9)
Datatype Operations:
All operations on 2 integers result in an int.
If you have an int operator int, the result is an int.
If you have a double operator double, the result is a double.
If you have double operator int, the result is a double.
If you have <…> operator String, the result is a String.
Printing:
Double quotes are a type of special character. By this, I don’t mean the double
quotes you would use when printing, like: System.out.print(“hello”); , I mean literally
printing out double quotes, so when you run a program, it will print out a message
with “” visibly on it. To print “”, put in a backslash \ before the “, and make sure that
it is, in fact, a backslash. Ex. “\”hello\””
Guide to Special Characters:
New line: \n
Tab: \t
Backslash: \\
Double quotes: \” \”
, Printing Numbers and Quotes:
Example: System.out.println(“hi” + 2 + 3); would print out ‘hi23’, not ‘hi5’, like you
might expect. This is because anything that comes after a String will be written as
though it is also a string, so numbers go one after another, and are not added
unless they are in parentheses. Another example of this would be:
System.out.println(“My age is “ + 5 + 5); What do you think it would print out?
When you have the datatype ‘double’ before your variable name, then set it equal
to an equation. Because the variable itself has the datatype double, whatever result
you get from the equation will have a .0 at the end. On the other hand, if you have
the datatype ‘int’ before a variable name, but one of the parts of your equation is a
double/decimal, then the result will be a double, even though the datatype of the
variable is an int. Ex. int i = 5 + .0 -> 5 + 5.0 -> 10.0
What is concatenation?
Putting two things together with quotes together. You may only use the + operator
for concatenation. Ex. System.out.print(“hi,” + “ how are you?”);
What is modulus?
Mod, or modulus, is when you get the remainder after division. Ex. 8 % 3 is 2, but
not because 8/3 = 2, because there are two 3s in 8, but you’re left with a remainder
of 2, because 3 > 2 and you can’t divide 8 by 3 three times. The % sign is always
used for mod, not the division sign. Mod can have a result of 0. Ex. 8 % 4 = 0,
because 4 goes into 8 two times perfect, without a remainder.
How is mod useful?
int r = 8 % 2 // any operations with % 2 can only result in 0 or 1 (even
numbers = 0, odd numbers = 1)
int r = n % 7 // n has to be a multiple of 7 when mod = 0
when d is a number, int i = (d / 10) % 10; will get you the second to last digit
of d
when d is a number, int i = (d / 100) % 10; will get you the first digit of d