1 data types, operations, and expressions overview l format of a java application l primitive data...

14
1 Data types, operations, and expressions Overview Format of a Java Application Primitive Data Types Variable Declaration Arithmetic Operations and Arithmetic Expressions Assignment Statement Associativity of Operators Preview: More Operators and Console Input

Post on 21-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

1

Data types, operations, and expressions

Overview Format of a Java Application

Primitive Data Types

Variable Declaration

Arithmetic Operations and Arithmetic Expressions

Assignment Statement

Associativity of Operators

Preview: More Operators and Console Input

Page 2: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

2

Format of a Java Application

A Java application consists of one or more classes

A Java class consists of one ore more methods (functions) one of which must be the main method

A method is a collection of instructions (statements) describing how to carry out a particular task..

The following is a simple Java application to print the the message "Hello World!“ on the screen.

public class Hello

{

public static void main ( String[] args )

{

System.out.println("Hello World!");

}

}

Page 3: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

3

Format of a Java Application – Contd.

The Statement: public class Hello starts a new class named Hello

public means the class is accessible to all other objects, we shall encounter private later.

The class containing the main method must be declared as public

It is up to you to give names to your classes provided you follow the following simple rules and conventions. A class name» must be one word (no spaces)

» must start with a letter

» must not contain special characters (+, &, etc)

» should start with capital letter

» If a class name contains more than one English words, each word should start with a capital letter (e.g. MyFirstClass)

» It should reflect the function of the class

A Java file must be saved with the name of the class it contains with the extension .java

If it contains more than one class, the name of the class containing the main method must be used.

Page 4: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

4

Format of a Java Application – Contd.

The Statement: public static void main starts the main method.

Again the main method must always be declared as public – most methods are private.

The main method must always be declared as static, to be explained later - most methods are not static

The parameter (String[],args) is required for the main method. It is used to pass command line arguments

Naming a method follow the same rule as naming a class, however, by convention, we shall always start a method with a small letter to differentiate it with a class.

If a method contains more than one English word, the subsequent words should start with a capital letter. E.g. myFirstMethod

The Statement: System.out.println(“Hello world”); prints a line of text “Hello world” on the standard output (the screen)

Page 5: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

5

Format of a Java Application – Contd.

The standard output is represented by an object called out contained in the class System and referred to as System.out

println is a method of the System.out object and can be used to print strings and numbers.

e.g. System.out.println(3+4); displays 7 When ever you use a method in Java, you must specify three

items:» The object containing the method (e.g. System.out )

» The name of the method (e.g. println)

» A pair of parenthesis which may contain any other information needed by the method (e.g. “Hello world”)

The println method prints the contents of its argument and move to the next line. E.g. the following prints two lines.

System.out.println(“ICS Department”);

Syetem.out.println(“KFUPM”); Another method of the System.out object is print. This

prints its argument but does not move to the next line. The following statements prints one line.

System.out.print(“ICS Department”);

Syetem.out.print(“KFUPM”);

Page 6: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

6

Primitive Data Types

Java has eight primitive data types as described below.

Type Size/Format Range

(whole numbers)

byte 8-bit two's complement -128 to 127

short 16-bit two's complement -32,768 to 32,767

int 32-bit two's complement about –2 billion to 2billion

long 64-bit two's complement about –10E18 to +10E18

(real numbers)

float 32-bit IEEE 754 -3.4E38 to +3.4E38

double 64-bit IEEE 754 -1.7E308 to 1.7E308

(other types)

char 16-bit Unicode character A single character

boolean true or false A boolean value (true or false)

Apart from these, any other information is represented in Java as an Object.

Page 7: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

7

Variables Declaration

You can declare a variable to hold a data value of any of the primitive types.

int counter;int numStudents = 583;long longValue;long numberOfAtoms = 1237890L;float gpa;float batchAverage = 0.406F;double e;double pi = 0.314;char gender;char grade = ‘B’;boolean safe;boolean isEmpty = true;

public class Example1{ public static void main ( String[] args ) { int payAmount = 123; System.out.println("The variable

contains: " + payAmount ); }}

Page 8: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

8

Arithmetic Operators

For whole/Real numbers:

OperatorUse Description

+ op1 + op2 Adds op1 and op2- op1 - op2 Subtracts op2 from op1 * op1 * op2 Multiplies op1 by op2/ op1 / op2 Divides op1 by op2% op1 % op2 Computes the remainder of

dividing op1 by op2

public class Example2{ public static void main ( String[] args ) { int hoursWorked = 40; double payRate = 10.0;

System.out.println("Hours Worked: " + hoursWorked );

System.out.println("pay Amount : " + (hoursWorked * payRate) );

}}

Page 9: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

9

Arithmetic Operation

Modes of operation :

If operand1 and operand2 are of same data type, then the resultant value of the operation will be of that same data type.

If operand1 and operand2 are of different data type like real and integer, then the resultant value of the operation will be of real data type. The real data type may be float or double.

e.g. 1/2 gives 0

1.0/2 gives 0.5

1.0/2.0 gives 0.5

Operand1 Operand2 Result

Real Real Real

Whole Whole Whole

Whole Real Real

Real Whole Real

Page 10: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

10

Arithmetic Expressions

Definition: An expression is a sequence of variables, constants, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value.

In Java, Arithmetic expressions are evaluated very similar to the way they are evaluated in algebra.

Operator Meaning Precedence

- unary minus highest

+ unary plus highest

* multiplication middle

/ division middle

% remainder middle

+ addition low

- subtraction low

e.g 78 - 12 / 4 75

2 + 6 / 2 – 9 -4

Page 11: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

11

Associativity of Operators

When operators of equal precedence appear in the same expression, associativity rule governs, which is to be evaluated first.

Left associativity rule

In Java, all binary operators except for the assignment operators are evaluated in left to right order.

2 * 7 * 3 4 - 2 + 5

----- -----

14 * 3 2 + 5

------- -------

42 7

Right associativity rule

Assignment operators are evaluated right to left.

int result;

result = ((a + b) / 2) % 3;

Page 12: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

12

Evaluating Expressions

Arithmetic Expression

int result, a = 3, b = 5;

result = ((a + b) / 2) % 3;

((a + b) / 2) % 3

(8 / 2) % 3

(8 / 2) % 3

4 % 3

1

Page 13: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

13

Assignment Statement

variable = expression; The value of constants / variables / expression in

the right side of the = operator, is assigned to the variable in the left side of the = operator.

Examples:a = 5;b = a;c = a + b;

The parameter in the left hand side of the = operator should be a variable.

It can not be a constant or an expression.

Example of an invalid assignment statement : a + b = c ;

Page 14: 1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations

14

Assignment Statement

Java allows multiple assignment.int start, end;int width = 100, height = 45,

length = 12;start = end = 0;

Note that whole integers appearing in the source code are taken to be ‘int’. So, one might wish to flag them when assigning to non-ints:

float maxGrade = 100f; // now holds ‘100.0’

double temp = 583d; // holds double precisionfloat temp = 5.5; // ERROR!

// Java treats 5.5 as a double to // retain maximal accuracy

Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L):

float maxGrade = 100F; // now holds ‘100.0’

long x = 583l; // holds 583, but looks like 5,381long y = 583L; // This looks much better!