SlideShare a Scribd company logo
Meeting 1: Java Revision
Introduction to programming usingJava
Part 1
1
• Introduction
• Java background
• The system & Getting Java running
• Data types
• Statements and Scope and Operators
• Conditional processing
• Repetitive processing
2
1. Introduction
• During the early daysof the internet, SunMicrosystems
introduced the Javalanguage, with great fanfare, in 1995.
Javaimmediately generated huge interest and excitement –
useof the language took off more rapidly than any computer
language before or since.
• Javais the language most identified with the internet and its
friendly face, theweb.
• Currently, Java is owned by Oracle.
• In this course, you will learn how Javaoriginated and how it is
usednowadays.
• you will learn the language used to build systemsthat power
somuch of the web inapplications ranging from e-commerce
to online games, downloadable music to online banking. 3
1. Introduction
• Javaruns on servers, PCs,mobile phones and PDAs(personal
digital assistants).
• It canbe foundin
– TVset-topboxes,
– in embedded devices (forming part of larger systems suchascars,
robots or printers),
– in smart cards,and
– wearable computers.
Javareally is everywhere.
4
2. Javabackground
The aims of the Java language
• Simple to learn, & Familiar and closely based on C++,but reduced in complexity.
• Object-oriented: java programs are structured aroundobjects
• Robust: Javaprograms are strictly checkedby software before theyrun
• Secure: Javaensures that programs running over networks cannot damageyour
computer files or introduceviruses.
• Portable: Javaprograms caneasily be transferred from one platform (e.g.
Windows) to run on another platform (e.g. Linux) with little or no change.
• High performance : Javaprograms canrun fast enough for whatis needed
• Interpreted: akey aspect of Javaportability.
• Threaded: allow aprogram todo several things at once.
• Dynamic: Javaprograms canadapt to changes in their environmenteven while
the program isrunning.
5
2. Javabackground
Versions &Editions of Java
Versions of Java
• Thefirst publicly available version wasJava1.0.
• Now, Java2 is the standard language
– and there have been anumber of further versions (Java2 version 1.3,Java2
version 1.4, Java2 version 1.5 and soon), each introducing relatively minor
improvements
Editions of Java
• Why Javaeditions was introduced?
– tocater for the different needs of, say,large international business systems
to software running on mobile phones with very limited hardware
resources.
• Current editions:
• Java2 Standard Edition (J2SE) - focus of our study!
• Java2 Enterprise Edition (J2EE) - for large-scale systems
• Java2 Micro Edition (J2ME) - for mobile phones
6
2. Javabackground
Asimple Javaprogram
Theoutput:
7
2. Javabackground
• Thefirst line of the program introduces aclass, calledHelloWalrus.
• When the program runs, the classgives rise to an object, which executes the code of the class.
• This classis defined aspublic, which means that objects ofthis classcan be freely accessed by
other objects in alargersystem.
• Thecurly bracket ‘{‘ on the line following HelloWalrus matches the closing bracket ‘}’ at the end
of the example, asthese enclose the whole classdefinition.
• The code in this classis in the form of a method, in this casea method called main.
• Thefirst line of the method is called the method header.
• Wedefer explaining the words static and void until later, but they are always used with main
methods like thisone.
• Themethod header defines the name of the method (in this case,main) and is followed by the
method body, which is enclosed in curly brackets.
• TheSystem.out.println method displays, in ascreen window, the exact text betweenthe
quotes.
• Eachof these two lines of code constitutes a statement, asingle command to be carried out.
• Eachstatement is terminated by asemicolon ‘;’.
8
2. Javabackground
Using comments in Javaprograms
• Acomment is ignored by the computersystem
– Form 1: Block Comment. Textenclosed in the symbols /* and */
– Form 2: Line Comment or In-line Comment (using//)
• Line comment:
• In-line comment:
9
3. The System & GettingJavarunning
Here we discuss the portability issue -> the way that programs written in
Java canbe run on many differentplatforms.
The conventional way
• source code is translated by acompiler to native
code (the baselanguage of the computer) which is
then executed.
• Thenative code that hasbeen generated can be
executed only by the particular type ofcomputer
that recognizesit.
In the case of Java
• source code is translated by acompiler to bytecode
(intermediate form) which is translated by an
interpreter into the native code of the computer it
runs on which is then executed.
• Theinterpreter is aprogram that translates
bytecode into the native code of the computer it
runs on
• Javainterpreters are available for many different
platforms.
10
3. The System & GettingJavarunning
The JavaSoftware Development Kit (abbreviated asSDKorJDK)
• SDKtools are used for compiling and running Javaprograms on a
variety of platforms
• SDKtools are freely available from Oracle
• SDKtools include:
– TheJavacompiler (javac) translates Javasource into bytecode.
– TheJavainterpreter (java) translates and executes Javabytecode. Javainterpreters are
available for many different computersystems.
– Thedocument generator (javadoc) processes Javasource files to produceuseful
documentation, such asstandardized descriptions of components of Javacode.
– TheJavadebugger (jdb) helps to look for errors in programs.
– TheJavadisassembler (javap) reads bytecode files created using the Javacompiler
and displays the corresponding sourcecode.
– Theapplet viewer (appletviewer) allows you to run and debug Javaapplets without a
web browser (Javaapplets are explained in the nextslide)
11
3. The System & GettingJavarunning
1. Javaprogrammers canproduce two types of software:
1. Applications are stand-alone programs, which can runindependently.
2. Applets are programs thatcan be included in web documents and are
run using abrowser.
2. Twowaysto develop and run Javasoftware:
– T
o useJavaSDK along with atext editor (mostly run fromcommand
line)
– T
ouse an Integrated Development Environment(IDE),
• IDEprovides asophisticated GUIand editor, with integrated facilities for
compiling, running and debuggingprograms.
• There are many IDEsavailable for Java,such as NetBeans and Eclipse.
12
• Introduction
• Java background
• The system & Getting Java running
• Data types
• Statements and Scope and Operators
• Conditional processing
13
4. Datatypes
Strong typing
Javais astrongly typed language
– This means that every variable and expression has a type when the
program is compiled and that you cannot randomly assign values of
one type to another.
• There are two categories of data type in Java:
1. Primitive variables store datavalues.
2. Reference variables do not themselves store data values, butare
used to refer toobjects.
• Data type sizesin Javaare fixed
– to ensure portability across implementations of the language.
14
4. Datatypes
Primitive DataTypes(Cont’d)
• Declaration and initialization:
we can reserve memory locations for storing values ofany primitive
type. Thosememory locations can then be made to contain values
that are legal for the particular type. This involves two steps.
1)Declaration: Create a variable of the type you want to use.
2)Initialization: This refers to when you first store something useful
in avariable's memorylocation.
Example:Declare two variables of type int and initialize them
int myInt;
int myOtherInt;
myInt =888;
myOtherInt =-2;
15
4. Datatypes
Primitive DataTypes(Cont’d) –
• The integral types are called byte, short, int and long. All of these types are said
to be signed, meaning that they can hold positive or negative numbers. Eachhas
adifferent capacity with fixedsize.
16
4. Datatypes
Primitive data types(Cont’d)
• Thechar type: Javahasacharacter data type called char, which allowsto
represent single characters. Character literals are denoted by avalue
enclosed within singlequotes.
• Acharacter variable can be assigned any value in the Unicodecharacter
set.
• Escapesequence: It is asequence of characters standing in place of a
single value of the character type. In Java,they are marked by the use of
the backslash character, which indicates that what follows isnot to be
interpreted literally.
• EX.of aprintable character: 'u00A9' = ©
• EX.of non-printable character: 'n' =New Line
• Theliteral for the single quote ' character is ' ''
• The literal for the backslash  character is ' ' 17
4. Datatypes
Primitive data types(Cont’d)
• Floating-point types: In order to represent numbers that may
include fractional parts, we use floating-point types. Thefloattype
has32 bits of spaceand double has64bits.
• Floating-point literals are denoted eitherby
– writing them asintegerpart.fractionalpart, asdouble x=23.8;
– using scientific notation, where the character e or E denotes
the fact that the number is to be multiplied by the power of
ten of the integer following the e orE,as
double y = 1.43E8, z = 1e-9;
18
4. Datatypes
19
4. Datatypes
Casting
• There are occasions where we want to be able to convert from one type to
another. This canbe done either:
– Automatically: Javadoes it for usand knownaspromotion
– Bycasting: we have to specify atypeconversion.
• Bywriting the desired type of an expression in parentheses in front of the
expression; for example, write (int) in front of an expression if we wanted it to
be converted to anint.
– Note that the fractional parts of afloat data type are lost if casting to an
integral type. Becausevariables ofdifferent types occupy different amounts
of memory, this cancauseinformationloss
Ex.Casting a double to an int. Example: Casting an int toa char
c will have the value 60, which is the
61st character in the Unicode set. This
turns out to be the character '<'.
intVal will have the value of 2
20
4. Datatypes
Casting(Cont’d)
• When is acast required?
– Acast is required if the type you are assigning (on the right-hand side of an
equals sign) occupies a larger spacein memory than the type you are
assigningto (on the left-hand side of an equals sign), or if you are trying to
assign (float or double) values to integral type.
– If you have not specified acast in acasewhere acast is required, the
compiler will produce a compilation error with the message that there is a
'possible loss of precision'.
21
5. Statements and ScopeandOperators
Statements and Scope
• Astatement is aunit of executable code,usually
terminated by asemicolon.
• Codeblock includes agroup of statementsin
enclosed in curly brackets.
• Local variables: They are variables that are declared inside a code
block, and they are valid only inside the brackets. We say that the
variables' scope is the region enclosed by thebrackets
22
5. Statements and ScopeandOperators
Operators
• All the primitive data types that are provided in Javaare associated with
sets of operators.
– EX.integers and floating-point types have addition and subtractionoperators.
• Operators canhave
– single arguments, in which casethey are known asunaryoperators;
– two arguments, in which casethey are known asbinaryoperators.
• Arithmetic operators: are used withfloating-point numbers and the various integral
types.All the standard arithmetic operations areavailable,
23
5. Statements and ScopeandOperators
Operators
• The negation operator (-) unary operator
• The single argument for the negation operator appears to the right of the minus sign. It is
aprefix operator, which simply meansthat it appears before the value it takes asan
argument.
• Increment (++) and decrement (--)operators
• Thehave two forms: postfix andprefix
• The postfix returns the old value of the variable to which it is applied where as
the prefix form returns the new value.
• The ++operator increments the variable it is associated with by 1
• The -- operator decrements the variable it is associated with by 1
myInt would have the value 11
and x would have the value 10.
myInt would have the value 1 then 2
24
5. Statements and ScopeandOperators
Operators
• Precedence of the arithmetic operators
• If an expression involves more than one operator, there is a clear order in which
operators are evaluated, determined by what is known asoperatorprecedence
3+4*5  evaluates to 23
• Weare deliberately not stating the precedence rules. Instead, you should ensure
aclear order of evaluation of an expression, using parentheses. Javawill
evaluate expressions in parenthesesfirst.
• 3+(4*5)  evaluates to23
• (3+4) * 5  evaluates to 35
25
5. Statements and ScopeandOperators
26
5. Statements and ScopeandOperators
• Relational operators:
– Therelational operators tell you about relationships between values of thesametype.
– Their arguments can be integers, floating-point numbers, or characters. The'equal to'
and 'not equal to' operators can also be used with theboolean type
• An expression involving arelational operator is alogical expression, sohasa
boolean value either true of false . Evaluate the following expressions
1 <2
true !=false
0.5 >0.0
false ==false
2 ==2
'a' <'c‘
27
5. Statements and Scope and Operators
28
5. Statements and ScopeandOperators
• Logical operators:
• Are used when we want to combine the results from several logical expressions
• Thearguments for alogical operator must be logicalexpressions.
Example:
• Thevariable eitherPositive is assignedthe value true, because a>0 is true and
therefore the whole expression is true (even though b >0 is false).
• Thevariable bothNegative is assignedthe value false because a<0 is false, and
• therefore the whole expression is false. Eventhough b<0 is true.
29
6. Conditional processing
The boolean type
• Theflow control structures determine the way in which aJavaprogram is
executed, allowing different segments of code to be executed under different
circumstances.
• Conditional processing and Repetitive processing
• In Conditional processing (The Selection statements) give uswaysofspecifying
the conditions for changing the flow of control in aprogram.
• Variables of the boolean type can hold either the literal value true or false,
which are keywords in the Javalanguage.
• Theexpression that evaluates to aboolean value named alogicalexpression.
• Javadefines anumber of relational operators for booleantypes:
–Relational operators and Logical operators
Theif statement
•Thegeneral form of the firstconditional
processing statement is shownhere 30
6. Conditional processing
Theif statement
• If the logical_expression within the parentheses
evaluates to true, then any statements in the following code block are executed.
• If the logical_expression is false, then any statements in the body of the if
statement are not executed.
• An if statement may cover several lines of code, (but it is just one statement ).
Asis usual when astatement hasabody in curly brackets, there is nosemicolon
at its end.
• It is better style to use curly brackets { }, to demarcate clearly the body of the
if statement .
• Thestyle of writing Javaprograms that includes indentation to indicate the
structure of a program. Indentation aids readability of code, but does not affect
the way the compiler interpretsit.
Variables of type boolean and ifstatements
• Aboolean variable is used when we want to store information about the truth
or falsity of some part of our program
31
6. Conditional processing
The if ... else statement
•There is a more complicated form of
the statement that allows us to specify
code to be processed when thelogical
condition is false in addition to code to be processed when it is true.
– statementsA are executed if the logical_expression is true and
statementsB are executed if the expression isfalse.
•Example:
32
6. Conditional processing
Nesting if statements
• Wecan write if statements within if statements, which we call nesting.An
example of this is shownbelow:
• Example:
Seethe advantage of using anindentation
style: the structure of the two if
statements is displayed and it becomes
clear which statements are going to be
executed when either of the twological expressions is true.
33
6. Conditional processing
The switch ... case statement
Theargument is an expression of type int,char, short
or byte (usually just the name of a variable).
Eachselector is aconstant value (usually a literal)
compatible with the argumenttype.
Acode block enclosing the statements in eachcaseis
optional.
Thestatements are performed if the caseselector is
logically equal to the argument ; in other words, if
argument ==selector.
The break keyword causes the switch to terminate.
Failure to use a break statement results in control
'falling through' to the nextcase.
Adefault casemay be given to indicate processingto
take place when no selector ismatched.
34
6. Conditional processing
The switch ... case statement
Example:
35
6. Conditional processing
The switch ... case statement Example:
for (inti=0; i<=10; i++){
switch ( i ){
case1: System.out.print("one ");
case2: System.out.print("two ");
case3: System.out.print("three "); break;
case4:
case5: System.out.print("four &five"); break;
case6: System.out.print("six"); break;
case7: System.out.print("seven ");
case8: System.out.print("eight ");break;
default: System.out.print("Not in the list");
}// switch
S
ystem.out.println();
}// for
Not in thelist
one two three
two three
three
four & five
four & five
six
seveneight
eight
Not in thelist
Not in thelist
36
7. Repetitive processing
• Javaallow sections of code tobe executed repeatedly while
some condition is satisfied € iteration!
7.1 - The while statement
Meaning: While the logical_expression is true, the statements are executed. Thetruth of
the logical_expression is rechecked after each execution of the body of the while.
Example:
37
7. Repetitive processing
38
7. Repetitive processing
7.2- The for statement
Meaning: Thecontrol variable is created and given its initial value in control_initializer. Next, the
logical_expression determines if the loop body is executed.After each time the statements are
executed, control_adjustment defines the new value of the control variable, and the
logical_expression is checked again. Theloop terminates when the logical_expression becomesfalse.
Note: The control_initializer is only executed once and is the first statement to be executed by the for.
Examples:
This sets all elements
of a[] to 10
Can you figure out how
this loop is executed??
39

