SlideShare a Scribd company logo
Programming in Java
5-day workshop
Variables and
assignment
Matt Collison
JP Morgan Chase 2021
PiJ1.2: Java Basics
Session overview
Primitive types in Java
• General purpose
• High-level compiled
• Strongly typed object-oriented
Who uses Java?
• popularity TIOBE index
• Java history
Getting started with Java?
• Java download and versions
• The javac Java compiler
• A first Java program - Hello world
Lecture overview
• Variables
• Statements and assignment
• Built-in types
• Identifiers, namespaces, Naming conventions
• What not to do
• What to do
•Command-line outputs and inputs
Statements and variables
• Each complete instruction is known as a statement
• There are multi-line statements and statements over multiple lines
later
• Statements conclude with a semi-colon
int a = 5;
System.out.println(a);
Assignment statements
• Each complete instruction is known as a statement
• In Java variables are must be created before you assign a value
• In an assignment statement the expression on the right hand side of the
equals sign is evaluated and assigned to the variable on the left hand side:
int a = 5;
int newVariable = a – 1
<var> = <expression>
Assignment, not equality
• Note: The equals sign indicates assignment, not equality. Some languages use a
different symbol, such as
• Or
• But even with a plain ‘=‘ there should be no suggestion that the statement can be
solved. This rapidly leads to nonsense:
• .
int bottles <- 10 // Not Java
int bottles <- bottles – 1 // Not Java
int bottles := 10 // Not Python
int bottles := bottles – 1 // Not Python
bottles = bottles -1 // Not an equation
0 = -1 // No
Assignment statements
• The right hand side of the assignment statement can be either:
• Literal – a quantity that needs no evaluation
• Expression – the computed product of an operation
int a = 5
int z = 792
String greeting = “Hello”
int a = 5 + 6
int z = a * 12
String question = greeting + “Who are you?”
Note the speech marks
Note the
type
Variables in a program
public class Variables {
public static void main(String[] args) {
int num = 2 + 30;
int bottles = 10;
int a = 5;
System.out.println( a );
num = bottles + a;
System.out.println( num );
int variableWithALongName = 789;
int another_long_name = bottles * 2;
System.out.println( " The value of another_long_name is "
+ another_long_name );
}
}
Variables.java
Variables in a program
$ javac Variables.java
$java Variables
5
15
The value of another_long_name is 20
• ‘5’ comes from System.out.println(a)
• ‘15’ comes from System.out.println(b)
• ‘The value of another_long_name is 20’ comes from the final print statement
• The only terminal output is from the print call, but all the statements are
executed
Run the program Variables.java from the command line
Primitive types
Primitive types
Category Types Size (bits) Precision Example
Integer
byte 8 From +127 to -128 byte b = 65;
char 16 All Unicode characters[1]
char c = 'A’;
char c = 65;
short 16 From +32,767 to -32,768 short s = 65;
int 32 From +2,147,483,647 to -2,147,483,648 int i = 65;
long 64 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 long l = 65L;
Floating-
point
float 32 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f;
double 64 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324 double d = 65.55;
Other
boolean -- false, true boolean b = true;
void -- -- --
Integer literals
• Integer literals (byte, short, int, long)
• Allowed:
• decimal: 197 -56 1234_5678 (Java 8 onwards)
• octal: 036
• hexadecimal: 0x1a 0X3E1
• binary: 0b11010 (Java 7 onwards)
• Not allowed:
• 137,879
• Be careful: 0457 (allowed, but its literal decimal value is 111)
• The suffix L or l is used to indicate it is of type long (e.g., 234L, OX3E3l). The
uppercase L is preferred.
char literals
char literals
• char type is a numeric type, which means the arithmetic operations
are allowed.
• Allowed: ’B’, ’.’, ’u5469’,
• Special characters: ’n’, ’t’, ’’, ’”, ’”’
Floating point literals
• Floating-point literals (float, double)
• Allowed: 6.67 0.1e+3f 7e5 4.5E-19 1E-9f 1e4D
• Allowed but not recommended: 3. .76
float height = 0.158; //error: incompatible types
float height = 0.158f;
float height = 1.58E-1f;
• int a=012;
• int b=23,120;
• int c=23 120;
• byte d=130;
• long e=23120l;
• double f=1e-2;
• float g = 1.02;
• float g = 1.02f;
• char h = "B";
• char h = ’B’;
• char i = ’u0034’;
• boolean j = TRUE;
• boolean j = "true";
• boolean j = true;
//correct, but ’a’ equals to 10
//error
//correct
//error: incompatible types
//correct, ’L’ is recommended
//correct
//error: incompatible types
//correct
//error: incompatible types
//correct
//correct
//error
//error
//correct
Constants
A constant refers to a data item which cannot be changed.
• The keyword final must be used in the declaration of a constant.
• E.g.,
final String MY_MOTTO = "I was struggling to begin, but in the end I
overcome and nail it!";
Naming conventions:
• Variables: mixed case, lower case first letter
• E.g., age, buttonColor, dateDue
• Constants: all upper case, words separated by underscore
• E.g., PI, MIN VALUE, MAX VALUE
Challenge
• Exercise: Write a program (CircleComputation.java) which
prints on the command-line the area and circumference of a circle,
given its radius (1.5).
Hint: final double PI = 3.14159265;
Casting primitive types
• It is possible to convert one numeral type to another numeral type, refers
to casting (or: primitive conversion).
• Widening casting (Implicit done)
• short a=20;
• long b=a;
• int c=20;
• float d=c;
• Narrowing casting (Explicitly done)
• long a = 20;
• short b = (short) a;
• float c = 20;
• int d = (int) c;
• float hight = (float) 0.158;
Overflow on casting (be careful)
• Overflow: Operations with integer types may produce numbers which
are too big to be stored in the primitive types you have allocated.
byte d=130; //error: incompatible types
byte d= (byte)130; //no error, but d’s value is -126, because of overflow
• NOTE: No error message for overflow will be raised, you’ll simply get
an unexpected number.
Learning resources
The workshop homepage
https://mcollison.github.io/JPMC-java-intro-2021/
The course materials
https://mcollison.github.io/java-programming-foundations/
• Session worksheets – updated each week
Additional resources
• Think Java: How to think like a computer scientist
• Allen B Downey (O’Reilly Press)
• Available under Creative Commons license
• https://greenteapress.com/wp/think-java-2e/
• Oracle central Java Documentation –
https://docs.oracle.com/javase/8/docs/api/
• Other sources:
• W3Schools Java - https://www.w3schools.com/java/
• stack overflow - https://stackoverflow.com/
• Coding bat - https://codingbat.com/java

