1/34
Looks like no tags are added yet.
Why use Java
Works on multiple platforms
Most popular programing language in the world
Easy to learn
What is the main() Method
The main() method is required in every Java program. It is where the program starts running
Example of Java syntax
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}What is println() Method
The println() method to print a line of text to the screen
Example of how to write out println()
System.out.println("Hello World");‘Create a porgam that outputs “Hello, Java”
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}What is the print() method
Similar to println() but it does not insert a new line at the end of the output
What is the difference between the println() compared to the print()
The main difference is that println() does insert a new line at the end of the output whereas print() does not.
How do you print numbers in Java
System.out.println(3);
System.out.println(358);
System.out.println(50000);True or False you can perform mathematical operations in println() method
True
example: System.out.println(3 + 3); → Output: 6
Create a program that to print your name and age
public class Main {
public static void main(String[] args) {
System.out.println("Philippe");
System.out.println(20);
}
}Create a program that calculates 22 × 3
public class Main {
public static void main(String[] args) {
System.out.println(22 * 3);
}
}What is a single line comment
Single-line comments start with two forward slashes (//).
Usually used for short comments (single lines)
How do you write a singe line comment
//This is a comment
System.out.println("Hello World");
What is a multi line comment
Multi-line comments start with /* and ends with */
Usually used for larger comments (so like more of a couple of sentences)
How do you write a multi line comment
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");What are variables
Variables are containers for storing data values.
List the different types of variables in Java
String → Stores text, such as “Hello”
int → Stores integers (so whole numbers, no deciamls), such as 123 or -123
float → Stores floating point numbers, with decimals, such as 19.99 or -19.99
char → Stores single characters, such as ‘a’ or ‘B’
boolean → Stores values with two states ,True or False
Declaring(/creating) variables in Java
Choose a type (like int or String)
Give the variable a name (like x, age, or name)
Optionally assign it a value using =
Example syntax:
type variableName = value;
Create a variable called name of type String and assign it the value "John".
Then use println() to print the name variable.
String name = "John";
System.out.println(name);Create a variable called myNum of type int and assign it the value 15,
Then use println() to print the myNum variable.
int myNum = 15;
System.out.println(myNum);int myNum = 15; Change the value of myNum from 15 to 20
int myNum = 15;
myNum = 20; // myNum is now 20
System.out.println(myNum);What are Final Variables
Its for when you don’t want to overwrite existing values.
(this will declare the variable as "final" or "constant", which means unchangeable and read-only)
Example of Final variable with error
final int myNum = 15;
myNum = 20; // will generate an error: cannot assign a value to a final variableExample of how to declare a String variable
String myText = "Hello"; Example of how to declare an integer variable
int myNumber = "123";Example of how to declare a float variable
float myFloat = 5.99f;Example of how to declare a char varaible
char myChar = "a"; Example of how to declare a boolean varaible
boolean myBool = true;How to display variables in java
You can use + to combine things (so string concatenation, like in python )
String name = "John";
System.out.println("Hello " + name);You can also + to add two varaibles together.
Example combine firstName and lastName to one variable fullName and print it
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);What are the two meanings for the + in Java
For text (so strings), it joins them together
For numbers, it adds values together
Add together the values x = 5 and y = 6 and print the results
int x = 5;
int y = 6;
System.out.println(x + y);What would the output of this be:
int x = 5;
int y = 6;
System.out.println("The sum is " + x + y);The output is:
The sum is 56
What would the output of this be:
int x = 5;
int y = 6;
System.out.println("The sum is " + (x + y));The output is:
The sum is 11