More Related Content

Similar to M251_Meeting 1(M251_Meeting 1_updated.pdf)

Java Programming concept
Java Programming concept Java Programming concept
Java Programming concept
Prakash Poudel
 
Letest
LetestLetest
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
JitendraYadav351971
 
The Java Story
The Java StoryThe Java Story
The Java Story
David Parsons
 
java slides
java slidesjava slides
java slides
RizwanTariq18
 
Lecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 FastLecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 Fast
UzairSaeed18
 
1 java introduction
1 java introduction1 java introduction
1 java introduction
abdullah al mahamud rosi
 
1 java intro
1 java intro1 java intro
JAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptxJAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptx
SuganthiDPSGRKCW
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
MeghaKulkarni27
 
JAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptxJAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptx
Murugesh33
 
JAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptxJAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptx
Murugesh33
 
Introduction To Java history, application, features.pptx
Introduction To Java history, application, features.pptxIntroduction To Java history, application, features.pptx
Introduction To Java history, application, features.pptx
sonalipatil225940
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
arnold 7490
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
 
Java Ch 1.pptx
Java Ch 1.pptxJava Ch 1.pptx
Java Ch 1.pptx
VanitaPatil14
 
MWLUG - Universal Java
MWLUG  -  Universal JavaMWLUG  -  Universal Java
MWLUG - Universal Java
Philippe Riand
 
