Вы находитесь на странице: 1из 22

LECTURE 2

BASIC ELEMENTS OF JAVA

programming
process of planning and creating a program

computer program
a sequence of statements intended to accomplish a task

programming language
a set of rules, symbols, and special words used to construct programs

A Java Program
//********************************************************
// This is a simple Java program. It displays three lines
// of text, including the sum of two numbers.

//********************************************************

public class ASimpleJavaProgram

{
public static void main(String[] args)

{
System.out.println("My first Java program.");
System.out.println("The sum of 2 and 3 = " + 5);
System.out.println("7 + 8 = " + (7 + 8));
}
}

Basics
syntax rules
tell you which statements (instructions) are legal, or accepted by the

programming language, and which are not

a violation of the syntax, or grammatical rules, of a natural language or a


programming language

semantic rules
determine the meaning of the instructions

a violation of the rules of meaning of a natural language or a


programming language

Basics
1.

2.

Comment

3.

Single-line:

//

Multiple-line:

/* and */

Special Symbols
considered a single symbol

Reserved Words (Keywords)


int,

float, double,
char, void, public,
static, throws, return

letters in a reserved word are

always lowercase
considered a single symbol

Basics
4.

Identifiers

names of things, such as variables, constants, and methods, that appear in programs

consists of letters, digits, the underscore character (_), and the dollar sign ($) and must
begin with a letter, underscore, or the dollar sign

can be made of only letters, digits, the underscore character (_), and the dollar sign ($); no
other symbols are permitted to form an identifier

Ex:
side Length

sideLength

angle!

angle

a+b

ab

2nd

nd

Note: Java is case sensitive

Data Types
set of values together with a set of operations on those values
Primitive Data Types
fundamental data types in

three categories
1.

integral

2.

floating-point

3.

boolean

Java

Arithmetic Operators and Operator Precedence


Java has 5 arithmetic operators:
1.

addition

2.

subtraction

3.

multiplication

4.

division

5.

mod (modulus or remainder)

Unary operator

Ex:

an operator that has only one

2+1*3

2, 1, 3 - operands

+,*

- operators

operand.
Ex:

-7

Binary operator
an operator that has two operands.

Ex: 7+8

Order of Precedence
*, /, %

(higher)

+, -

(lower)

Ex:
Evaluate 3 * 7 - 6 + 2 * 5 / 4 + 6
Answer:

23

Expressions
integral expression
floating-point or decimal expression
mixed expression

Two rules apply when evaluating a mixed expression:


1.

When evaluating an operator in a mixed expression:


If the operator has the same types of operands (that is, both are

integers or both are


floating-point numbers), the operator is evaluated according to the type of the operand.

If the operator has both types of operands (that is, one is an

integer and the other is a


floating-point number), during calculation the integer is treated temporarily as a
floating-point number with the decimal part of zero, and then the operator is evaluated.
The result is a floating-point number.

2.

The entire expression is evaluated according to the precedence


rules.

Evaluate the following expressions.


a. 13 / 4

b. 2 + 12 / 4
c. 21 % 5

d. 3 - 5 % 7
e. 17.0 / 4

f. 8 - 5 * 2.0
g. 14 + 5 % 2 3

h. 15.0 + 3.0 / 2.0

Evaluate the following expressions.


a. 13 / 4

=3

b. 2 + 12 / 4

=5

c. 21 % 5

=1

d. 3 - 5 % 7

= -2

e. 17.0 / 4

= 4.25

f. 8 - 5 * 2.0

= -2.0

g. 14 + 5 % 2 3

= 12

h. 15.0 + 3.0 / 2.0

= 16.5

Type Conversion (Casting)


(dataTypeName)

expression

Class String
Strings
data values that contain more than one character

a sequence of zero or more characters


enclosed in double quotation marks (not in single quotation marks,

as are the char data types)

String and the operator +


the operator +
used to concatenate (or join) two strings as well as a string and a numeric value or a

character.

Ex:

"Sunny" + " Day" evaluates to Sunny Day

"Amount Due = $" + 576.35 evaluates to "Amount Due =


$576.35"

"The sum = " + 12 + 26 evaluates to "The sum = 1226"

"The sum = " + (12 + 26) evaluates to "The sum = 38"

Increment and Decrement

Ex:
x = 7;
y = ++x;

x = 7;
y = x++;

Packages, Classes, Methods


package
a collection of related classes

class
used to create Java programs,

either application or applet

used to group a set of related operations

used to allow users to create their own data

types

method
set of instructions designed to accomplish a specific task

Ex:

Importing

class Math
in java.lang package
abs, cos, exp, max,

import packageName.*;

atan, sqrt,

etc
class Scanner
in java.util package

next, nextInt, nextDouble,

useDelimiter

import

java.util.*;

import

java.util.Scanner;

Creating Java Application


syntax of a class to create a Java application program

Вам также может понравиться