Week 1
Primitive types
The eight primitive types are: byte, short, int, long, float, double, char and boolean
All other types are non-primitive types
Automatic conversion is only possible if the conversion goes from a more specific to a more
general type
Non-primitive types
String, Scanner and arrays are examples of non-primitive types. Classes created yourself
result in new non-primitive types. For every primitive type there is an associated non-
primitive type. Java supports autoboxing of primitive types, so these types are automatically
converted to the right type
Objects
Objects hold data in their instance variables, the configuration of these instance variables is
the state of the object. Objects do things when methods are called on the object.
Every object (instance of a class) has its own copy of the non-static instance variables
Public/private
Public instance variables can be accessed from anywhere, private ones only within the same
.java file as where they are defined
Method
A method operates on the object we call it on, can use the current instance variables of the
object and can modify the state of the instance variables of the object if it is mutable
Class variables
Class variables are static, final values, such as Math.PI
Static/non-static
Static methods are not associated with an object and cannot access instance variables.
A static variable belongs to and class and not to an object. Static methods can access only
static variables. A not-static method can access both static and non-static variables
Objects and references
Variables of a primitive type store a value and variables of a non-primitive type store a
reference. So, when a non-primitive type is copied, it means it just has the same reference
Immutable objects
Immutable objects never change their state, they hold values just as primitive types.
Non-primitive versions of primitive types are all immutable
Immutable class
When making an immutable class, make all the instance variables final, and let the methods
return new instances instead of change the current instance
Week 2
‘this’ keyword
When instance variables are used in methods and constructors, we implicitly assume they
refer to the current or local instance. We can refer to the current or local instance explicitly
with the keyword this. So, that will be this.instanceVariable = …
Null
The null reference is used to indicate that there is no object. Instance variables with a non-
primitive type always get ‘null’ as the default value. If we try to call a method on a variable
that is null, we will get a ‘NullPointerException’ and the program will stop. When concerned
a variables is ‘null’, you can do a ‘null’ check. It is important to initialize your non-primitive
instance variables in your constructor, or you will run into the risk of getting
‘NullPointerExceptions’. ‘null’ can also be used to return if for example no object was found.
, Random numbers
Since computers words deterministically, generating random numbers is not easy. The idea
to get a pseudo random number is to define an extremely long sequence of numbers, then
start at a point in this sequence, the seed, and use the following numbers in the sequence as
your random values. The seed can be picked randomly by using current time for example.
Errors
- Syntax errors: grammatical error, program will not run
- Type error: error when wrong type is used, program will not run
- Structural error: error concerning the structure of the code, program will not run
- Semicolon on the wrong spot
- Wrong mathematics
- ‘ArrayIndexOutOfBoundException’: the index is not existing in the array
- ‘OutOfMemoryError’: loop goes on too long
- ‘StackOverFlowError’: method refers to itself, memory is stored at stack and fits no more
Types of errors
- Compiler errors: syntax, types, structure
- Runtime errors: something unexpected happens during execution
- Logical errors: program executes fine but gives wrong answer/behaviour
Exception handling
Java has special features incorporated into the language that can pass control from the point
a runtime error was detected to a handler. You can raise an exception with the ‘throw’
keyword. After a ‘throw’ action, the method will stop. You can put ‘throws’ in the method
header to indicate that the method might throw an exception. An exception is an object
Exception types
Exceptions that may be caught:
- IllegalArgumentException
- NumberFormatExcteption
- IllegalStateException
Exceptions that must be caught or declared in the method header using ‘throws’:
- IOException
- FileNotFoundException
Some problems are so severe that you should stop your program if occur, these are errors:
- OutOfMemory Error
- StackOverflowError
‘finally’
‘finally’ can also be used with ‘try’. The ‘finally’ block is always executed as soon as the try
block is exited, no matter for what reason. Try to avoid finally.
Design principles
Exceptions are meant to deal with exceptional situation, they should not be used for regular
control flow. Throwing and handling of exceptions is not optimized.
You should throw early and catch late. Check the input of a method before you start doing
computations. If the input is not okay, throw an exception. If an exception can occur within a
method that is not able to provide a good solution to the problem, the exception should be
thrown to the caller. And never use return in a finally block.
Week 3
Polymorphism
Treating objects of completely different classes in a uniform way.
Les avantages d'acheter des résumés chez Stuvia:
Qualité garantie par les avis des clients
Les clients de Stuvia ont évalués plus de 700 000 résumés. C'est comme ça que vous savez que vous achetez les meilleurs documents.
L’achat facile et rapide
Vous pouvez payer rapidement avec iDeal, carte de crédit ou Stuvia-crédit pour les résumés. Il n'y a pas d'adhésion nécessaire.
Focus sur l’essentiel
Vos camarades écrivent eux-mêmes les notes d’étude, c’est pourquoi les documents sont toujours fiables et à jour. Cela garantit que vous arrivez rapidement au coeur du matériel.
Foire aux questions
Qu'est-ce que j'obtiens en achetant ce document ?
Vous obtenez un PDF, disponible immédiatement après votre achat. Le document acheté est accessible à tout moment, n'importe où et indéfiniment via votre profil.
Garantie de remboursement : comment ça marche ?
Notre garantie de satisfaction garantit que vous trouverez toujours un document d'étude qui vous convient. Vous remplissez un formulaire et notre équipe du service client s'occupe du reste.
Auprès de qui est-ce que j'achète ce résumé ?
Stuvia est une place de marché. Alors, vous n'achetez donc pas ce document chez nous, mais auprès du vendeur LeonVerweij. Stuvia facilite les paiements au vendeur.
Est-ce que j'aurai un abonnement?
Non, vous n'achetez ce résumé que pour 6,99 €. Vous n'êtes lié à rien après votre achat.