INTRODUCTION Chapter 1: part 2
TO JAVA
• Overview of Class
• Variables & Literals
• Data Types
Outline • Operators
• Input & Output
• Expressions & blocks
• Conditional Statements
• Looping Statements
• Array
• It follows CamelCase naming convention and must be
the same with the file name if it has public modifier
• Java application must contain a main method, since
Class Overview
execution starts from main method
• Syntax:
modifier [KW] class ClassName [KW] class/interface {
// fields
// constructors
// methods
}
• A variable is a reserved memory (storage area) used for
storing a value
• E.g int speedLimit = 80;
Variables & Literals • A variable only store a value that matches its data type
• Unlike constants a value in a variable can be changed
• Variable naming rules:
• Variables are case sensitive
• E.g. int age & int AGE are two different variables
• Variables must start with either a letter or an underscore, or
a dollar sign
• A variable name can not start with numbers
• Whitespace can’t be used
Variables & Literals
• Use meaningful name when declaring a variable
(recommended)
• Keywords can’t be used as variable name
• Literals are data used for representing fixed values like 1, 2.5.
‘F’
• Types of literals
Variables & Literals
• Boolean Literals
• Used to initialize Boolean data types
• e.g. boolean flag1 = true
• Integer Literals
• Is a numeric value without any fractional part
• Floating-point Literals
• Is a numeric literal that has a fractional part
Variables & Literals
• Character Literals
• Is a character enclosed inside single quotes
• String literals
• Is a sequence of characters enclosed inside double-quotes
•
• Specify the type of data that can Short
be stored inside a variable • Default value is 0
• Primitive data types • Uses 2 bytes to represent a
• Boolean number
Data Types • The default value is false • Char
• Uses 1 bit to represent true • 16-bit Unicode character
or false • The default value is ‘\u0000’
• Byte
• String (not a primitive)
• Default value is 0
• It represents array of
• Uses 8 bits to represent the character and found in
• Int • Double
• Default value is 0 • Default value is 0.0d
Data Types • Uses 4 bytes to represent a • Is a double-precision 64-bit
number floating-point
• Long • Float
• Default value is 0 • Default value is 0.0f
• Uses 8 bytes to represent a • Is a single-precision 32-bit
number floating-point
• Arithmetic operators (+, -, *, /, %)
• Relational operators (==, !=, >, <, >=, <=)
Operators • Logical operators (&&, ||, !)
• Instanceof (check if an object is a certain type) – return true or
false
• Ternary operator ( expression ? expression1 : expression2 )
• Example: 5 > 10 ? true : false
If (condition){
statement(s)
} else if (condition) {
statement(s)
} else {
statement(s)
Conditional Statements }
Switch(expression) {
Case value1:
code(s)
break;
default:
code(s)
}
1. write a program that checks if a number is odd or even
2. Write a program that takes a number between 1 and 7 and displays the
name of the weekday
3. Take three numbers from the user and print the greatest number
4. Write a Java program to find the number of days in a month
Exercise
5. Write a Java program that takes a single character from a user and prints
Vowel or Consonant, depending on the user input. If the user input is not a
letter (between a and z or A and Z), or is a string of length > 1, print an
error message.
6. Write a Java program that takes a year from a user and prints whether
that year is a leap year or not.
For loop
For (initialExpression; testExpression; updateExpression) {
statement(s)
}
Looping Statements
while loop
while (testExpression) {
statement(s)
}
Looping Statements
do…while loop
do {
statement(s)
} while(testExpression);
Looping Statements
• An array is a variable that can store multiple values of
same type
• Use the following syntax to declare an array
Array • DataType[] name = new DataType[size];
• An array can also be initialized during declaration
• DataType[] name = {val1, …, valn};
• To access element of an array use the name of the array
with its index
• 1. Create an array of number with a size of 10. Then write
a program that sorts the array in a ascending order. And
also write a program that searches a number from the
Exercise
array.
• 2. Using the array created in the previous question, write
a program that calculates average of the numbers in the
array
Thank you