Java: data types,
variables, expression,
statements, operators.
DR. ADENIYI JIDE KEHINDE
Your best quote that reflects
your approach… “It’s one small
step for man, one giant leap for
mankind.”
- NEIL ARMSTRONG
Basic structure/Hello World
o The basic structure of a java code is presented in the code below:
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
}
} // end of class HelloWorld
o The command that actually displays the message is: [Link]("Hello
World!");
12/07/2025
Basic structure/Hello World
o It uses a "built-in subroutine" named [Link] to do the actual
work.
o A subroutine consists of the instructions for performing some task,
chunked together and given a name.
o // - in the code is a single line comment in java.
o /* text */ - in the code is a multiple line comment
o /** text **/ - is also a multiple line comment but its special because it is a
"Javadoc" comment that can be used to produce documentation for the
program.
o All programming in Java is done inside "classes“.
12/07/2025
Basic structure/Hello World
o public class HelloWorld – in the code introduces the class name
as HelloWorld. This also serves as the name of the program.
o To define a program, there is a need for the main subroutine,
this is the next three lines of code after the class declaration.
o public static void main(String[] args) {
o [Link]("Hello World!");
o }
o When the java interpreter is called to run the code, it calls the
main() subroutine and executes the instruction in it.
12/07/2025
Basic structure/Hello World
The word "public" in the first line of main() means that
this routine can be called from outside the program.
Java is case sensitive.
{ … } – are used for code block and if opened must be
closed.
12/07/2025
General program format
public class program-name {
optional-variable-declarations-and-subroutines
public static void main(string[] args) {
statements;
}
optional-variable-declarations-and-subroutines
}
12/07/2025
Java Identifiers
According to the syntax rules of Java, the most basic names
are identifiers.
Identifiers can be used to name classes, variables, and
subroutines.
Rules for forming identifiers:
◦ It must begin with a letter or underscore.
◦ It is made up of letters, digits, and underscores.
◦ No spaces are allowed in identifiers.
◦ Upper case and lower-case letters are considered to be
different. 12/07/2025
Exercise (Valid and Invalid
Identifiers)
Identifier Valid/Not Valid
B1
HelloWorld
Hello World
Hello_world
public
else
if
A14_
12/07/2025
Variable
A name used in this way -- to refer to data stored in memory -- is
called a variable.
Syntax for Variable Declaration:
◦ typeName variableName = value; OR typeName variableName;
A variable in Java is designed to hold only one particular type of data;
it can legally hold that type of data and no other.
◦ Java is strongly typed language because it enforces this rule.
◦ The variable type can be one of three things:
◦ 1. One of the eight basic primitive data types (-The primitive
types are named byte, short, int, long, float, double, char, and
boolean )
◦ 2. The name of a class 12/07/2025
Assignment Operator
◦ In Java, the only way to get data into a variable -- that
is, into the box that the variable names -- is with an
assignment statement.
◦ i.e. variable = expression;
◦ rate = 0.07; or interest = rate * principal;
◦ The assignment operator is the “=” sign
12/07/2025
Literals
o In a program, you represent constant values as literals.
o A literal is something that you can type in a program to represent a
value.
o For example, to type a value of type char in a program, you must
surround it with a pair of single quote marks, such as 'A', '*', or 'x’.
o The character and the quote marks make up a literal of type char
o Certain special characters have special literals that use a backslash
(\):
o a tab is represented as ‘\t’; a carriage return as ‘\r’; a linefeed as ‘\n’;
the single quote character as ‘\‘;
o and the backslash itself as '\\' 12/07/2025
String and String Literals
o Single quotes are used for char literals and double quotes for
String literals!
o There is a big difference between the String "A" and the char
'A’.
o To represent the string value
o I said, "Are you listening!"
o with a linefeed at the end, you would have to type the string
literal:
o "I said, \"Are you listening!\"\n"
12/07/2025
String Operations
o String is a class, and a value of type String is an object.
o That object contains data, namely the sequence of characters that
make up the string.
o It also contains subroutines. All of these subroutines are in fact
functions.
o For example, advice might have been declared and assigned a value
as follows:
o String advice;
o advice = "Seize the day!";
o Then [Link]() is a function call that returns the number of
12/07/2025
String Operations
o The String class defines a lot of functions. Here are
some that you might find useful.
o Assume that s1 and s2 are variables of type String:
o [Link](s2); [Link](s2); [Link]();
[Link](N); [Link](N,M); [Link](s2);
[Link](s2); [Link]();
[Link]() ; [Link]()
o Strings are concatenated with “+”
12/07/2025
Variables
o A variable can be used in a program only if it has first been declared.
o A simple variable declaration takes the form:
o type-name variable-name-or-names;
o For example:
o int numberOfStudents;
o String name;
o double x, y;
12/07/2025
Variables
o boolean isFinished;
o char firstInitial, middleInitial, lastInitial;
o double principal; // Amount of money invested.
o double interestRate; // Rate as a decimal, not percentage.
o For now, we will only use variables declared inside the main()
subroutine of a program.
o Variables declared inside a subroutine are called local
variables for that subroutine.
12/07/2025
Variables
oJava is strongly typed because rules are enforced.
oThere are eight so-called primitive types built into Java. They
are: Primitive Type Size
byte
Int - 32 -2147483648 to 2147483647
short -32768 to 32767
float
double
long -9223372036854775808 to
9223372036854775807
char
boolean
12/07/2025
Expression/ Operators
o An expression is a piece of program code that represents or
computes a value:
o Arithmetic operators include addition, subtraction, multiplication,
and division.
o They are indicated by +, -, *, and /, %.
o If your program tells the computer to combine two values of
different types, the computer will convert one of the values from
one type to another.
o For example, to compute 37.4 + 10, the computer will convert
the integer 10 to a real number 10.0 and will then compute
37.4 + 10.0. This is called a type conversion. 12/07/2025
counter = counter + 1; goalsScored = goalsScored + 1;
Incrementing/Decrementing
Incrementing simply adds a constant number (usually 1) and
decrementing does the reverse.
counter = counter + 1; //Incrementing by 1
counter = counter – 1; //decrementing by 1
This can be shortened to:
counter++; //(post) or
++counter; //(pre)
Decrementing follow the same pattern
12/07/2025
Relational Operators
Java has boolean variables and boolean-valued expressions that
can be used to express conditions that can be either true or
false.
A == B Is A "equal to" B?
A != B Is A "not equal to" B?
A < B Is A "less than" B?
A > B Is A "greater than" B?
A <= B Is A "less than or equal to" B?
A >= B Is A "greater than or equal to" B?
A || B is A or B
A && B is A and B
!B is not B
Precedence: always keep expressions in bracket
12/07/2025
Example: Calculate interest
public class Interest {
public static void main(String[] args) {
/* Declare the variables. */
double principal; // The value of the investment.
double rate; // The annual interest rate.
double interest; // Interest earned in one year.
12/07/2025
Cont.
/* Do the computations. */
principal = 17000;
rate = 0.027;
interest = principal * rate; // Compute the interest.
principal = principal + interest;
// Compute value of investment after one year, with
interest.
// (Note: The new value replaces the old value of principal.)
/* Output the results. */
12/07/2025
Cont.
[Link]("The interest earned is $");
[Link](interest);
[Link]("The value of the investment after one year
is $");
[Link](principal);
} // end of main()
} // end of class Interest
12/07/2025
import [Link]; // Import the Scanner class
Assignment
o Write a Java program that swaps two numbers using only three
variable which includes those holding the two numbers.
o Write a Java program that calculates the volume of a sphere Take
input using the code:
Scanner scanner = new Scanner([Link]);
int num = [Link]();
Remember to import the scanner class with:
import [Link]; // Import the Scanner class
12/07/2025
12/07/2025