6 Week Coding Camp
6 Week Coding Camp
6 Week Coding Camp
CODING CAMP
What we will be going
over in the course
When it is declared you can also give it a value by using the = sign. That’s optional
int score = 1;
The keyword final can be placed in front of the type to make it constant, meaning it can’t
change.
final doublePI=3.14
Naming variables
When choosing name for variable there are certain
rules that must be followed
Should start with alphabetic character and can
contain numbers, letters and underscores.
Cannot contain spaces
Cannot use reserved keywords such as static
Should be descriptive name
Should use java convention which is lowercase letter
and uppercase each word (camel case) i.e myVariable
Other conventions are snake case used in some other
languages i.e my_variable
QUIZ TIME
Input with Java
Variables can also be given input from stdin or standard input. This is taking input directly
from the keyboard
This is done using the Scanner class or Console class. Typically will see this done with the
scanner class
Expressions and Assignment
Statements
Assignment statements can initialize or change values stored in variables using the
assignment operator (= sign)
Adding 1 to a variable (x = x + 1). Essentially says to take the value of the original value
add 1 and set the value equal to expression
Operators
The same operators in math are available here. Addition
(+), Subtraction (-), Division (/). The multiplication
operator uses (*). For exponents typically (^) is used but
in Java this does not mean taking the exponent but
instead xor which we’ll cover later.
Arithmetic expressions will evaluate to type int or
double. If the expression only consists of ints then the
final value will be an int. If used with a double it will
evaluate to a double
This is because it defaults to the most precise data type
which is a double.
Operators take precedence just like PEMDAS in math
and the same rules apply i.e dividing by 0 is not allowed
and will throw an error.
Using these expressions you can create compound
expressions, mixing variables and numbers to do
powerful things.
Modulo Operator
The (%) symbol is referred to as the modulus
operator. When dividing by a number you
sometimes get the remainder if you think back to
when you first learned division. 10 % 3 yields that
remainder portion so the answer would be 1.
Although it’s referred to as the modulo operator it is
actually used as the remainder. Modulo uses
Euclidian division and so a true modulo would get
different values for negative numbers.
However, for positive numbers remainder and
modulo yield the same values.
Compound assignment
operators
These are shortcuts that do math in one step.
Can be used with all of the previous operators discussed before
X+=1 essentially is equivalent to x = x + 1. This can be done with the (-), the (*), the (/), or the
(%)
Since incrementing and decrementing by 1 is so common there is a special shortcut for that. X++
or ++x and x– or –x. This only works for these operations and only allows you to change the
value by 1.
These are called postfix and prefix. The difference is where the value gets updated. Only postfix
will be covered
Casting
Type casting or casting allows for converting between data types
The format looks like (int) or (double) which is placed before the
expression. For example (double) 1 / 3 will evaluate to a double
Ints are stored in 4 bytes so they range from -2^31 -> 2^31 – 1.
Stored in values in java Integer.MIN_VALUE and
Integer.MAX_VALUE. (All caps indicates constants)
When casting from doubles to ints the values get truncated because
ints are not as precise. So 1.8 casted to an int would be 1. If an
expression evaluates to an int value outside the range it could also
cause an overflow issue so be careful