More Related Content

What's hot (17)

ODP
Preparing Java 7 Certifications
Giacomo Veneri
 
PPT
C++ for beginners
Salahaddin University-Erbil
 
PPSX
C++ Programming Language
Mohamed Loey
 
PPTX
C programming tutorial for Beginner
sophoeutsen2
 
PPT
C++ Programming Course
Dennis Chang
 
PPTX
LLVM Backend Porting
Shiva Chen
 
PPT
Gcc porting
Shiva Chen
 
PPT
C tutorial
Anurag Sukhija
 
PDF
Cheat Sheet java
arkslideshareacc
 
PDF
Fundamentals of Computing and C Programming - Part 1
Karthik Srini B R
 
PDF
Fundamentals of Computing and C Programming - Part 2
Karthik Srini B R
 
PPTX
Variables in C and C++ Language
Way2itech
 
PPTX
LLVM Instruction Selection
Shiva Chen
 
PPT
Token and operators
Samsil Arefin
 
PDF
Semantic Analysis of a C Program
Debarati Das
 
PPT
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
Preparing Java 7 Certifications
Giacomo Veneri
 
C++ for beginners
Salahaddin University-Erbil
 
C++ Programming Language
Mohamed Loey
 
C programming tutorial for Beginner
sophoeutsen2
 
C++ Programming Course
Dennis Chang
 
LLVM Backend Porting
Shiva Chen
 
Gcc porting
Shiva Chen
 
C tutorial
Anurag Sukhija
 
Cheat Sheet java
arkslideshareacc
 
Fundamentals of Computing and C Programming - Part 1
Karthik Srini B R
 
Fundamentals of Computing and C Programming - Part 2
Karthik Srini B R
 
Variables in C and C++ Language
Way2itech
 
LLVM Instruction Selection
Shiva Chen
 
Token and operators
Samsil Arefin
 
Semantic Analysis of a C Program
Debarati Das
 
Chapter 2 - Basic Elements of Java
Adan Hubahib
 

Similar to Pi j1.2 variable-assignment (20)

PPT
Introduction to-programming
BG Java EE Course
 
PPTX
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
PDF
7-Java Language Basics Part1
Amr Elghadban (AmrAngry)
 
PPTX
Java Basics 1.pptx
TouseeqHaider11
 
PPT
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 
PDF
Acquiring a solid foundation in java
Julio Cesar Rocha Vera
 
PPTX
Introduction to Java Programming
One97 Communications Limited
 
PPTX
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPT
a basic java programming and data type.ppt
GevitaChinnaiah
 
PPTX
OOP-java-variables.pptx
ssuserb1a18d
 
PPT
Programming with Java: the Basics
Jussi Pohjolainen
 
PDF
Shiksharth com java_topics
Rajesh Verma
 
PPTX
OOP Introduction Part 2 In Java Language.pptx
habibansar098
 
PPTX
Java fundamentals
Jayfee Ramos
 
