CSBP219_SP25_Java_Week1
CSBP219_SP25_Java_Week1
Basics of Java
Identifiers, Literals, Operators, Variables, Expressions, and Data types
Objectives
In this chapter you will learn:
• Basic Java components (methods, special symbols, and identifiers)
• Primitive data types
• Arithmetic operators and evaluation of arithmetic expressions
• Type casting
• String type
• Assignment statement
• Use of increment and decrement operators
Slide 2
2
Introduction
What is a computer program?
A computer program or a program is a sequence of statements written in a
programming language intended to accomplish a task.
What is programming?
Programming is a process of planning & creating a program.
3
A Java Application Program ASimpleJavaProgram.java
Slide 4
4
Java Basics
Every programming language has its
special symbols
words
syntax rules
The syntax rules identify which statements are accepted by the
programming language and which are not.
The semantic rules determine the meaning of the statements.
Programming language:
A set of rules , symbols , and special words used to construct programs
Slide 5
5
Comments
The program should be
understandable not only to the
developer but also to the
reader of the program.
A good program includes
comments in it.
Comments are for a reader ,
not for the compiler.
When compiler compiles a
program, it ignores comments.
7
Reserved Words (Keywords)
Reserved words are always lowercase.
8
Identifiers
Identifiers are names of things, such as variables, constants , methods,
classes , that appear in programs.
A Java identifier consists of only letters , digits , the underscore character
(_), and the dollar sign ($).
An identifier must begin with a letter , the underscore or the dollar sign.
Java is case sensitive : Number , NUMBER , number are different
identifiers.
A Java identifier cannot be a reserved word
Slide 9
9
Identifiers
Example: Valid or Invalid?
Slide 10
10
Data Types
The objective of a Java program is to manipulate data.
Different programs manipulate different data:
A program designed to calculate the area of a triangle will do some math
operations on numbers.
A program designed to build up the students list will manipulate names (words).
Java classifies data into different types: only certain operations can be
performed on a particular type of data:
We cannot multiply names, or concatenate numbers.
Data type is a set of values together with a set of operations on these
values.
Slide 11
11
Data Types
The primitive data types are fundamental data types in Java:
integrals: data types dealing with integers(numbers without decimal parts).
floating‐point: data types dealing with decimal numbers.
boolean: data types dealing with logical values.
char
byte
Integral short
int
Floating‐point
double
Boolean
12
Variable
Variable: a memory location whose content may change during program
execution.
The syntax to declare one variable or multiple variables is:
Examples:
Slide 13
13
Creating a Java Program
The syntax of a class: The syntax of the main method:
14
Java Application Program
Slide 15
15
Putting Data into Variables
Two common ways to place data into a variable are:
Use an assignment statement.
Use input(read) statements.
Assignment statement:
The value of expression should match the data type of the variable.
The expression is evaluated & its value is assigned to the variable.
The equal sign (=) is called the assignment operator.
Slide 16
16
Arithmetic Operators
Java has five arithmetic Java provides:
operators: the increment operator ++, which
increments the value of a variable by 1.
17
Increment & Decrement Operators
Example:
Slide 18
18
Assignment Statements (cont.)
Java provides five compound operators
+= , –= , *= , /= , %=
19
Operator + for Strings
The operator + can be used to concatenate (join) two strings
a string and a numeric value or a character
Example:
"Sunny" + " Day" evaluates to "Sunny Day"
Amount = "$"+ "576.35" evaluates to Amount = "$576.35"
Exercises:
"The sum = "+12+26 => "The sum = "+ "12"+"26" =>"The sum = 1226"
Slide 20
20
Type Conversion (Casting)
In a mixed expression 5.4/ 2, the integer 2 is treated as a floating‐point
number 2.0.
When a value of one data type is automatically treated as another type,
an implicit type coercion has occurred.
Java provides explicit type conversion through cast operator:
Example:
Slide 21
double x = 5.5;
int y = (int) x;
21
Named Constants
Named constant: a memory location whose content is not allowed to
change during program execution.
The syntax to declare a named constant is
Example:
final int NO_OF_PAGES = 900;
22
Packages, Classes & Methods
To use existing classes, methods, identifiers, we must tell the program
which package contains them.
The general syntax to import the contents of a package:
Example:
23
Parsing Numeric Strings
Convert a string consisting of an integer to a value of
the type int:
int num = Integer.parseInt("6723") ;
// num will be 6723