Software Technologies Java Basics
Software Technologies Java Basics
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
1. Welcome to Java
2. Variables and Operators
3. Conditions: if-else, switch
4. Loops: for, while, do-while, …
5. Methods, parameters, return value
6. Objects and Classes
7. Arrays and Collections:
ArrayList, Map, Set
8. Lambda Expressions and Stream API 2
Have a Question?
sli.do
#3295
3
Welcome to Java
Java is a statically-typed, object-oriented
programming language
Very similar to C#, but less flexible
5
Installing Java
Download and install Java 8 SDK (JDK 8)
http://oracle.com/technetwork/java/javase/downloads/
6
Eclipse for Java EE
Eclipse is open-source Java /
Java EE / PHP / C++ IDE
Community edition
(free)
Ultimate edition
(commercial)
8
Variables and Operators in Java
Play with Java online:
Define variables by their type www.javarepl.com
int i = 5; double d = 3; // i is integer, d is double
System.out.println(i + " ; " + d); // 5 ; 3.0
d = i * d; d++; // d = 16.0 The ; separates
System.out.println(d + 1); // 17.0 multiple statements
int number = 5;
if (number % 2 == 0) {
System.out.println("Even number");
}
else {
System.out.println("Odd number");
}
12
Beware of Comparing Strings
String s = "sho";
if ("Pesho" == "Pe" + s) {
System.out.println("Здрасти, Пешо!");
}
13
Problem: Three Integers Sum
You are given 3 integers
Check whether the sum of two of them is equal to the third
8 15 7 7 + 8 = 15 3 8 12 No
-5 -3 -2 -3 + -2 = -5 0 0 0 0 + 0 = 0
int count = 1;
while (count < 1024)
System.out.println(count *= 2); // 2 4 8 … 1024
String s = "ha";
do { System.out.println(s); s = s + s; }
while (s.length() < 10); // ha haha hahahaha
18
Problem: Sum N Integers in Java
import java.util.Scanner; 5
public class SumIntegers { 10
public static void main(String[] args) { 20
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(); 30
long sum = 0; 40
for (int i = 0; i < n; i++) -1
sum += scan.nextInt();
System.out.println("Sum = " + sum);
}
} 99
Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/264#3 19
Problem: Symmetric Numbers
Write a Java program that finds and prints all symmetric
numbers in the range [1…n]
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99
101 111 121 131 141 151 161 171 181 191 202
212 222 232 242 252 262 272 282 292 303 313
750 323 333 343 353 363 373 383 393 404 414 424
434 444 454 464 474 484 494 505 515 525 535
545 555 565 575 585 595 606 616 626 636 646
656 666 676 686 696 707 717 727 737 747
Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/264#4 20
Solution: Symmetric Numbers
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i = 1; i <= n; i++)
if (isSymmetric("" + i))
System.out.print(i + " ");
}
static boolean isSymmetric(String str) {
for (int i = 0; i < str.length() / 2; i++)
if (str.charAt(i) != str.charAt(str.length() - i - 1))
return false;
return true;
}
Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/264#4 21
Methods in Java
Methods in Java hold a named piece of code
Can take parameters and return a result (strongly typed!)
System.out.println(multiply(2, 3)); // 6 == 2 * 3
System.out.println(multiply(5)); // Compilation error
22
Objects
In programming objects holds a set of named values
E.g. a rectangle object holds width and height
Object name
rectangle
width = 5 Object
height = 4 properties
The new operator
Creating a rectangle object: creates a new object
bigRect.setWidth(bigRect.getWidth() * 2);
bigRect.setHeight(bigRect.getHeight() * 2);
System.out.println(bigRect); // Rect[width=200, height=160]
System.out.println("Area: " + bigRect.area()); // Area: 32000
26
Interfaces
Interfaces define a set of methods to be implemented later
27
Implementing an Interface
28
Strings
Strings in Java
Know their number of characters: length()
Can be accessed by index: charAt(0 … length()-1)
Reference types
Stored in the heap (dynamic memory)
Can have null value (missing value)
Strings cannot be modified (immutable)
Most string operations return a new String instance
StringBuilder class is used to build stings 30
Strings – Examples
String str = "SoftUni";
System.out.println(str);
for (int i = 0; i < str.length(); i++) {
System.out.printf("str[%d] = %s\n", i, str.charAt(i));
}
System.out.println(str.indexOf("Uni")); // 4
System.out.println(str.indexOf("uni")); // -1 (not found)
System.out.println(str.substring(4, 7)); // Uni
System.out.println(str.replace("Soft", "Hard")); // HardUni
System.out.println(str.toLowerCase()); // softuni
System.out.println(str.toUpperCase()); // SOFTUNI
31
Strings – Examples (2)
String firstName = "Steve";
String lastName = "Jobs";
int age = 56;
System.out.println(firstName + " " + lastName +
" (age: " + age + ")"); // Steve Jobs (age: 56)
String allLangs = "C#, Java; HTML, CSS; PHP, SQL";
String[] langs = allLangs.split("[, ;]+");
for (String lang : langs) {
System.out.println(lang);
}
System.out.println("Langs = " + String.join(", ", langs));
System.out.println(" \n\n Software University ".trim());
32
Arrays in Java
// Array holding numbers
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(numbers));
// Array holding strings
String[] weekDays = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
// Array of mixed data (objects)
Object[] mixedArr = {1, new Date(), "hello"};
// Array of arrays of strings (matrix)
String[][] matrix = {
{"0,0", "0,1", "0,2"},
{"1,0", "1,1", "1,2"}};
33
Processing Array Elements
String[] capitals =
{"Sofia", "Berlin", "London", "Paris", "Moscow"};
capitals[0] = "SOFIA";
capitals[4] = null;
System.out.println(Arrays.toString(capitals));
// [SOFIA, Berlin, London, Paris, null]
for (String capital : capitals)
System.out.println(capital); Traditional foreach loop
10 30 15 20 50 5 20 30 10 5 20 3 20
50 30 20 30 20 20 20 10
40
HashMap<K, V> – Examples
Counting words occurrences in a list:
String[] words = { "yes", "hi", "hello", "hi", "welcome",
"yes", "yes", "welcome", "hi", "yes", "hello", "yes" };
Map<String, Integer> wordsCount = new HashMap<String, Integer>();
for (String word : words) {
Integer count = wordsCount.get(word);
if (count == null) {
count = 0;
}
wordsCount.put(word, count+1);
}
System.out.println(wordsCount); // {hi=3, yes=5, hello=2, welcome=2}
41
TreeMap<K, V> – Examples
Students and their grades
42
Problem: Sums by Town
You are given a sequence of lines, each holding town + income
Sofia | 200
Towns can appear
Varna | 120
multiple times
Pleven | 60
Varna | 70
45
Collections and Streams: Map and Collect
46
Collection Querying and Traversing
Optional<String> first =
names.stream()
.findFirst();
System.out.println(first.get());
47
Summary
Java is a statically-typed language
Java programs consist of classes
? ?
?
sti o n s ?
e
?
Qu ?
?
?
https://softuni.bg/courses/software-technologies
License
This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International"
license
51
Free Trainings @ Software University
Software University Foundation – softuni.org
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University @ YouTube
youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg