Vietnam National University of HCMC
International University
School of Computer Science and Engineering
Introduction to Java
(IT069IU)
Le Duy Tan, Ph.D.
📧
[email protected] 🌐 leduytanit.com
1
Previously,
We talked about:
- Programming Paradigms.
- Basic idea of Object Oriented Programming (OOP).
- The importance of Java.
- Basic OOP concepts:
- Class
- Object
- Encapsulation with Access Modifier
- Abstraction
- Inheritance
- Composition
- Polymorphism
- The differences between JDK, JRE, JVM and how these works together.
2
Agenda
- Different Java Platforms
- Our choice of JDK
- The best IDE selections
- Create the first Java programs
- Compile and Run with commands or on IntelliJ
- Java data types:
- Primitive
- Non-primitive (reference types)
- Variable
- Operators
3
Revisited: How Java works (JDK, JRE, JVM)
Windows
Developers JVM
Java Runtime MacOS
develops (JDK) Environment JVM
(JRE)
Runs
Java source compile (JDK) Java class files
(*.class) - Java JVM Linux
files (*.java) 4
bytecode
Platform Independent Platform Dependent
=> Java is cross platform - “Write once, run everywhere!”
Cross-platform = “Write Once, Run Anywhere”
5
The Java Platforms
- Java Standard Edition (Java SE)
- Developing desktop and server applications.
- This course will mainly focus on this edition.
- Java Enterprise Edition (Java EE)
- Developing large-scale, distributed networking applications and web-based applications.
- You will learn in Web Application Development course (IT093IU).
- Java Micro Edition (Java ME)
- A subset of Java SE
- Developing applications for resource-constrained embedded devices, such as smartwatches,
MP3 players, television set-top boxes, smart meters (for monitoring electric energy usage).
6
Our choice for Java SDK
- Oracle JDK 17
- LTS version (Long-Term Support)
- Released in 09/2021 and got supported till 09/2029
- The implementation of Java Standard Edition (Java SE)
7
https://www.oracle.com/java/technologies/downloads/#jdk17-windows
The best Java IDEs
- IDE is Integrated Development Environment (editor + compiler + debugger + convenient tools + GUI)
- IntelliJ IDEA (Recommended)
- A commercial IDEs developed by JetBrains.
- Students can get Ultimate version:
https://www.jetbrains.com/community/education/#students
- Link to download: https://www.jetbrains.com/idea/
- Netbeans
- Open-source and free.
- Link to download: http://www.netbeans.org/index.html.
- Eclipse
- Developed by the eclipse open-source community.
- Link to download: http://www.eclipse.org.
8
An instant-noodle web IDEs
- For people who haven’t installed SDK or IDEs on your laptop and still want to
follow the lecture and practice Java at home.
- You can try the online Java compiler to run Java right on your browser:
https://www.jdoodle.com/online-java-compiler/
- This only support only one main class which is extremely limited so you still need
a full IDEs on your laptop for your project and labs.
9
First Java Project on IntelliJ
10
Our first Java Program
MyFirstJavaProgram.java
11
Let’s compile
- First, we check if our JDK is installed correctly
- We can compile the java file by the command “javac”
- The compiler creates a new compiled file with the extension .class, containing java bytecode
12
Let’s run
- To run the compiled .class file, we need to use the command “java”
Awesome! Now we can see the output string “Hello World”
Use Editor/IDEs MyFirstJavaProgram.java javac MyFirstJavaProgram.java
Java bytecode
Output: “Hello World” java MyFirstJavaProgram MyFirstJavaProgram.class 13
Run & Compile Automatically on IntelliJ
Method 1: Click on the main method Method 2: Setup a build configuration for the project
Method 3: Setup the new project from the template
14
Let’s try it with Multiple Class Files
GradeBook.java
UML class diagram
15
Let’s try it with Multiple Class Files
GradeBookTest.java
16
Compiling & Run with Multiple Class Files
- To compile multiple files, just list all the names of java files:
javac GradeBook.java GradeBookTest.java
- Or to compile all java files in the directory:
javac *.java
- After the compilation, two new compiled file with the extension class are created:
Gradebook.class, GradeBookTest.class
- To run a particular compiled file containing the main method, in our case, GradeBookTest.class:
java GradeBookTest
- You should see the output message:
17
Types of Comment Lines
There are 3 types of comments:
- Single line comment //
- Multiple line comment /* */
- Javadoc comment /** */
- prepare program documentation in HTML format.
18
Data Types
19
Data Types
- Java is statically typed and also a strongly typed language.
- There are two categories of data types:
- Primitive data types (8)
- Non-primitive data types (reference type)
20
Byte
Byte data type is an 8-bit signed two's complement integer.
- Minimum value is -128 (-2^7)
- Maximum value is 127 (2^7 -1)
- Default value is 0
- Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four
times smaller than an int.
- They are also useful when you’re working with raw binary data that may not be directly compatible
with Java’s other built-in types
- Example:
byte a = 100;
byte b = -50;
21
Short
Short data type is a 16-bit signed two's complement integer.
- Minimum value is –32,768 (-2^15)
- Maximum value is 32,767 (2^15 -1)
- Short data type can also be used to save memory as byte data type. A short is 2 times smaller than
an int
- Default value is 0.
- Example:
short s= 10000;
short r = -20000;
22
Int
int data type is a 32-bit signed two's complement integer.
- Minimum value is - 2,147,483,648 (-2^31)
- Maximum value is 2,147,483,647 (2^31 -1)
- Int is generally used as the default data type for integral values unless there is a concern about
memory.
- The default value is 0.
- Example:
int a = 100000;
int b = -200000;
23
Long
Long data type is a 64-bit signed two's complement integer.
- Minimum value is -9,223,372,036,854,775,808 (-2^63)
- Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
- This type is used when a wider range than int is needed.
- Default value is 0L.
- Example:
long a = 100000L;
long b = -200000L;
24
Float
Float data type is a single-precision 32-bit
- Evaluating expressions that require fractional precision, such as square root, or transcendentals
such as sine and cosine.
- Float is mainly used to save memory in large arrays of floating point numbers.
- Default value is 0.0f.
- It will become imprecise when the values are either very large or very small.
- Can be useful when representing dollars and cents.
- Example:
float houseTemperature = 23.5f;
25
Double
Double data type is a double-precision 64-bit
- This data type is generally used as the default data type for decimal values, generally the default
choice.
- Double values should never be used for precise values such as currency. (use BigDecimal instead)
- The range is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Default value is 0.0d.
- Example:
double myWallet = 123.435;
26
Boolean
boolean data type represents one bit of information.
- There are only two possible values: true and false.
- This data type is used for checking true/false for if statement conditions.
- Default value is false.
- Example:
boolean one = true;
boolean larger = 8 > 4;
27
Char
char data type is a single 16-bit Unicode character.
- Minimum value is '\u0000' (or 0).
- Maximum value is '\uffff' (or 65,535).
- Char data type is used to store any character.
- Example:
char letter = 'A';
28
https://en.wikipedia.org/wiki/UTF-16
Variable
29
Variable
- The variable is the basic unit of storage in a Java program.
- The identifier is the name of the variable (or class name, …)
- The valid identifier has a series of characters consisting of letters, digits,
underscores (_) and dollar signs ($) that does not begin with a digit and does
not contain spaces.
- Some valid identifiers are Welcome1, $value, _value, m_inputField1 and button7.
[Question] Is the name 7button a valid
30
identifier?
Name Convention in Java
31
Java Keywords
- The following list shows the reserved words in Java, which may not be used
as constant or variable or any other identifier names.
32
Garbage Collection
- Allocate and Deallocate memory:
- Java:
- System responsibility:
- Dynamically allocated memory
- Deallocation is done automatically (system-level thread)
- Checks for and frees memory no longer needed
- C/C++:
- Programmer's responsibility
- Manually done (malloc & free)
33
Operators
34
Operators
All the Java operators can be divided into the following groups −
• Arithmetic Operators: * / % + -
• Relational Operators: < > <= >= == != instanceof
• Bitwise Operators: & ^ |
• Logical Operators: && || !
• Assignment Operators: = += -= *= /=
• Ternary operator: ? :
35
Math (Arithmetic) Operators
Operator Description Example Output
+ (Addition) Adds values A & B A+B 32
- (Subtraction) Subtracts B from A A-B -12
* (Multiplication) Multiplies B by A A*B 220
/ (Division) Divides B by A B/A 2.2
% (Modulus) Divides left-hand by B%A 2
right-hand and
return remainder
[Question] Can you guess what are A and B?
36
Relational Operators int A = 5, B = 2;
Operator Description Example Output
== (equal to) Check if the values are equal. A == B false
!= (not equal to) Check if the values are not equal. A != B true
> (greater than) Check if the left value is greater than A>B true
the right value.
< (less than) Check if the left value is less than the A<B false
right value.
>= (greater than or equal to) Check if the left value is more than or A >= B true
equal to the right value
<= (less than or equal to) Check if the left value is less than or A <= B false
equal to the right value
instance of Check if the object is a particular type “hello” instance of String true
of (class type or interface type)
37
Bitwise Operators
Operator Description Example Output
A=3 00000011 (3)
B=5 00000101 (5)
& (bit-wise and) Copies a bit if it exists in both. A&B 00000001 (1)
| (bit-wise or) Copies a bit if it exists in either. A|B 00000111 (7)
^ (bitwise XOR) Copies a bit if it is in one number but not both. A^B 00000110 (6)
~ (bitwise compliment) Flips all bits ~A 11111100 (-4)
<< (left shift) Shift all bits to the left by a specified number. A << 2 00001100 (12)
[Real Interview Question] Can you figure out how to
know a number is even or odd using bitwise operators? 38
Logical Operators boolean A = true, B = false;
Operator Description Example Output
&& (logical and) If both inputs are true then only that the condition is true. A && B false
|| (logical or) If either of any input is true then the condition is true. A || B true
! (logical not) Use to reverse the logical state of the input. !(A && B) true
39
Assignment Operator float A = 6, B = 3;
Operator Description Example Output
= (assignment operator) Assign values from the right side to the B=A+B 9.0
left side.
+= (Add AND assignment Is equivalent to B = B + A B += A 9.0
operator)
-= (Subtract AND assignment Is equivalent to B = B - A B -= A -3.0
operator)
*= (Multiply AND assignment Is equivalent to B = B * A B *= A 18.0
operator)
/= (Divide AND assignment Is equivalent to B = B / A B /= A 0.5
operator)
40
Ternary Operator
- A special ternary (three-way) operator that can replace simple statement of if-
then-else statements to have it in one line.
- expression1 ? expression2 : expression3
- For example,
int val1 = 10;
int val2 = 20;
int max = val1 >= val2 ? val1 : val2;
[Question] Can you guess what is the value of max?
41
Recap
This lecture, we have learnt about:
- Different Java Platforms
- Our choice of JDK
- The best IDE selections
- Create the first Java programs
- Compile and Run with commands or on IntelliJ
- Java data types:
- Primitive
- Non-primitive (reference types)
- Variable
- Operators
42
Thank you for your listening!
43