Chapter 2: Basic Elements of Java
Chapter 2: Basic Elements of Java
Chapter 2: Basic Elements of Java
Java Programming:
From Problem Analysis to Program Design,
Second Edition
Chapter Objectives
Become familiar with the basic components of a Java
program, including methods, special symbols, and
identifiers.
Explore primitive data types.
Discover how to use arithmetic operators.
Examine how a program evaluates arithmetic
expressions.
Explore how mixed expressions are evaluated.
Decimal Logical
integers values
numbers
Mixed expressions
Named constant
A memory location whose content cannot be
changed during program execution.
Declared by using the reserved word final.
Initialized when it is declared.
Example 2-12
double amountDue;
int counter;
char ch;
int x, y;
i = 4;
j = 4 * 5 - 11;
sale = 0.02 * 1000;
first = 'D';
str = "It is a sunny day.";
23 7
Example 2-20
String str;
int num1, num2;
num1 = 12;
num2 = 26;
str = "The sum = " + num1 + num2;
Methods:
print
println
Syntax:
System.out.print(stringExp);
System.out.println(stringExp);
System.out.println();
variable += expression;