0% found this document useful (0 votes)
3 views

CSBP219_SP25_Java_Week1

This document covers the basics of Java programming, including identifiers, literals, operators, variables, expressions, and data types. It introduces fundamental concepts such as primitive data types, arithmetic operations, type casting, and the importance of comments in code. Additionally, it explains how to create Java programs, use assignment statements, and parse numeric strings.

Uploaded by

banana80milk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSBP219_SP25_Java_Week1

This document covers the basics of Java programming, including identifiers, literals, operators, variables, expressions, and data types. It introduces fundamental concepts such as primitive data types, arithmetic operations, type casting, and the importance of comments in code. Additionally, it explains how to create Java programs, use assignment statements, and parse numeric strings.

Uploaded by

banana80milk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Week 1

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.

How can we learn a programming language?


We must have a fundamental knowledge of the language, and we must write & test
our programs on the computer to make sure they work correctly.
Slide 3

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.

 There are two kinds of comments:


 Multi‐line Comment: begins with /* and ends with */, and may span more than
Slide 6

one lines (as in Lines 1‐4).


 End‐of‐line Comment: begins with // and lasts until the end of the current line (as
in Lines 5 and 7).
6
Example of Java Symbols
Slide 7

7
Reserved Words (Keywords)
Reserved words are always lowercase.

Each reserved word is considered as a single symbol.


Slide 8

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

Primitive Data Types long


float
Slide 12

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:

class Members: declarative statements:


data members declare variables, etc.
methods executable statements:
perform calculations,
Slide 14

manipulate date, input &


output, etc.

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.

the decrement operator – –, which


decrements the value of a variable by 1.
Slide 17

17
Increment & Decrement Operators
Example:
Slide 18

18
Assignment Statements (cont.)
Java provides five compound operators
+= , –= , *= , /= , %=

corresponding to the five arithmetic operators.


Slide 19

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

"The sum = "+ (12+26) => "The sum = 38"

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

Static and final are reserved words


Final specifies that the value stored in IDENTIFIER is fixed and cannot be
changed.
Slide 22

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:

import java.util.*; // imports necessary classes


import java.util.Scanner; // only imports Scanner class
Slide 23

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

 Convert a string consisting of a floating‐point


number to a value of the type float:
float num = Float.parseFloat("234.56") ;
// num will be 234.56
 Convert a string consisting of a floating‐point
number to a value of the type double:
double num = Double.parseDouble(“‐234.56") ;
// num will be ‐234.56
24

You might also like