unit1.pptx
unit1.pptxunit1.pptx
unit1.pptx
PrasadKalal4
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
Nanthini Kempaiyan
 
CS8392 OOP
CS8392 OOPCS8392 OOP

Similar to M251_Meeting 1(M251_Meeting 1_updated.pdf) (20)

Java Programming concept
Java Programming concept Java Programming concept
Java Programming concept
 
Letest
LetestLetest
Letest
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
 
The Java Story
The Java StoryThe Java Story
The Java Story
 
java slides
java slidesjava slides
java slides
 
Lecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 FastLecture-01 _Java Introduction CS 441 Fast
Lecture-01 _Java Introduction CS 441 Fast
 
1 java introduction
1 java introduction1 java introduction
1 java introduction
 
1 java intro
1 java intro1 java intro
1 java intro
 
JAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptxJAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptx
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
JAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptxJAVAPart1_BasicIntroduction.pptx
JAVAPart1_BasicIntroduction.pptx
 
JAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptxJAVA_Day1_BasicIntroduction.pptx
JAVA_Day1_BasicIntroduction.pptx
 
Introduction To Java history, application, features.pptx
Introduction To Java history, application, features.pptxIntroduction To Java history, application, features.pptx
Introduction To Java history, application, features.pptx
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Java Ch 1.pptx
Java Ch 1.pptxJava Ch 1.pptx
Java Ch 1.pptx
 
