Programming: you are telling the hardware what should happen.
Higher level languages à assembly language à machine code à binary code.
Difference in higher-level programming languages:
• C, C++, Haskell, Go … à compiled for a specific (hardware) platform
o Programmer à program code à compiler à machine code à program user
• Java, C#, Scala … à compiled to an intermediate format.
o For Java, this intermediate format is called bytecode (.class files).
o Bytecodes are executed (“played”) by a virtual machine (“video-recorder”), a piece of
software that translates your program to run on the hardware underneath it.
o Java programmer à program code à java compiler à java byte code à JVM
o We have a JVM for Windows, Apple, Unix and Linux so you can write and compile
the code ones and execute it on multiple machines.
o Call to compiler: javac Hello.java; call to virtual machine: java Hello
• Python, Ruby, PHP, … à not compiled at all, a special program (interpreter) directly
executes them.
Imperative programming: program is composed of a list of statements; a statement is a
combination of data and operations.
Object-oriented programming: program consists of classes, objects and a list of statements; the
statements are composed of objects, data and operations.
Floating-point numbers cannot precisely represent all real numbers, precision is limited. A
programmer must be aware of this while programming a program that does computations.
Java type bits These are Java’s eight primitive types.
Booleans 1. boolean String is not a primitive data type, but a reference
2. byte 8 data type.
Integers 3. short 16
4. int 32
5. long 64
Real numbers 6. float 32
7. double 64
Characters 8. char 16
Question: what is the value of a local variable after its declaration?
Answer: it is unassigned, and you will get a compiler error if you try to use it before initializing it.
Assignment operator: =
Arithmetic operators (for integer and floating-point types): +, −, ∗, /, % à be aware of overflow.
The result of the remainder operator is such that (𝑎 / 𝑏 ) ∗ 𝑏 + (𝑎%𝑏) = 𝑎
,Lecture 2: Programming Structures
Rules of thumb, in your program, there should be:
• Structure;
• Cohesion: show parts that logically belong together;
• Separation: use spaces, empty lines, capital/smaller letters;
• Indentation: to show that certain parts belong together;
• Comments: use comments to explain;
• Variable naming: use variable names that clearly express the role of the variable;
Equality operators: == equal to, ! = not equal to. You should not use equality for Strings, also be
careful when testing floats or doubles, because they may be not precise.
Relational operators: <, <=, >, >= à only used for numerical comparison (including characters).
Boolean logical operators: & logical AND, | logical OR, ^ logical XOR, ! logical complement.
&& and || are “lazy” operators: the right-hand side is only evaluated when necessary.
𝑖𝑓, 𝑒𝑙𝑠𝑒 𝑖𝑓 and 𝑒𝑙𝑠𝑒: used for selection
𝑠𝑤𝑖𝑡𝑐ℎ: used for selection à 𝑏𝑟𝑒𝑎𝑘𝑠 are needed, type of condition can be byte, short, char, int,
Character, Byte, Short, Integer, String and enum types.
Character is a class (contains methods such as 𝑖𝑠𝑈𝑝𝑝𝑒𝑟𝐶𝑎𝑠𝑒()) char is a primitive type.
For iteration we can use a 𝑓𝑜𝑟-statement, a 𝑤ℎ𝑖𝑙𝑒-loop or a 𝑑𝑜 … 𝑤ℎ𝑖𝑙𝑒-loop (this loop is always
executed at least once, only after the first execution, the condition is checked).
Lecture 3: Methods
Methods are added to code to prevent code duplication (and duplicating mistakes). A method can
accept parameters and can give a return value.
The parameter types along with the return type form the function’s signature.
A 𝑣𝑜𝑖𝑑 method does not return something, a 𝑟𝑒𝑡𝑢𝑟𝑛 is not obligatory but you can use it.
Precondition: defines the initial state before the calculation in the method.
Postcondition: defines the end state after the calculation in the method.
Overloading: you can use the name of a method more than once, if:
• The number of parameters for both methods is different;
• The number of parameters is equal, but its types are different (or have a different order).
Formal parameters: the identifier used in a method to stand for the value that is passed into the
method by a caller.
Actual parameter: the actual value that is passed into the method by a caller.
A variable declared within a block, will get cleaned (garbage collected) when control leaves the
block à garbage collection is freeing up unused memory.
, Lecture 4: Classes
Class: a software module that is composed of attributes and methods. It is a blueprint for object, it
defines attributes and methods.
Object: an instance of a class, with its own values for the attributes.
Constructor: special method (same name as class) is called when memory needs to be allocated
for the class.
𝑂𝑏𝑗𝑒𝑐𝑡1 == 𝑂𝑏𝑗𝑒𝑐𝑡2: compares memory addresses. To compare two objects, you should write an
𝑒𝑞𝑢𝑎𝑙𝑠 method in a class. The parameter of this 𝑒𝑞𝑢𝑎𝑙𝑠 methods should be of type 𝑂𝑏𝑗𝑒𝑐𝑡.
Then you can check of the 𝑂𝑏𝑗𝑒𝑐𝑡 to which we compare is actually an instance of the class, if so
you can cast 𝑂𝑏𝑗𝑒𝑐𝑡 to an instance of this class.
A static method does not work on an object. This implies:
• No reference to “this”
• No access to attributes (it can access static attributes).
A static method is called through the class: 𝑆𝑡𝑟𝑖𝑛𝑔. 𝑟𝑒𝑎𝑑(𝑥)
The compiler will give a warning when a static method is called through an object: 𝑠. 𝑟𝑒𝑎𝑑(𝑥),
where 𝑠 is of type String.
Private: other classes cannot access it (UML: -). Why private?
• To hide implementation: the consumer of your class cannot know how something is
implemented internally;
• As a protective measure against yourself (the programmer).
• All objects of a type can access a private value.
Public: other classes can access it (UML: +)
A static attribute is common to all objects of a class.
Java passes arguments by value, if you want to change objects who are made in main in other
functions, you should pass the address of the objects to these functions (addresses are passed by
value). Now, the memory addresses can’t be changed, but the objects the addresses points to,
can be changed.
When a method is called, Java will always create a copy of the arguments (actual parameters).
This copy is stored in the local table of the method. For reference types parameters it is the
reference that is copied, not the memory block where the reference points to.
Lecture 5: Debugging
Solution to fix problems in your code:
• Add print statements to see what is going on.
• Make use of the debugger in Eclipse: “playing your program in slow motion”.
o Set breakpoints.
§ A breakpoint on an entire class will suspend execution when something from
the class is called.
§ A breakpoint on a method will suspend execution when method is called.
§ “Watch point”: debugger will suspend execution when an attribute is read
and/or changed (this can be configured).
§ Advanced breakpoints: you can add the properties of a breakpoint.
• After reaching the breakpoint 𝑥 times à stop execution.
• Conditional breakpoints.
o Continue executing: until next breakpoint (or end, in case of no breakpoints).
o Stop the execution.
o Stepping into: look what is happening inside a method call.
o Step over: step over the statement.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller sachakorte. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.88. You're not tied to anything after your purchase.