Intro To Java
Intro To Java
Introduction to Java
Brandon Krakowsky
Introduction to Java
3
11/30/2020
Java is Compiled
• When Java is compiled, it’s converted to binary machine code (or Java bytecode)
‐ This allows Java programs to be “portable” and run on different machines and operating
systems
• Compiled languages have many advantages over interpreted languages
‐ When code is compiled, it’s optimized under the hood
‐ Since your program will be inspected for errors, many kinds of potential bugs will be caught
early (e.g. using the same variable name twice)
• Your program will not run if it is not compiled!
• The IDE we’ll be using for Java development, Eclipse, will compile your code for you (on the fly)
as you save your work
‐ It will also help you fix MANY problems in your code
Ref: https://www.tiobe.com/tiobe‐index/
6
11/30/2020
• General highlights:
‐ Java and Python are in the top 3 most popular programming languages
‐ Currently, both languages have almost the same rating
Ref: https://www.tiobe.com/tiobe‐index/
9
11/30/2020
10
11
Eclipse
• Eclipse is one of two main IDEs for Java development
‐ The other IDE is IntelliJ
‐ I’ll work with Eclipse
• Eclipse makes it very easy to write well‐formatted Java, with good style
‐ Like Python’s PyCharm, it has a TON of features
‐ It compiles code on the fly, provides autocomplete suggestions, and fixes simple bugs
‐ Overall, Eclipse greatly speeds up Java programming
• Getting Eclipse:
‐ Go to https://www.eclipse.org/downloads/ and download the latest version
12
11/30/2020
13
14
15
11/30/2020
16
17
Java Language
18
11/30/2020
//Class declaration
public class MyClass { //Should begin with a capital letter
//The Java file will be named (and saved in) ‘myPackage/MyClass.java’
19
System.out.println(“Hello World!”);
• Here’s another statement
20
21
11/30/2020
if (myVar == true) {
//code block
}
• And here’s a function
22
23
24
11/30/2020
25
26
Printing
• There are two methods you can use for printing:
//This prints something and doesn’t end the line (so the next thing you
print will go on the same line)
System.out.print(something);
• These methods will print any one thing, but only one at a time
• Of course, you can always concatenate Strings with the + operator
• Example:
System.out.println("Four " + 4 + ", three " + 3 + ", two " + 2 + ", one
" + 1);
27
11/30/2020
while Loops
• while loops in Java have a similar syntax to while loops in Python
• Simple while loop that iterates 10 times:
int i = 0;
while (i < 10) {
//do stuff here every time loop happens
i++; //manually increment i
}
//i is initially set to 0
//i must be less than 10 in order to enter the loop each time
//code in the loop manually increments i by 1 at the end of each loop
28
for Loops
• for loops in Java have a very different syntax than for loops in Python
‐ But they are equivalent to: for i in range(10)
• A for loop has 3 parts:
‐ Setting the initial value
‐ The condition for entering the loop
‐ The change in the loop variable that happens at the end of each loop
• Simple for loop that iterates 10 times:
29
Getting Input
• First, import the Scanner class:
import java.util.Scanner;
• Create a scanner and assign it to a variable:
Scanner scan = new Scanner(System.in);
‐ The name of the scanner is scan
‐ new Scanner(...) tells Java to make a new one
‐ System.in tells Java that the scanner is to take input from the keyboard
• To read in the next int:
int myNumber = scan.nextInt();
• To read in the next String:
String myString = scan.next();
• To read in the entire next line as a String:
String myLine = scan.nextLine();
30
11/30/2020
Java Comments
• Here is a single line comment, using double slashes //
//Here is an int, initially set to 0
int myInt = 0;
• Here is a block comment, using /* */
/*
* Here is an int
* It’s initially set to 0
*/
int myInt = 0;
• As a shortcut in Eclipse, you can type the following
/*
and then hit Enter
• It will add a block comment and you can fill in the rest
31
Javadocs
• You can add Javadocs (Java documentation) just before the definition of a variable, method, or class
‐ This is the equivalent of a docstring inside of a Python function or class
• As a shortcut, you can type the following right above a variable, method, or class name
/**
and then hit Enter
• It will add a javadoc block and you can fill in the rest
/**
* Returns the sum of two given numbers.
* @param firstNum First value to add
* @param secondNum Second value to add
* @return Sum of values
*/
public int getSum(int firstNum, int secondNum) {
return firstNum + secondNum;
}
32
33
11/30/2020
34
• Click “Next”
35
• Click “Finish”
36
11/30/2020
37
38
• Provide a Name
‐ Class names should be capitalized
• Click “Finish”
39
11/30/2020
40
41
42
11/30/2020
43
44
45
11/30/2020
46
47
48
11/30/2020
49
50
51
11/30/2020
52