SlideShare a Scribd company logo
1 of 21
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

Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 CertificationsGiacomo Veneri
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
LLVM Backend Porting
LLVM Backend PortingLLVM Backend Porting
LLVM Backend PortingShiva Chen
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Karthik Srini B R
 
Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Karthik Srini B R
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction SelectionShiva Chen
 
Semantic Analysis of a C Program
Semantic Analysis of a C ProgramSemantic Analysis of a C Program
Semantic Analysis of a C ProgramDebarati Das
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 

What's hot (17)

Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
 
C++ for beginners
C++ for beginnersC++ for beginners
C++ for beginners
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
LLVM Backend Porting
LLVM Backend PortingLLVM Backend Porting
LLVM Backend Porting
 
Gcc porting
Gcc portingGcc porting
Gcc porting
 
C tutorial
C tutorialC tutorial
C tutorial
 
Cheat Sheet java
Cheat Sheet javaCheat Sheet java
Cheat Sheet java
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1
 
Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
Semantic Analysis of a C Program
Semantic Analysis of a C ProgramSemantic Analysis of a C Program
Semantic Analysis of a C Program
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
Automata Theory
Automata TheoryAutomata Theory
Automata Theory
 

Similar to Java Basics Workshop: Variables, Types, and Assignment

Similar to Java Basics Workshop: Variables, Types, and Assignment (20)

OOP-java-variables.pptx
OOP-java-variables.pptxOOP-java-variables.pptx
OOP-java-variables.pptx
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
 
JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
Java introduction
Java introductionJava introduction
Java introduction
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
C basics
C basicsC basics
C basics
 
C language
C languageC language
C language
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 
Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Lec-1c.pdf
Lec-1c.pdfLec-1c.pdf
Lec-1c.pdf
 

More from mcollison

Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliabilitymcollison
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packagesmcollison
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structuresmcollison
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classesmcollison
 
Pi j1.0 workshop-introduction
Pi j1.0 workshop-introductionPi j1.0 workshop-introduction
Pi j1.0 workshop-introductionmcollison
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loopsmcollison
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operatorsmcollison
 
Pi j1.1 what-is-java
Pi j1.1 what-is-javaPi j1.1 what-is-java
Pi j1.1 what-is-javamcollison
 

More from mcollison (11)

Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Pi j1.0 workshop-introduction
Pi j1.0 workshop-introductionPi j1.0 workshop-introduction
Pi j1.0 workshop-introduction
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loops
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Pi j1.1 what-is-java
Pi j1.1 what-is-javaPi j1.1 what-is-java
Pi j1.1 what-is-java
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

Java Basics Workshop: Variables, Types, and 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

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