MWLUG - Universal Java
MWLUG  -  Universal JavaMWLUG  -  Universal Java
MWLUG - Universal Java
 
unit1.pptx
unit1.pptxunit1.pptx
unit1.pptx
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 

Recently uploaded

A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

M251_Meeting 1(M251_Meeting 1_updated.pdf)

  • 1. Meeting 1: Java Revision Introduction to programming usingJava Part 1 1
  • 2. • Introduction • Java background • The system & Getting Java running • Data types • Statements and Scope and Operators • Conditional processing • Repetitive processing 2
  • 3. 1. Introduction • During the early daysof the internet, SunMicrosystems introduced the Javalanguage, with great fanfare, in 1995. Javaimmediately generated huge interest and excitement – useof the language took off more rapidly than any computer language before or since. • Javais the language most identified with the internet and its friendly face, theweb. • Currently, Java is owned by Oracle. • In this course, you will learn how Javaoriginated and how it is usednowadays. • you will learn the language used to build systemsthat power somuch of the web inapplications ranging from e-commerce to online games, downloadable music to online banking. 3
  • 4. 1. Introduction • Javaruns on servers, PCs,mobile phones and PDAs(personal digital assistants). • It canbe foundin – TVset-topboxes, – in embedded devices (forming part of larger systems suchascars, robots or printers), – in smart cards,and – wearable computers. Javareally is everywhere. 4
  • 5. 2. Javabackground The aims of the Java language • Simple to learn, & Familiar and closely based on C++,but reduced in complexity. • Object-oriented: java programs are structured aroundobjects • Robust: Javaprograms are strictly checkedby software before theyrun • Secure: Javaensures that programs running over networks cannot damageyour computer files or introduceviruses. • Portable: Javaprograms caneasily be transferred from one platform (e.g. Windows) to run on another platform (e.g. Linux) with little or no change. • High performance : Javaprograms canrun fast enough for whatis needed • Interpreted: akey aspect of Javaportability. • Threaded: allow aprogram todo several things at once. • Dynamic: Javaprograms canadapt to changes in their environmenteven while the program isrunning. 5
  • 6. 2. Javabackground Versions &Editions of Java Versions of Java • Thefirst publicly available version wasJava1.0. • Now, Java2 is the standard language – and there have been anumber of further versions (Java2 version 1.3,Java2 version 1.4, Java2 version 1.5 and soon), each introducing relatively minor improvements Editions of Java • Why Javaeditions was introduced? – tocater for the different needs of, say,large international business systems to software running on mobile phones with very limited hardware resources. • Current editions: • Java2 Standard Edition (J2SE) - focus of our study! • Java2 Enterprise Edition (J2EE) - for large-scale systems • Java2 Micro Edition (J2ME) - for mobile phones 6
  • 8. 2. Javabackground • Thefirst line of the program introduces aclass, calledHelloWalrus. • When the program runs, the classgives rise to an object, which executes the code of the class. • This classis defined aspublic, which means that objects ofthis classcan be freely accessed by other objects in alargersystem. • Thecurly bracket ‘{‘ on the line following HelloWalrus matches the closing bracket ‘}’ at the end of the example, asthese enclose the whole classdefinition. • The code in this classis in the form of a method, in this casea method called main. • Thefirst line of the method is called the method header. • Wedefer explaining the words static and void until later, but they are always used with main methods like thisone. • Themethod header defines the name of the method (in this case,main) and is followed by the method body, which is enclosed in curly brackets. • TheSystem.out.println method displays, in ascreen window, the exact text betweenthe quotes. • Eachof these two lines of code constitutes a statement, asingle command to be carried out. • Eachstatement is terminated by asemicolon ‘;’. 8
  • 9. 2. Javabackground Using comments in Javaprograms • Acomment is ignored by the computersystem – Form 1: Block Comment. Textenclosed in the symbols /* and */ – Form 2: Line Comment or In-line Comment (using//) • Line comment: • In-line comment: 9
  • 10. 3. The System & GettingJavarunning Here we discuss the portability issue -> the way that programs written in Java canbe run on many differentplatforms. The conventional way • source code is translated by acompiler to native code (the baselanguage of the computer) which is then executed. • Thenative code that hasbeen generated can be executed only by the particular type ofcomputer that recognizesit. In the case of Java • source code is translated by acompiler to bytecode (intermediate form) which is translated by an interpreter into the native code of the computer it runs on which is then executed. • Theinterpreter is aprogram that translates bytecode into the native code of the computer it runs on • Javainterpreters are available for many different platforms. 10
  • 11. 3. The System & GettingJavarunning The JavaSoftware Development Kit (abbreviated asSDKorJDK) • SDKtools are used for compiling and running Javaprograms on a variety of platforms • SDKtools are freely available from Oracle • SDKtools include: – TheJavacompiler (javac) translates Javasource into bytecode. – TheJavainterpreter (java) translates and executes Javabytecode. Javainterpreters are available for many different computersystems. – Thedocument generator (javadoc) processes Javasource files to produceuseful documentation, such asstandardized descriptions of components of Javacode. – TheJavadebugger (jdb) helps to look for errors in programs. – TheJavadisassembler (javap) reads bytecode files created using the Javacompiler and displays the corresponding sourcecode. – Theapplet viewer (appletviewer) allows you to run and debug Javaapplets without a web browser (Javaapplets are explained in the nextslide) 11
  • 12. 3. The System & GettingJavarunning 1. Javaprogrammers canproduce two types of software: 1. Applications are stand-alone programs, which can runindependently. 2. Applets are programs thatcan be included in web documents and are run using abrowser. 2. Twowaysto develop and run Javasoftware: – T o useJavaSDK along with atext editor (mostly run fromcommand line) – T ouse an Integrated Development Environment(IDE), • IDEprovides asophisticated GUIand editor, with integrated facilities for compiling, running and debuggingprograms. • There are many IDEsavailable for Java,such as NetBeans and Eclipse. 12
  • 13. • Introduction • Java background • The system & Getting Java running • Data types • Statements and Scope and Operators • Conditional processing 13
  • 14. 4. Datatypes Strong typing Javais astrongly typed language – This means that every variable and expression has a type when the program is compiled and that you cannot randomly assign values of one type to another. • There are two categories of data type in Java: 1. Primitive variables store datavalues. 2. Reference variables do not themselves store data values, butare used to refer toobjects. • Data type sizesin Javaare fixed – to ensure portability across implementations of the language. 14
  • 15. 4. Datatypes Primitive DataTypes(Cont’d) • Declaration and initialization: we can reserve memory locations for storing values ofany primitive type. Thosememory locations can then be made to contain values that are legal for the particular type. This involves two steps. 1)Declaration: Create a variable of the type you want to use. 2)Initialization: This refers to when you first store something useful in avariable's memorylocation. Example:Declare two variables of type int and initialize them int myInt; int myOtherInt; myInt =888; myOtherInt =-2; 15
  • 16. 4. Datatypes Primitive DataTypes(Cont’d) – • The integral types are called byte, short, int and long. All of these types are said to be signed, meaning that they can hold positive or negative numbers. Eachhas adifferent capacity with fixedsize. 16
  • 17. 4. Datatypes Primitive data types(Cont’d) • Thechar type: Javahasacharacter data type called char, which allowsto represent single characters. Character literals are denoted by avalue enclosed within singlequotes. • Acharacter variable can be assigned any value in the Unicodecharacter set. • Escapesequence: It is asequence of characters standing in place of a single value of the character type. In Java,they are marked by the use of the backslash character, which indicates that what follows isnot to be interpreted literally. • EX.of aprintable character: 'u00A9' = © • EX.of non-printable character: 'n' =New Line • Theliteral for the single quote ' character is ' '' • The literal for the backslash character is ' ' 17
  • 18. 4. Datatypes Primitive data types(Cont’d) • Floating-point types: In order to represent numbers that may include fractional parts, we use floating-point types. Thefloattype has32 bits of spaceand double has64bits. • Floating-point literals are denoted eitherby – writing them asintegerpart.fractionalpart, asdouble x=23.8; – using scientific notation, where the character e or E denotes the fact that the number is to be multiplied by the power of ten of the integer following the e orE,as double y = 1.43E8, z = 1e-9; 18
  • 20. 4. Datatypes Casting • There are occasions where we want to be able to convert from one type to another. This canbe done either: – Automatically: Javadoes it for usand knownaspromotion – Bycasting: we have to specify atypeconversion. • Bywriting the desired type of an expression in parentheses in front of the expression; for example, write (int) in front of an expression if we wanted it to be converted to anint. – Note that the fractional parts of afloat data type are lost if casting to an integral type. Becausevariables ofdifferent types occupy different amounts of memory, this cancauseinformationloss Ex.Casting a double to an int. Example: Casting an int toa char c will have the value 60, which is the 61st character in the Unicode set. This turns out to be the character '<'. intVal will have the value of 2 20
  • 21. 4. Datatypes Casting(Cont’d) • When is acast required? – Acast is required if the type you are assigning (on the right-hand side of an equals sign) occupies a larger spacein memory than the type you are assigningto (on the left-hand side of an equals sign), or if you are trying to assign (float or double) values to integral type. – If you have not specified acast in acasewhere acast is required, the compiler will produce a compilation error with the message that there is a 'possible loss of precision'. 21
  • 22. 5. Statements and ScopeandOperators Statements and Scope • Astatement is aunit of executable code,usually terminated by asemicolon. • Codeblock includes agroup of statementsin enclosed in curly brackets. • Local variables: They are variables that are declared inside a code block, and they are valid only inside the brackets. We say that the variables' scope is the region enclosed by thebrackets 22
  • 23. 5. Statements and ScopeandOperators Operators • All the primitive data types that are provided in Javaare associated with sets of operators. – EX.integers and floating-point types have addition and subtractionoperators. • Operators canhave – single arguments, in which casethey are known asunaryoperators; – two arguments, in which casethey are known asbinaryoperators. • Arithmetic operators: are used withfloating-point numbers and the various integral types.All the standard arithmetic operations areavailable, 23
  • 24. 5. Statements and ScopeandOperators Operators • The negation operator (-) unary operator • The single argument for the negation operator appears to the right of the minus sign. It is aprefix operator, which simply meansthat it appears before the value it takes asan argument. • Increment (++) and decrement (--)operators • Thehave two forms: postfix andprefix • The postfix returns the old value of the variable to which it is applied where as the prefix form returns the new value. • The ++operator increments the variable it is associated with by 1 • The -- operator decrements the variable it is associated with by 1 myInt would have the value 11 and x would have the value 10. myInt would have the value 1 then 2 24
  • 25. 5. Statements and ScopeandOperators Operators • Precedence of the arithmetic operators • If an expression involves more than one operator, there is a clear order in which operators are evaluated, determined by what is known asoperatorprecedence 3+4*5  evaluates to 23 • Weare deliberately not stating the precedence rules. Instead, you should ensure aclear order of evaluation of an expression, using parentheses. Javawill evaluate expressions in parenthesesfirst. • 3+(4*5)  evaluates to23 • (3+4) * 5  evaluates to 35 25
  • 26. 5. Statements and ScopeandOperators 26
  • 27. 5. Statements and ScopeandOperators • Relational operators: – Therelational operators tell you about relationships between values of thesametype. – Their arguments can be integers, floating-point numbers, or characters. The'equal to' and 'not equal to' operators can also be used with theboolean type • An expression involving arelational operator is alogical expression, sohasa boolean value either true of false . Evaluate the following expressions 1 <2 true !=false 0.5 >0.0 false ==false 2 ==2 'a' <'c‘ 27
  • 28. 5. Statements and Scope and Operators 28
  • 29. 5. Statements and ScopeandOperators • Logical operators: • Are used when we want to combine the results from several logical expressions • Thearguments for alogical operator must be logicalexpressions. Example: • Thevariable eitherPositive is assignedthe value true, because a>0 is true and therefore the whole expression is true (even though b >0 is false). • Thevariable bothNegative is assignedthe value false because a<0 is false, and • therefore the whole expression is false. Eventhough b<0 is true. 29
  • 30. 6. Conditional processing The boolean type • Theflow control structures determine the way in which aJavaprogram is executed, allowing different segments of code to be executed under different circumstances. • Conditional processing and Repetitive processing • In Conditional processing (The Selection statements) give uswaysofspecifying the conditions for changing the flow of control in aprogram. • Variables of the boolean type can hold either the literal value true or false, which are keywords in the Javalanguage. • Theexpression that evaluates to aboolean value named alogicalexpression. • Javadefines anumber of relational operators for booleantypes: –Relational operators and Logical operators Theif statement •Thegeneral form of the firstconditional processing statement is shownhere 30
  • 31. 6. Conditional processing Theif statement • If the logical_expression within the parentheses evaluates to true, then any statements in the following code block are executed. • If the logical_expression is false, then any statements in the body of the if statement are not executed. • An if statement may cover several lines of code, (but it is just one statement ). Asis usual when astatement hasabody in curly brackets, there is nosemicolon at its end. • It is better style to use curly brackets { }, to demarcate clearly the body of the if statement . • Thestyle of writing Javaprograms that includes indentation to indicate the structure of a program. Indentation aids readability of code, but does not affect the way the compiler interpretsit. Variables of type boolean and ifstatements • Aboolean variable is used when we want to store information about the truth or falsity of some part of our program 31
  • 32. 6. Conditional processing The if ... else statement •There is a more complicated form of the statement that allows us to specify code to be processed when thelogical condition is false in addition to code to be processed when it is true. – statementsA are executed if the logical_expression is true and statementsB are executed if the expression isfalse. •Example: 32
  • 33. 6. Conditional processing Nesting if statements • Wecan write if statements within if statements, which we call nesting.An example of this is shownbelow: • Example: Seethe advantage of using anindentation style: the structure of the two if statements is displayed and it becomes clear which statements are going to be executed when either of the twological expressions is true. 33
  • 34. 6. Conditional processing The switch ... case statement Theargument is an expression of type int,char, short or byte (usually just the name of a variable). Eachselector is aconstant value (usually a literal) compatible with the argumenttype. Acode block enclosing the statements in eachcaseis optional. Thestatements are performed if the caseselector is logically equal to the argument ; in other words, if argument ==selector. The break keyword causes the switch to terminate. Failure to use a break statement results in control 'falling through' to the nextcase. Adefault casemay be given to indicate processingto take place when no selector ismatched. 34
  • 35. 6. Conditional processing The switch ... case statement Example: 35
  • 36. 6. Conditional processing The switch ... case statement Example: for (inti=0; i<=10; i++){ switch ( i ){ case1: System.out.print("one "); case2: System.out.print("two "); case3: System.out.print("three "); break; case4: case5: System.out.print("four &five"); break; case6: System.out.print("six"); break; case7: System.out.print("seven "); case8: System.out.print("eight ");break; default: System.out.print("Not in the list"); }// switch S ystem.out.println(); }// for Not in thelist one two three two three three four & five four & five six seveneight eight Not in thelist Not in thelist 36
  • 37. 7. Repetitive processing • Javaallow sections of code tobe executed repeatedly while some condition is satisfied € iteration! 7.1 - The while statement Meaning: While the logical_expression is true, the statements are executed. Thetruth of the logical_expression is rechecked after each execution of the body of the while. Example: 37
  • 39. 7. Repetitive processing 7.2- The for statement Meaning: Thecontrol variable is created and given its initial value in control_initializer. Next, the logical_expression determines if the loop body is executed.After each time the statements are executed, control_adjustment defines the new value of the control variable, and the logical_expression is checked again. Theloop terminates when the logical_expression becomesfalse. Note: The control_initializer is only executed once and is the first statement to be executed by the for. Examples: This sets all elements of a[] to 10 Can you figure out how this loop is executed?? 39