Java Notes
Java Notes
Edition
Chapter 1 - Fundamental Programming Structures
A static method is just method that can be called using a class without creating a
object from that class
Static methods should be used when you might want to call a function but not
have a instance of a object
The final keyword is used to indicate that a value or method cannot change
For a variable this is intuitive, for a method this means that subclasses cannot
redefine it
The term instance method describes what you think of as a method ie NOT a
static method but a method that is called from a instance of a class
Because the String datatype is actually not really one of the primitive data types -
then you use a capital letter to declare a String var, unlike a int var
jshell provides a REPL (read evaluate print loop) for learning java and checking out
classes - it is a bit like using the console python interpreter, one thing that is nice
is that is has good autocomplete so you can what methods and classes are
available
jshell> generator.
doubles( equals( getClass() h
ashCode() ints( isDeprecated() lo
ngs(
nextBoolean() nextBytes( nextDouble( n
extExponential() nextFloat( nextGaussian( ne
xtInt(
nextLong( notify() notifyAll() s
etSeed( toString() wait(
jshell> generator.
boolean - boolean has no relation to 1/0 for true/false unlike other languages
The only other thing is that is now rare to use the float data type - you should
nearly always use the double data type
When declaring variables you can either declare the type of the variable or use
the var keyword, you should only use the var keyword when the type is
completely obvious
The term identifier is used to describe the name you have given to a variable or
method ect
Before you do anything with a variable it must be initialized - see below as to what
I mean - this is an easy pitfall
int count;
count++; // Error—uses an uninitialized variable
int count;
if (total == 0) {
count = 0;
} else {
count++; // Error—count might not be initialized
}
as already mentioned the final keyword is used for final values - for variables a
final variable is of course a constant (see below) - constants should be capitalized
and use underscores
// calling it
Weekday startDay = [Link];
Also if you divide two integers then integer division will be used - discarding any
remainder
When an operator combines operands of different number types, the numbers are
automatically converted to a common type before they are combined. - usually
the conversion is intuaitve - for example dividing a int and a double - both
numbers will be converted to doubles. Java converts so that there is no loss of
information
instead you should use the .equals() operation, the only time you might want to do
this is check i the string is null - you should always call . equals on the string
because that way you wont get any errors trying to call a method on a null pointer
there is a weird thing called a fall through switch statement where if the 1st case
works the switch statemnt keeps going unless it hits a yield or break
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
// No break statement here, so execution will
fall through to the next case
case 4:
[Link]("Thursday");
// No break statement here, so execution will
fall through to the next case
case 5:
[Link]("Friday");
yield 5
arrays are fixed size but you can use ArrayList which is [Link] and provides a
resizeable array
there is a slighty nicer syntax for decalring array lists as seen in the final example
there is some synatic sugar to make for loops a big nicer when you just want th
element:
int sum = 0;
for (int n : numbers) {
sum += n;
}
[Link](names);
[Link](friends);
for some reason if you just put all your code in one class with the static main
method (and add other methods to that class, then all those methods but be static
as well) - to be clear the class with the main method entrance point must have all
its methods be static