Programming 1...Objective In this chapter you’ll: •The basic structure of the program in the...
Transcript of Programming 1...Objective In this chapter you’ll: •The basic structure of the program in the...
![Page 1: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/1.jpg)
Programming 1
Java Basic+ Java Data Types
![Page 2: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/2.jpg)
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
![Page 3: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/3.jpg)
What is java?
• A general-purpose object-oriented language.
• Write once Run Anywhere.
• Designed for easy Web/Internet applications
• Widespread acceptance
![Page 4: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/4.jpg)
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)
![Page 5: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/5.jpg)
A Simple Java Program• Let's write our first program in Java, to print a message identifying your name.
![Page 6: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/6.jpg)
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.
![Page 7: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/7.jpg)
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.
![Page 8: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/8.jpg)
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.
![Page 9: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/9.jpg)
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 }.
![Page 10: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/10.jpg)
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.
![Page 11: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/11.jpg)
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:
![Page 12: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/12.jpg)
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
![Page 13: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/13.jpg)
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.
![Page 14: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/14.jpg)
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
![Page 15: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/15.jpg)
Reserved words
![Page 16: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/16.jpg)
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.
![Page 17: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/17.jpg)
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.
![Page 18: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/18.jpg)
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?
![Page 19: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/19.jpg)
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
![Page 20: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/20.jpg)
What is a variable?
• Think of the variable x in the equation
• We can assign any value to the x .
![Page 21: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/21.jpg)
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
![Page 22: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/22.jpg)
Basic data types
Type
![Page 23: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/23.jpg)
Basic data types (Cont’d)
Can be classified in four Categories:
• Integers
• Floating Point
• Characters
• Boolean
![Page 24: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/24.jpg)
Most commonly used
![Page 25: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/25.jpg)
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
![Page 26: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/26.jpg)
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
![Page 27: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/27.jpg)
Make sure the values are valid!
• Assigned values must be appropriate for the type of data that is defined.
![Page 28: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/28.jpg)
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!
![Page 29: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/29.jpg)
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” .
![Page 30: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/30.jpg)
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”
![Page 31: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/31.jpg)
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
![Page 32: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/32.jpg)
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
![Page 33: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/33.jpg)
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
![Page 34: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/34.jpg)
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
![Page 35: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/35.jpg)
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.
![Page 36: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/36.jpg)
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
![Page 37: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/37.jpg)
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?
![Page 38: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/38.jpg)
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.
![Page 39: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/39.jpg)
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.
![Page 40: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/40.jpg)
The assigned value must be within the data type field.
![Page 41: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/41.jpg)
![Page 42: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/42.jpg)
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).
![Page 43: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/43.jpg)
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
![Page 44: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/44.jpg)
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.
![Page 45: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/45.jpg)
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.
![Page 46: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/46.jpg)
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
![Page 47: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/47.jpg)
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)
![Page 48: Programming 1...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](https://reader033.fdocuments.us/reader033/viewer/2022041508/5e266f2cb061f661e44bac6c/html5/thumbnails/48.jpg)
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