Programming 1: Lecture 2 - Basic Java Syntax
Programming 1: Lecture 2 - Basic Java Syntax
int x = 5;
// x = x + 3;
System.out.println(x);
int x = 5;
x += 3; // increase x
Multi-line comments
• Two types:
– Block comment starts with /* and ends with */
– Javadoc comment starts with /**, ends with */ and
each line begins with a *.
What is a variable?
A named value
int age = 19; I use the name age to call
the number 19
So that when I say:
I am age years old!
You would understand that I
was 19 years old.
19 is an integer
literal.
int age;
Semicolon
Space
(-28) .. (28 - 1)
byte Very small integers 0 8 bits
-128 .. 127
6 * 5 + 3 evaluates to 33
a similar to single-valued
expressions
6 == 5 evaluates to false
Math Operators
5 + 3 addition, evaluates to 8
5 * 3 multiplication, evaluates to 15
5 - 3 substraction, evaluates to 2
<variable> = <expression>;
Implication vs Assignment
• In math, we have • In programming,
the left side and we have the right
want to figure out side and computer
the right side (you calculates the left
do the calculation). side for us.
2+3=? a=2+3
then then
2+3=5 a gets the value of 5
Assignment statements
x = x + 1;
Confused?
int x = 5;
x = x + 1;
int x = 5;
x = x + 1;
means
x = 5 + 1;
x++;
Display expression's value
public class Example {
public static void main(String[] args) {
int a = 15;
System.out.println(a);
}
}
Output:
15
Display expression's value
public class Example {
public static void main(String[] args) {
int a = 15;
System.out.println(a*3);
}
}
Output:
45
Display expression's value with text
public class Example {
public static void main(String[] args) {
int age = 20;
System.out.println("I am " + age);
}
}
Output:
I am 20
Display variable's value with text
public class Example {
public static void main(String[] args) {
int age = 20;
System.out.println(age + " is my age!");
}
}
Output:
20 is my age!
Display variable's value with text
public class Example {
public static void main(String[] args) {
int age = 20;
System.out.println("I am " + age + " years old");
}
}
Output:
I am 20 years old
Exercise
• Write a program to get the result of 152 × 1132
• Expected results: 172064
Answer
public class Exercise {
public static void main(String[] args) {
int a = 152;
int b = 1132;
int c = a * b;
System.out.println(c);
}
}
Equivalent Answer
public class Exercise {
public static void main(String[] args) {
System.out.println(152 * 1132);
}
}
Exercise
• Write a program to get the result of the following
math operation:
5
2
• Expected results: 2.5
Answer
public class Exercise {
public static void main(String[] args) {
double a = 5;
double b = 2;
double c = a / b;
System.out.println(c);
}
}
Equivalent Answer
public class Exercise {
public static void main(String[] args) {
double a = 5;
System.out.println(a / 2);
}
}
Non-Equivalent Answer
public class Exercise {
public static void main(String[] args) {
System.out.println(5 / 2);
}
}
• Actual result: 2
• Reason: integer division is used when both sides
are integer.
Another Equivalent Answer
public class Exercise {
public static void main(String[] args) {
System.out.println(5.0 / 2);
}
}
< true if the left side is smaller than the right side a < b
> true if the left side is greater than the right side a > b
|| OR a == b || a == c
if (a < 0 || b < 0) {
Statements to
a = 0; be executed if
b = 0; the Boolean
expression
System.out.println("…"); evaluates to
} true
End of block
if statement
if (a < 0 || b < 0)
System.out.println("…");
Curly braces { } can be omitted if the block contains only one statement
*Note: a code block only needs curly braces { } if it has multiple statements
if…else statement
// assume that a and b are entered by user
if (a < 0 || b < 0) {
System.out.println("Please enter non-negative numbers!");
} else {
System.out.println("You entered correctly!");
}
if (a < 0 || b < 0) {
a = 0; Statements to
be executed if
b = 0; the condition
System.out.println("…"); is true
} else {
System.out.println("ok"); Executed if
condition is
System.out.println(a + b); false
}
Demo program
• Write a program to solve
ax + b = 0
given a and b
• For a = 2 and b = 1, expected result:
x = –0.5