Java Basic
1. Java variables and literals
2. Java Datatypes and Operator
3. Input & Output
4. Java expression, block and Comment
5. Keyword in Java
6. Java if…else
7. Switch statement
8. Loop
9. Array
1. Java variables and literals
*Java variables:
• A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type.
• There are three types of variables in java: local, instance and static.
*Literals:
• Literals are the data items that have fixed or constant values
• Java allows 6 kinds of literals which are:
- Boolean Literals: gồm 2 giá trị true and false
- Integer: Binary, Decimal, Octal, Hexadecimal
int binary = 0b10010;
int Octal = 027;
int Decicmal = 34;
int heximal = 02xF;
- Floating – point Literials
- Character Literals: char letter = ‘a’;
- String Literals: String str1 = “Phan Hai”
- Null Literals: Null literals are the values that represent null values (null)
2. Java Datatypes and Operator
*Datatypes: Data types specify the different sizes and values that can be stored in the variable
*Operator: a symbol that is used to perform operations
3. Input & Output
*Input: Java provides different ways to get input from the user
• Import [Link], [Link]
Example:
*Output: can simply use
• [Link](): It prints string inside the quotes.
• [Link](): similar like print() method. Then the cursor moves to the beginning of the next
line.
• [Link](): It provides string format
Example:
4. Java expression, block and Comment
• Java expression: A Java expression consists of variables, operators, literals, and method calls
• Java block: A block is a group of statements (zero or more) that is enclosed in curly braces { }
• Comment:
- Single Line Comment: //
- Multi Line Comment: /* */
- Documentation Comment: /** */
5. Keyword in java
• abstract: Java abstract keyword is used to declare an abstract class. An abstract class can provide the
implementation of the interface. It can have abstract and non-abstract methods.
• boolean: Java boolean keyword is used to declare a variable as a boolean type. It can hold True and False
values only.
• break: Java break keyword is used to break the loop or switch statement. It breaks the current flow of the
program at specified conditions.
• byte: Java byte keyword is used to declare a variable that can hold 8-bit data values.
• case: Java case keyword is used with the switch statements to mark blocks of text.
• catch: Java catch keyword is used to catch the exceptions generated by try statements. It must be used after
the try block only.
• char: Java char keyword is used to declare a variable that can hold unsigned 16-bit Unicode characters
• class: Java class keyword is used to declare a class.
6. Java if…else
• If..then statement:
• If…else statement:
- if-else-if ladder Statement: The if-else-if ladder statement executes one condition from multiple
statements.
- Nested if statement: the if block within another if block. Here, the inner if block condition executes
only when outer if block condition is true.
7. Switch Statement
- The Java switch statement executes one statement from multiple conditions
8. Loops in Java
*Java For loop: The Java for loop is used to iterate a part of the program several times. If the number of
iteration is fixed, it is recommended to use for loop.
• For – each loop: used to traverse array or collection in java
Example:
*Java While loop:
Syntax:
*Java Do - While loop
Syntax:
⮚ Loop in Java
⮚ Loop in Java
- Java loop for: it used to repeat a part of the program many times. The loop
for is often used in cases where the number of iterations is fixed
- Java For –Each Loop: A loop is used to traverse an array or a collection, its
moves from the first word to the last element of the array or set in turn.
- Java While Loop: Its used to excute a program repeatedly, while a
condition is still true. While loop is usually used when the number of iterations is
not predetermined
- Java do – While Loop: Its used to excute a program segment multiple
times. The characteristic of do – while is that a block of code is always excuted at
least once.
9. Array:
• Java array is an object which contains elements of a similar data type
• Advantages:
- Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
- Random access: We can get any data located at an index position.
• Disadvantages:
- Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection framework is used in Java which grows automatically.
• Multidimensional Array in Java
Syntax:
Example:
• Coppy Array:
Syntax:
Example: