programming 1 3- java basic and data types.pdfa simple java program (cont’d) • build the...

Post on 02-Jun-2020

16 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Programming 1

Java Basic+ Java Data Types

Atheer Aldwighri

Objective

In this chapter you’ll:

• The basic structure of the program in the Java language

• Understand the benefits of variables, constants, and reserved words

• Identify the types of data

• Define the variable in Java and assign a value to it

What is java?

• A general-purpose object-oriented language.

• Write once Run Anywhere.

• Designed for easy Web/Internet applications

• Widespread acceptance

The basic structure of the program in the Java language

• The program in any programming language consists of a set of sentences and key words, the simplest program can not work without them, as follows

• Java program begins with a class definition in which the keyword class is followed by the class name.

• Java program must have a main method where program execution begins.

• Opening and closing braces { }.

• Output and input statements ( the program may work without it)

A Simple Java Program• Let's write our first program in Java, to print a message identifying your name.

A Simple Java Program (Cont’d)

• Build the program: public class

• Public: means that what is inside this object can be used by any other object, it is considered (public) within this project.

• Class: means an object which is the basic structure of Java, every Java program must have at least one class.

• helloworld (class name): It is the identifier for this class and is changeable depending on the nature of the class.

Basic function (main)

• Is the basic function of all programming projects in Java.

• The program is executed from the main method, It has a clear and stable composition as you see.

Basic Function Components (main)• The main function consist of:

• Signature: one of the methods must be called main and must be

defined. Methods perform tasks and can return information when

they complete their tasks. We’ll explain the purpose of keyword

static later, keyword void indicates that this method will not return

any information. Later, we’ll see how a method can return

information, the String[] args in parentheses is a required part of the

method main’s declaration later we’ll see it.

Basic Function Components (main) (Cont’d)

• Body: is the area in which they are written commands and instructions

that represent the task that we want to implement.

• Beginning and the End ({ }): braces that determine the beginning

and end of a function object the beginning is { and the end is }.

Programming language components

• Any programming language has a set of components whose representation may vary from language to language, they are:

• Punctuators.

• Reserved words.

• Variables – Identifiers.

• Constants.

• Operators.

Punctuators

• It is known when writing any language using the so-called punctuators

• Their goal is:

• Determine the beginning or end of some sentences.

• Separate sentences even if you type them in one line.

• There are several punctuators marks we will review in the following table:

PunctuatorsSymbol Pronounced Used For

; Semicolon Ending an instruction

, Comma is used to separate items in sets/lists

. Dot Two purposes: 1. Indicating ownership of a

method, variable, or constant (Java only)

2. Separator for whole & fractional parts

of a real number

“ ” Double quotes Surround the text of a String literal

' ' Single quotes Surround text of a char literal

{ } Brackets (braces) of code block Indicate the start ({) and end (}) of a block

of code

( ) Passed parentheses values Argument list in method (function) calls,

grouping in expressions

// Slash slash Single-line comment

/* */ Slash-star / Star-slash Mark the beginning an ending of multiline

comments

Programming language components

• Any programming language has a set of components whose representation may vary from language to language, they are:

• Punctuators.

• Reserved words.

• Variables – Identifiers.

• Constants.

• Operators.

Reserved words

• Java reserved words are keywords that are reserved by Java functions or other uses that cannot be used as identifiers (e.g., variable names, function names, class names). If a reserved word was used as a variable, you would get an error or unexpected result.

• These words cannot be used or customized for any unallocated function within the translator.

• All are written in small letters

CAUTION: Java is a case-sensitive language. The translator

considers the word Name different from the word name

Reserved words

Programming language components

• Any programming language has a set of components whose representation may vary from language to language, they are:

• Punctuators.

• Reserved words.

• Variables – Identifiers.

• Constants.

• Operators.

Variables

• Variables are reserved positions in memory of different sizes, where data is stored to be processed and used to accomplish certain work, these position named by identifiers.

• By naming the variable, it is easy to handle and process the data stored within the program.

• These variables have different types depending on the type of data we need such as integer numbers, decimal numbers, text, characters …… etc.

Variables

• We use variables and define them for several reasons:

• Link the variable name to address in memory.

• Determine the type of data that will be stored in this variable, thus determine the amount of memory needed to store this data.

• The computer can handle the values correctly when converted to binary system.

What kind of data

does this program

require?

The nature of the variables

0 0 0 1 1 1 0 0

Type1 x = 28 ; // 1-byte memory space will be allocated

Type2 y; //2-byte memory space will be allocated

Once this definition is allocated space in the memory to store the size of a small number to be suitable for the type.

A part of memory

What is a variable?

• Think of the variable x in the equation

• We can assign any value to the x .

Variables

• To deal with variables, you must specify the following:

• The type of data to be stored.

• Variable name.

• You can also assign a value to the variable

Basic data types

Type

Basic data types (Cont’d)

Can be classified in four Categories:

• Integers

• Floating Point

• Characters

• Boolean

Most commonly used

Define the variable

• Java “language that restricts data types”.

• You must define the type of data that the variable will process with keywords.

• After the variable is define.

• The variable will become exist.

• No need to define it again

Type name

Options to define the variable and assign a value to it

a) Declare the variable and set it in a single line.

b) Declare a variable in a line and assign a value to it later.

Type name value

Make sure the values are valid!

• Assigned values must be appropriate for the type of data that is defined.

Make sure the values are valid!

• Any numeric value can be set to x.

• But a string value cannot be set to x.

• This is illogical!

The disadvantages of not using variables

• The instruction (code) becomes inflexible.

• To replace the name “Alex” , you will need to make several changes to multiple placements.

• Perform a boring editing process.

• The risk of forgetting one of the repetitions of the name “Alex” .

Benefits of using variables

• The instruction (code) becomes flexible.

• The ability to remember and manipulate values is available.

• To replace the name “Alex” we will make one change:\

• An efficient editing process.

• Avoid the risk of forgetting any of the repeated name “Alex”

Program (code) errors in variables

• There are some bugs (errors) in the following code, Find out what these mistakes are!

• Eclipse put a line under the instruction (code) that includes a problem.

• Place the cursor over the instruction (code) or the icon in the left margin for details.

• Eclipse may suggest possible solutions. Click the icon in the left margin

Program (code) errors in variables

• There are some bugs (errors) in the following code, Find out what these mistakes are!

The assigned value does

not match the data type

The variable is not already

defined

Java is case-sensitive and should be

written as defined in line 14

You cannot define a variable twice or create

two variables with the same name

Program (code) errors in variables

• Assign values that are not appropriate for the variable type

• Forget about defining the type of the variable.

• Writing a variable in the wrong way.

// Java is case sensitive

Program (code) errors in variables

• Declare the same variable twice.

• Forget about assign a value before using a variable.

Variables are printed without using quotation marks “ “. The

name is typed directly. If we want to print more than one

variable, we use the + sign to link the variables.

int

x= 5 ; int y= 2

System.out.println( x+” “+y)

The result is 5 2

Noticeable

• It can declare many variables in one line.

• It can assign values when many variables are declared.

• It is up to personal preference with regarding to….

• Declare each variable in a single line.

• Declare all variables of a specific type in one line.

Naming is not valid for variables

• You can use any name you want on the variable.

• That might be fun, but...

• Do you or a friend understand what dsfdsfsplxp represents when reading instruction (code)?

• It is not recommended to use micro name.

• This name maybe useful in exam….

• Do you or a friend understand what data unit X represents when reading instruction (code)?

// ha ha ha

Naming is not valid at all for variables

• Variables cannot share the same name.

• You can not start naming variables with numbers.

• Reserved words cannot be used as variable names.

• Reserved words turn into color ……….

• Reserved words have special meanings in Java.

// what variable x?

Identifiers

• They are defined names used as variable names, classes, methods and constants.

• The identifiers name consists of English letters ( A Z , a z ), numbers (0-9), and special symbols $,_

• When choosing an identifying name, six rules must be taking into account:

• The name starts with a letter.

• The name cannot begin with a number.

• It does not contain any white space.

• It should not be reserved names.

• It's a good idea to give your variables mnemonic names that are closely related to the values they hold.

• It does not contain any letters or other special marks are not mentioned earlier.

Methods of naming variables

• Start each variable with a lowercase letter. Later words must begin with a capital letter:

• myVariable.

• Choose easy-to-remember names that anyone can see can understand the purpose of the variable.

• Remember that….

• Names are case sensitive.

• Names cannot include a space.

The assigned value must be within the data type field.

When we use each type of data?

frequently nevernever

Not usually used

• Both types of byte and short are used to save space in memory consumed on older or

smaller devices.

• But modern desktops include large amounts of memory.

• Of these four types, we will use int more in this course (programming 1).

Assign more than one value to the variable X

Find X

• X equals 20

• Until you set x to another value.

• The calculated value can be set to x.

• The final value of x is 8.

value

Programming language components

• Any programming language has a set of components whose representation may vary from language to language, they are:

• Punctuators.

• Reserved words.

• Variables – Identifiers.

• Constants.

• Operators.

Constants

• It is a fixed values used in programs, intended to be constant that they do not change when you run the program every time.

• To change it must modify the program code and then restart the program.

• Constants are divided into four types:

• Integers such as 1, -49, 105

• Decimal numbers 1.5, 0.34, -2.9

• Characters and this type cannot be typed directly so we use the punctuation mark ‘ ‘ to determine this character, such as ' a', ' b’

• Text constants (texts) are a set of consecutive characters that are used to express a particular sentence, also, in order not to confuse the compiler with the code, it must be placed between the double quotations “ " which are used to specify the beginning and end of a constant or a text variable.

Constants

• Constants are variables whose value cannot be changed after they are defined and assigned a value, and to make any variable immutable we use the reserved word final.

• You will see an error if you later try to change its value.

// not allowed

Programming language components

• Any programming language has a set of components whose representation may vary from language to language, they are:

• Punctuators.

• Reserved words.

• Variables – Identifiers.

• Constants.

• Operators. (We’ll see it in the next lecture)

Summary

• The basic structure of the program in the Java language

• Understand the benefits of variables, constants, and reserved words

• Identify the types of data

• Define the variable in Java and assign a value to it

top related