w01 Intro Curs
w01 Intro Curs
Introduction module
Introduction
Your expectations
Introduction
Our expectations
• ask questions
• really, ask anything
• work on exercises
How development works?
Key takeaway:
Continue breaking down the problem until you have manageable parts!
How development works?
Example:
Key takeaway:
Continue splitting the problem until you have manageable parts.
3. Compute delta
4. Check if the equation has real solutions
5. Compute the first solution
6. Print the first solution
7. Check if there is a second solution
8. If yes, print the 2nd solution
How development works?
Problem solving
This sequence of steps that need to be taken to solve a problem is called an..
Algorithm!
Practice
• Source code: human readable text, respecting programming language’s syntax, stored in
text files (.java), describes application’s logic
• Compiler (javac): validates and translates source code to compiled code (‘bytecode’)
• Bytecode: stored in binary files (aka “not text”; like .class/.jar), can be executed by a Java
Virtual Machine (JVM), multi-platform (runs on: Windows/MacOS/Linux..)
● JRE?
● JDK??
● JVM???
● ...bytecode?
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!") ;
}
}
Gotchas:
● Don’t forget the semicolon at the end of the 3rd line
● Make sure that the file is called HelloWorld.java (same name as class + .java extension)
General structure of Java code
Java files may contain a few other kinds of code (like comments, annotations..).
Hello World - code structure
HelloWorld program: it consists of just one file (HelloWorld.java), containing one class:
} //..end method
} //..end class
Variables
Declaration: Examples:
<type> variableName; String myName;
int myAge;
Assignment:
variableName = <value>; myName = "Andrei";
myAge = 20;
myAge = myAge + 1; //reassignment
Declaration combined with assignment:
double pi = 3.14;
<type> variableName = <value>;
int answer = 42;
Variables - declaration vs assignment
Rules:
- declaration: a variable can be declared only once; the name of a variable
must be unique (in its scope)
- assignment:
- regular variables can be re-assigned multiple times
- constants: variables declared as ‘final’ can be assigned only once
Examples:
int a; //declared, not initialized final int c; //declared final, not initialized
int b=1; //declared and initialized c = 1; //can be assigned (once!)
final int d = 1; //declared and initialized
int a; //-> error: 'a' already declared
a = 1; //can be reassigned.. c = 2; //-> error: cannot reassign final var 'c'
a = b+1; //..multiple times d = 2; //-> error: cannot reassign final var 'd'
Variables - scope
● For now, we’ll focus on the local scope (we’ll see later that there is also a
global scope)
public static void main(String[] args){ There are two blocks of code here: the
int a = 1; method body and the small nested block
{
int b = 2; - Variable “a” is defined within the
} method body, so it’s visible
System.out.println(a); throughout the whole method body
System.out.println(b); //error! - Variable “b”, on the other hand, is
} visible only in its small block
//variables should start with lowercase (use camel case for multiple words)
int someVariable;
String anotherVariable = "text";
boolean isEnabled = true; //usual for boolean vars to start with ‘is…’
//method names should start with lowercase (and use camel case); same for their parameters
static void someMethodName(int paramName1, int paramName2) { … }
}
Types - Primitives
Type Description
byte on 8-bit, -128 (-2^7) to 127 (inclusive, 2^7 - 1); example: byte a = 100, byte b = -50
short on 16-bit, -32,768 (-2^15) to 32767 (inclusive, 2^15 -1); example: short s = 10000, short r = -20000
int on 32-bit, - 2,147,483,648 (-2^31) to 2,147,483,647(inclusive) (2^31 -1); example: int a = 100000, int b = -200000
float single-precision 32-bit IEEE 754 floating point; should not be used for precise values such as currency
example: float f1 = 234.5f
double double-precision 64-bit IEEE 754 floating point; should not be used for precise values such as currency
example: double d1 = 123.4
boolean one bit, can hold only 2 values (true/false); example: boolean b = true
char 16-bit Unicode character; char data type is used to store any character; example: char letterA = 'A'
Questions: - why multiple types for similar data? (short vs int vs long - why not use long all the time?)
- what happens when reaching the range limits?
Types - Non-primitives
References
● created using the constructor of the classes (using 'new’);
● default value is null
● example: Employee emp = new Employee("Razvan");
Strings
● Holds fragments of text
● Made up of several char values
● String is also a reference type, not a primitive! (doesn’t need ‘new’ to build, but can be null !)
● example: "Hello World", "two\nlines"
Literals
● fixed values that appear directly in the source code: 100, 2.5, 33.8d, true, ‘A’
● byte, int, long, short can be expressed in decimal (base 10), hexadecimal (base 16) and octal:
int decimal = 100; int hexa = 0x64;
Operators
Operators: special symbols that perform specific operations on one, two or three operands
and then return a result.
• Arithmetic operators: + , - , / , * , % , ++ , --
• Relational operators: == (equal to), != (not equal to), > (greater than), < (less than),
>= (greater than or equal to), <= (less than or equal to)
• Bitwise operators: & (bitwise and), | (bitwise or), ^ (bitwise XOR), ~ (bitwise complement),
<< (left shift), >> (right shift)
• Conditional operator: ( ? : )
- - ternary operator, usage: (<condition>) ? <value for true> : <value for false>
This is a table with the value representations for the bitwise operations of OR, AND, XOR
and the logical operation NOT.
Operators - short-circuit
The logical operators ‘&&’ and ‘||’ behave similarly to their bitwise counterparts (‘&’ and ‘|’),
with a CRITICAL distinction: they can be short-circuited.
Short-circuiting means that at evaluation time, based on the previous values table, expression
composed of multiple operators will be evaluated only until necessary:
● && (logical AND): expression is evaluated until it encounters the first FALSE operand (as in this case
the end result will for sure be FALSE, no matter the rest of expression), or until its end
● || (logical OR): expression is evaluated until it encounters the first TRUE (as in this case the end
result will for sure be TRUE, no matter the rest of expression), or until its end
Example:
(a > 0) && (false) && (b ==2) && (c<0) //=> evaluation stops at 2nd term, end result is: false
true || (a>0) || (b==2) //=> evaluation stops at 1st term, end result is: true
Expressions
An expression may be composed of multiple operators (as long as the types of operands match). In this
case it’s evaluated by applying the operators in a specific order, given by their precedence (priority) - see:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
- Tip: when you are unsure about the evaluation order, or want to change the default one, you can use
parentheses ‘(‘ and ‘)’ to force a specific order:
a + b / 100 //evaluates ‘b/100’ first, then +
(a + b) / 100 //evaluates ‘a+b’ first, then /
Statements
A statement is the basic executable unit of code (can be used to build a full program).
- It is similar to a full phrase in natural language (it should say/mean something, do an action..)
- Need to be terminated with a “;”. Usually written one per line, but multiple per line are allowed.
- Multiple types:
- expression statements: some types of expressions (assignments, method calls..) with an “;” after
- declaration statements: declarations of a variables
- control flow statements - detailed in a later lesson
Note: a block of executable Java code expects a list of full statements, not just expressions!
Advantages:
- easy to reuse the code (run it multiple times, with different params..)
- DRY principle (Don’t Repeat Yourself)
- allows problem decomposition (split complex operations to simpler steps, give them
human-friendly names)
Methods - definition
public static void add (int a, int b) { public static int add (int a, int b) {
int sum = a + b; return (a + b);
System.out.println(sum) ; }
}
public static void writeSum(int a, int b) {
int sum = add(a, b);
System.out.println(sum) ;
}
Methods - full example
Tutorials:
- Java tutorial: http://tutorials.jenkov.com/java/index.html
- Oracle docs: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
Online exercises:
- Coding bat: https://codingbat.com/java
- Codewars: https://www.codewars.com/?language=java