PPTX
JavaVariablesTypes.pptx
charusharma165
 
PPT
Presentation on programming language on java.ppt
HimambashaShaik
 
PPT
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Introduction to-programming
BG Java EE Course
 
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
7-Java Language Basics Part1
Amr Elghadban (AmrAngry)
 
Java Basics 1.pptx
TouseeqHaider11
 
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
 
Acquiring a solid foundation in java
Julio Cesar Rocha Vera
 
Introduction to Java Programming
One97 Communications Limited
 
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
a basic java programming and data type.ppt
GevitaChinnaiah
 
OOP-java-variables.pptx
ssuserb1a18d
 
Programming with Java: the Basics
Jussi Pohjolainen
 
Shiksharth com java_topics
Rajesh Verma
 
OOP Introduction Part 2 In Java Language.pptx
habibansar098
 
Java fundamentals
Jayfee Ramos
 
JavaVariablesTypes.pptx
charusharma165
 
Presentation on programming language on java.ppt
HimambashaShaik
 
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Ad

More from mcollison (11)

PPTX
Pi j4.2 software-reliability
mcollison
 
PPTX
Pi j4.1 packages
mcollison
 
PPTX
Pi j3.1 inheritance
mcollison
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
Pi j3.4 data-structures
mcollison
 
PPTX
Pi j2.3 objects
mcollison
 
PPTX
Pi j2.2 classes
mcollison
 
PPTX
Pi j1.0 workshop-introduction
mcollison
 
PPTX
Pi j1.4 loops
mcollison
 
PPTX
Pi j1.3 operators
mcollison
 
PPTX
Pi j1.1 what-is-java
mcollison
 
Pi j4.2 software-reliability
mcollison
 
Pi j4.1 packages
mcollison
 
Pi j3.1 inheritance
mcollison
 
Pi j3.2 polymorphism
mcollison
 
Pi j3.4 data-structures
mcollison
 
Pi j2.3 objects
mcollison
 
Pi j2.2 classes
mcollison
 
Pi j1.0 workshop-introduction
mcollison
 
Pi j1.4 loops
mcollison
 
Pi j1.3 operators
mcollison
 
Pi j1.1 what-is-java
mcollison
 
Ad

Recently uploaded (20)

PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PPTX
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
Martyrs of Ireland - who kept the faith of St. Patrick.pptx
Martin M Flynn
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PPTX
A Case of Identity A Sociological Approach Fix.pptx
Ismail868386
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
Martyrs of Ireland - who kept the faith of St. Patrick.pptx
Martin M Flynn
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
A Case of Identity A Sociological Approach Fix.pptx
Ismail868386
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 

