Sirma
Basic Syntax
Java Programming Language
Data Types & Variables
[Link]
Sirma
Content
01 Course Introduction
02 Data Types and Variables
03 User Input
[Link]
Sirma
Content
04 Output Format
05 Common Mistakes
06 Operations
[Link]
Alen Paunov
Sirma
Alen has 11 years of experience
as a teacher and lecturer in various fields and
educational organizations.
After 5 years of working in the legal services sector, he
decides to dedicate himself to software technologies.
He has been an Educational Director in multiple
academies and organizations. Throughout this time, he
has successfully balanced the responsibilities of his
position with his great passion for teaching.
[Link]
Sirma
01
Course Introduction
What is Programming, IDE, source code
[Link]
Sirma
Schedule
• Essentials Syntax
• Basic Syntax
• Algorithmic Thinking
• Foundation Knowledge
• Application Development
• Final Exam
• Interview
[Link]
Sirma
Programming Language
• Written by people for people
• Contains exact commands
• Commands are reduced (compiled) to computer code
• Performed unquestioningly by the computer
• There are many programming languages:
• Java, C#, JavaScript, Python, C++, C, PHP, Go, Swift и др.
• Java was established in 1995.
• Over the past 20 years, in the top three of the TIOBE Index* *
[Link]
[Link]
Sirma
TIOBE Index – Java
2021 2016 2011 2006 2001 1996
2 1 28
1 1 3
[Link]
Sirma
What is IDE?
• Integrated Development Environment (IDE)
• The overall development environment
• Code Editor
• Debugger
• Automation tools
• Hinting and completing the code
• Compiler
• And other instruments
• The Best Java Environment > IntelliJ Idea
[Link]
Sirma
IDE
IntelliJ Idea
Navigation
Code
editor
Project
Menus
window
Output
[Link]
Sirma
New Project
1
2 3
[Link]
Sirma
First Application
Elements of our program:
• public class Main - our class (file)
• public static void main - the starting point of our program
The class and the method are followed by an open and closed
curly brace.
• The execution code is written
between the innermost opening
and closing curly braces.
[Link]
Sirma
Code Example
public class Main {
public static void main(String[] args) {
Command
[Link]("Hello, world!");
end
} Print Text to be
command printed
}
[Link]
Sirma
02
Data Types & Variables
Text. Numeric Types. Variables
[Link]
Sirma
Data Types
• Every data type has a specific 23
int "Java
size (permissible values).
•
100 "
Different types of data (numbers,
text) are stored in specific
memory types.
•
5
They are named using keywords
String
"A"
[Link]
in Java.
Sirma
Data Types
According to the type of information stored in the
computer's memory, data types vary:
• Numeric
• Integer numbers - byte, short, int, long
• Floating-point numbers - double, float
• Boolean type 10" 3
• boolean
me . 1
• Character
• char
"N 0"J 4
a
• String (Text)
• String
true"
[Link]
• Object
• Object
Sirma
Variables
• In memory, data needs to be stored somewhere.
• They are stored in containers of various sizes.
• These containers are called variables.
• Variables have a type, a name, and a value.
type int number = 100; value
[Link]
name
Sirma
Variables - Examples
We assign different data based on
the type of the variable.
• int number = 100;
• long bigNumber = 70 000 000 000;
• double floatingPoint = 3.14;
• String name = "Java";
• char symbol = 'J';
• boolean isValid = true;
in
[Link]
t
Sirma
Task
Printing Numbers.
• Create a variable of type "int".
• Assign it a value of 100.
• Print the value of the variable.
• Repeat with type "double", with a value of 3.14.
*Tip - use the code for printing "Hello, world!" (instead of the text
in parentheses, use the variable name)
*Don't forget the ";", to end the command.
[Link]
Sirma
Example
public class Main {
public static void main(String[] args) {
int number = 100;
[Link](number);
}
}
[Link]
Sirma
03
User Input
Reading from the Console
[Link]
Sirma
User Input
We can read user input from the console.
• Store the data in a variable.
• Work with the data and modify it.
Everything in the console is text.
We need to create a reader:
Scanner scanner = new Scanner([Link]);
To read different types, we need to explicitly specify commands for
[Link]
them.
Sirma
Reading Different Data Types
Reading from the console
String text = [Link]();
Reading a character from the console:
char symbol = [Link]().charAt(0);
Reading an Integer from the console:
int number = [Link]([Link]());
Reading a real number from the console:
[Link]
double number = [Link]([Link]());
Sirma
05
Output Format
Formatting Numbers and Text
[Link]
Sirma
Output Format
We can print the data stored in variables to
the console.
Printed
Text
[Link]
Sirma
Print Output
We print using a specific command.
• Print and download the cursor on the next line:
[Link]("Hello, world");
• Print and cursor remains on the same line:
[Link]("Hello, world");
• Print variable:
[Link]
[Link](number);
Sirma
Format Output
Using templates:
%s – String
%c – tank
%d – int / long
%f – double / float
Task:
Write a program that reads from the user city and degrees and
displays the following message on the console:
"Today in {town} it is {degrees} degrees."
[Link]
Sirma
Example
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String town = [Link]();
int degrees = [Link]([Link]());
[Link]("Today in %s it is %d degrees.",
town, degrees);
}
[Link]
Sirma
Rounding
• Round to the next (larger) integer:
double up = [Link](3.14); //4.00
• Round to the previous (lower) integer:
double down = [Link](3.14); //3.00
• Print variable:
[Link]("%.3f", 3.14159); //3.142
[Link]
Sirma
05
Common Mistakes
[Link]
Sirma
Common Mistakes
Outside the "main" method
Lack of ";"
Lack of ")"
Lowercase (System)
Capital letter
(println)
[Link]
Lack of quotation
mark
Sirma
06
Operations
Operators +, -, *, /
[Link]
Sirma
Operations
Operations with a number
public static void main(String[] args) {
int a = 10;
int b = 5;
[Link](a + b); //15
[Link](a - b); //5
[Link](a * b); //50
[Link](a / b); //2
[Link]
}
Sirma
Operations
The result can be stored in a variable:
public static void main(String[] args) {
int a = 10;
int b = 5;
int sum = a + b;
[Link](sum); //15
}
[Link]
*The variable must be the same type so we don't lose data
Sirma
Edge Cases
• Division with integer and real number
int num = 7;
[Link](num / 2); //3
[Link](num / 2.0); //3.5
• Division with 0
int num = 7;
[Link](num / 0); //Error
[Link](num / 00,); //Infinity
[Link]
[Link](0.0 / 00,); //NaN
Sirma
Text operations
Concatenating text with an operator " + " is
called concatenation
Scanner scanner = new Scanner([Link]);
String name = [Link]();
[Link]("Hello, " + name);
The result is a combination
of the two texts.
[Link]
Sirma
Edge Case
Concatenation of text and number
double a = 3.14;
double b = 5.35;
[Link]("The sum is: " + a + b);
[Link]
Sirma
Thank
you!
[Link]