30
CS110: PROGRAMMING LANGUAGE I Lecture 3: Java Basics (I) Computer Science Department

CS110: PROGRAMMING LANGUAGE I - … consist of fixed text and format specifiers. Fixed text is output as it would be by print or println. Each format specifier is a placeholder for

  • Upload
    dodat

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

CS110: PROGRAMMING

LANGUAGE I

Lecture 3: Java Basics (I) Computer Science

Department

Lecture Contents:

Dr. Amal Khalifa,2014

2

A java program

Java language Basics

Comments

Reserved words

Identifiers

The main method

Text Output

Strings

Arithmetic Operators, Expressions, and Operator Precedence

A java Program

Dr. Amal Khalifa,2014

1-3

Class in file .java

class keyword

braces { , } delimit a class body

main Method

// indicates a comment.

white space

Java is case sensitive

“Everything must be in a class”

There are no global functions or global data.

Java Basics : comments

Compiler ignores comments.

Used to document programs and improve their readability.

Comments

// Fig. 2.1: Welcome1.java

A comment that begins with // is an end-of-line comment—it

terminates at the end of the line on which it appears.

Traditional comment, can be spread over several lines as in

/* This is a traditional comment. It can be split over multiple lines */

Javadoc comments

Delimited by /** and */.

Enable you to embed program documentation directly in your

programs.

Be careful!!

Java Basics : reserved words

Dr. Amal Khalifa,2014

6

Keywords (Appendix C) are reserved for use by

Java and are always spelled with all lowercase

letters

public

static

class

void

Java Basics : identifiers

Dr. Amal Khalifa,2014

7

A class name is an identifier—a series of characters

consisting of letters, digits, underscores (_) and dollar

signs ($) that does not begin with a digit and does not

contain spaces.

Java is case sensitive—uppercase and lowercase letters

are distinct—so a1 and A1 are different (but both

valid) identifiers.

By convention, begin with a capital letter and

capitalize the first letter of each word they include

(e.g., SampleClassName).

Java basics: main method

Declaring the main Method public static void main( String[] args )

Starting point of every Java application.

Parentheses after the identifier main indicate that it’s a program building block called a method.

Java class declarations normally contain one or more methods.

main must be defined as shown; otherwise, the JVM will not execute the application.

Methods perform tasks and can return information when they complete their tasks.

Keyword void indicates that this method will not return any information.

Java Basics: Text output

Dr. Amal Khalifa,2014

9

Statement System.out.println("Welcome to Java Programming!");

Instructs the computer to perform an action

Print the string of characters contained between the double quotation marks.

A string is sometimes called a character string or a string literal.

White-space characters in strings are not ignored by the compiler.

Strings cannot span multiple lines of code.

Example

System.out

object

Multiple line

O/P

Formatting

output

statements

end with a

semicolon

Escape Sequences

Dr. Amal Khalifa,2014

"Extend" character set

Backslash, \ preceding a character

Instructs compiler: a special "escape

character" is coming

Following character treated as

"escape sequence char"

Common escape sequences 12

Example

String Literals

Dr. Amal Khalifa,2014

14

A literal is a constant value that appears directly

in the program.

A String literal consists of a group of characters.

Ex:

"Welcome to Java"

15

String concatenation

Dr. Amal Khalifa,2014

16

// Three strings are concatenated

"Welcome " + "to " + "Java"

// String Chapter is concatenated with number 2

"Chapter" + 2

// String Supplement is concatenated with character B

"Supplement" + “b”

Example : program output 17

Dr. Amal Khalifa,2014

Public class Test1 {

public static void main(String args[]){

String msg;

String msg2= + msg;

System.out.print ("message1 =“ + "I" +

"Like“);

System.out.println ("\n message 2 ="

+ "Java“ + "\t"+ “end”);

}

}

Displaying Text with printf

Dr. Amal Khalifa,2014

18

System.out.printf method f means “formatted”

displays formatted data

Multiple method arguments are placed in a comma-separated list.

Java allows large statements to be split over many lines.

Method printf’s first argument is a format string May consist of fixed text and format specifiers.

Fixed text is output as it would be by print or println.

Each format specifier is a placeholder for a value and specifies the type of data to output.

Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type.

Format specifier %s is a placeholder for a string.

Example

The asterisk (*) indicates multiplication

The percent sign (%) is the remainder operator

The remainder operator, %, yields the remainder after division.

Java basics : Arithmatic operators

The arithmetic operators are binary operators because

they each operate on two operands.

Java Basics: Expressions

Dr. Amal Khalifa,2014

21

Examples:

5 / 2 yields an integer 2.

5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Remainder is very useful in programming.

an even number % 2 is always 0 and

an odd number % 2 is always 1.

So you can use this property to determine whether

a number is even or odd

Operator precedence

Dr. Amal Khalifa,2014

22

Rules Multiplication, division and remainder operations are applied first.

If an expression contains several such operations, they are applied from left to right.

Multiplication, division and remainder operators have the same level of precedence.

Addition and subtraction operations are applied next.

If an expression contains several such operations, the operators are applied from left to right.

Addition and subtraction operators have the same level of precedence.

Parentheses are used to group terms in expressions in the same manner as in algebraic expressions.

If an expression contains nested parentheses, the expression in the innermost set of parentheses is evaluated first.

Operator precedence

More examples

What is the order of evaluation in the following expressions?

3 + 4 + 5 + 6 + 2

4 3 2

3 + 4 * 5 - 6 / 2

3 2 4 1

3 / (4 + 5) - 6 % 2

2 3 4 1

3 / (4 * (5 + (6 - 2)))

4 1 2 3

1

25

Case study: Writing Expressions

Dr. Amal Khalifa,2014

26

Write a program that computes and displays the

result of the following expressions

3 + 4 + 5 + 6 + 2

3 + 4 * 5 - 6 / 2

3 / (4 + 5) - 6 % 2

3 / (4 * (5 + (6 - 2)))

Individual Arithmetic Precision

Dr. Amal Khalifa,2014

1-27

Calculations done "one-by-one"

1 / 2 / 3.0 / 4 performs 3 separate divisions.

First 1 / 2 equals 0

Then 0 / 3.0 equals 0.0

Then 0.0 / 4 equals 0.0!

So not necessarily sufficient to change just "one

operand" in a large expression

Must keep in mind all individual calculations that will be

performed during evaluation!

Debugging

Dr. Amal Khalifa,2014

28

errors are called bugs.

When you type a program, typos and unintentional

syntax errors are likely to occur.

Therefore, when you compile a program, the

compiler will identify the syntax errors.

Debugging means to identify and fix syntax errors.

Find and fix error!!

Example 29

Dr. Amal Khalifa,2014

class Test2 {

public static void main (String[] args)

{

System.out.println("*\n java \n*”);

System.out.println("I" * ‘Love’);

System.out.println(3 + 2)

}

Text Book:

[2] Chapter 2: 2.1 2.7

That’s all for today !! 30

Dr. Amal Khalifa,2014