Pi j1.2 variable-assignment

  • 1. Programming in Java 5-day workshop Variables and assignment Matt Collison JP Morgan Chase 2021 PiJ1.2: Java Basics
  • 2. Session overview Primitive types in Java • General purpose • High-level compiled • Strongly typed object-oriented Who uses Java? • popularity TIOBE index • Java history Getting started with Java? • Java download and versions • The javac Java compiler • A first Java program - Hello world
  • 3. Lecture overview • Variables • Statements and assignment • Built-in types • Identifiers, namespaces, Naming conventions • What not to do • What to do •Command-line outputs and inputs
  • 4. Statements and variables • Each complete instruction is known as a statement • There are multi-line statements and statements over multiple lines later • Statements conclude with a semi-colon int a = 5; System.out.println(a);
  • 5. Assignment statements • Each complete instruction is known as a statement • In Java variables are must be created before you assign a value • In an assignment statement the expression on the right hand side of the equals sign is evaluated and assigned to the variable on the left hand side: int a = 5; int newVariable = a – 1 <var> = <expression>
  • 6. Assignment, not equality • Note: The equals sign indicates assignment, not equality. Some languages use a different symbol, such as • Or • But even with a plain ‘=‘ there should be no suggestion that the statement can be solved. This rapidly leads to nonsense: • . int bottles <- 10 // Not Java int bottles <- bottles – 1 // Not Java int bottles := 10 // Not Python int bottles := bottles – 1 // Not Python bottles = bottles -1 // Not an equation 0 = -1 // No
  • 7. Assignment statements • The right hand side of the assignment statement can be either: • Literal – a quantity that needs no evaluation • Expression – the computed product of an operation int a = 5 int z = 792 String greeting = “Hello” int a = 5 + 6 int z = a * 12 String question = greeting + “Who are you?” Note the speech marks Note the type
  • 8. Variables in a program public class Variables { public static void main(String[] args) { int num = 2 + 30; int bottles = 10; int a = 5; System.out.println( a ); num = bottles + a; System.out.println( num ); int variableWithALongName = 789; int another_long_name = bottles * 2; System.out.println( " The value of another_long_name is " + another_long_name ); } } Variables.java
  • 9. Variables in a program $ javac Variables.java $java Variables 5 15 The value of another_long_name is 20 • ‘5’ comes from System.out.println(a) • ‘15’ comes from System.out.println(b) • ‘The value of another_long_name is 20’ comes from the final print statement • The only terminal output is from the print call, but all the statements are executed Run the program Variables.java from the command line
  • 11. Primitive types Category Types Size (bits) Precision Example Integer byte 8 From +127 to -128 byte b = 65; char 16 All Unicode characters[1] char c = 'A’; char c = 65; short 16 From +32,767 to -32,768 short s = 65; int 32 From +2,147,483,647 to -2,147,483,648 int i = 65; long 64 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 long l = 65L; Floating- point float 32 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f; double 64 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324 double d = 65.55; Other boolean -- false, true boolean b = true; void -- -- --
  • 12. Integer literals • Integer literals (byte, short, int, long) • Allowed: • decimal: 197 -56 1234_5678 (Java 8 onwards) • octal: 036 • hexadecimal: 0x1a 0X3E1 • binary: 0b11010 (Java 7 onwards) • Not allowed: • 137,879 • Be careful: 0457 (allowed, but its literal decimal value is 111) • The suffix L or l is used to indicate it is of type long (e.g., 234L, OX3E3l). The uppercase L is preferred.
  • 13. char literals char literals • char type is a numeric type, which means the arithmetic operations are allowed. • Allowed: ’B’, ’.’, ’u5469’, • Special characters: ’n’, ’t’, ’’, ’”, ’”’
  • 14. Floating point literals • Floating-point literals (float, double) • Allowed: 6.67 0.1e+3f 7e5 4.5E-19 1E-9f 1e4D • Allowed but not recommended: 3. .76 float height = 0.158; //error: incompatible types float height = 0.158f; float height = 1.58E-1f;
  • 15. • int a=012; • int b=23,120; • int c=23 120; • byte d=130; • long e=23120l; • double f=1e-2; • float g = 1.02; • float g = 1.02f; • char h = "B"; • char h = ’B’; • char i = ’u0034’; • boolean j = TRUE; • boolean j = "true"; • boolean j = true; //correct, but ’a’ equals to 10 //error //correct //error: incompatible types //correct, ’L’ is recommended //correct //error: incompatible types //correct //error: incompatible types //correct //correct //error //error //correct
  • 16. Constants A constant refers to a data item which cannot be changed. • The keyword final must be used in the declaration of a constant. • E.g., final String MY_MOTTO = "I was struggling to begin, but in the end I overcome and nail it!"; Naming conventions: • Variables: mixed case, lower case first letter • E.g., age, buttonColor, dateDue • Constants: all upper case, words separated by underscore • E.g., PI, MIN VALUE, MAX VALUE
  • 17. Challenge • Exercise: Write a program (CircleComputation.java) which prints on the command-line the area and circumference of a circle, given its radius (1.5). Hint: final double PI = 3.14159265;
  • 18. Casting primitive types • It is possible to convert one numeral type to another numeral type, refers to casting (or: primitive conversion). • Widening casting (Implicit done) • short a=20; • long b=a; • int c=20; • float d=c; • Narrowing casting (Explicitly done) • long a = 20; • short b = (short) a; • float c = 20; • int d = (int) c; • float hight = (float) 0.158;
  • 19. Overflow on casting (be careful) • Overflow: Operations with integer types may produce numbers which are too big to be stored in the primitive types you have allocated. byte d=130; //error: incompatible types byte d= (byte)130; //no error, but d’s value is -126, because of overflow • NOTE: No error message for overflow will be raised, you’ll simply get an unexpected number.
  • 20. Learning resources The workshop homepage https://mcollison.github.io/JPMC-java-intro-2021/ The course materials https://mcollison.github.io/java-programming-foundations/ • Session worksheets – updated each week
  • 21. Additional resources • Think Java: How to think like a computer scientist • Allen B Downey (O’Reilly Press) • Available under Creative Commons license • https://greenteapress.com/wp/think-java-2e/ • Oracle central Java Documentation – https://docs.oracle.com/javase/8/docs/api/ • Other sources: • W3Schools Java - https://www.w3schools.com/java/ • stack overflow - https://stackoverflow.com/ • Coding bat - https://codingbat.com/java

Editor's Notes

  • #7: Computational operation not a formal assertion
  • #9: Expressions in a file 5 15  The value of another_long_name is 20
  • #11: Primitive types are the building blocks of a language. They are built-in types that enable storage of pure simple values.
  • #12: Why does a byte go from -128 to 127? Count from zero.
  • #13: 1234_5678 easier to read Octal base 8 numeral system
  • #14: Unicode encoding \ escape character
  • #21: All resources hang from the ELE pages. How many of you have looked through them?