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

PPT
Basic elements of java
PDF
07 control+structures
PPT
Introduction to objects and inputoutput
PPTX
Lecture 2 variables
PPTX
Cs1123 4 variables_constants
PPTX
PDF
12 computer science_notes_ch01_overview_of_cpp
PPTX
java in Aartificial intelligent by virat andodariya
Basic elements of java
07 control+structures
Introduction to objects and inputoutput
Lecture 2 variables
Cs1123 4 variables_constants
12 computer science_notes_ch01_overview_of_cpp
java in Aartificial intelligent by virat andodariya

What's hot (17)

ODP
Preparing Java 7 Certifications
PPT
C++ for beginners
PPSX
C++ Programming Language
PPTX
C programming tutorial for Beginner
PPT
C++ Programming Course
PPTX
LLVM Backend Porting
PPT
Gcc porting
PPT
C tutorial
PDF
Cheat Sheet java
PDF
Fundamentals of Computing and C Programming - Part 1
PDF
Fundamentals of Computing and C Programming - Part 2
PPTX
Variables in C and C++ Language
PPTX
LLVM Instruction Selection
PPT
Token and operators
PDF
Semantic Analysis of a C Program
PPT
Chapter 2 - Basic Elements of Java
Preparing Java 7 Certifications
C++ for beginners
C++ Programming Language
C programming tutorial for Beginner
C++ Programming Course
LLVM Backend Porting
Gcc porting
C tutorial
Cheat Sheet java
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 2
Variables in C and C++ Language
LLVM Instruction Selection
Token and operators
Semantic Analysis of a C Program
Chapter 2 - Basic Elements of Java
Ad

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

PPTX
OOP-java-variables.pptx
PPTX
Review 2Pygame made using python programming
PPSX
Esoft Metro Campus - Programming with C++
PPT
Data types and Operators
PPTX
Review of C programming language.pptx...
PPTX
Cs1123 3 c++ overview
PPTX
Chapter 2: Elementary Programming
PPTX
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
PPT
ch4 is the one of biggest tool in AI.ppt
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PPTX
Fundamentals of C,Data types, Decision Structure and Loops.pptx
PPTX
C for Engineers
PPTX
Shell Programming Language in Operating System .pptx
PDF
Programming for Problem Solving Unit 2
PPTX
JAVA in Artificial intelligent
PPTX
Android webinar class_java_review
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PPT
learn JAVA at ASIT with a placement assistance.
PPT
Basic concept of c++
PPTX
PYTHON PROGRAMMING PROBLEM SOLVING PPT DOCUMENTS PPT
OOP-java-variables.pptx
Review 2Pygame made using python programming
Esoft Metro Campus - Programming with C++
Data types and Operators
Review of C programming language.pptx...
Cs1123 3 c++ overview
Chapter 2: Elementary Programming
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
ch4 is the one of biggest tool in AI.ppt
Dr.C S Prasanth-Physics ppt.pptx computer
Fundamentals of C,Data types, Decision Structure and Loops.pptx
C for Engineers
Shell Programming Language in Operating System .pptx
Programming for Problem Solving Unit 2
JAVA in Artificial intelligent
Android webinar class_java_review
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
learn JAVA at ASIT with a placement assistance.
Basic concept of c++
PYTHON PROGRAMMING PROBLEM SOLVING PPT DOCUMENTS PPT
Ad

More from mcollison (11)

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

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharma ospi slides which help in ospi learning
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Types and Its function , kingdom of life
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Basic Mud Logging Guide for educational purpose
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharma ospi slides which help in ospi learning
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Anesthesia in Laparoscopic Surgery in India
Sports Quiz easy sports quiz sports quiz
Cell Types and Its function , kingdom of life

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?