This document provides an overview and introduction to programming in Java. It discusses Java basics like variables, primitive data types, literals, casting, and more. The document also provides examples of Java code and recommends additional learning resources for mastering Java programming.
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’, ’’, ’”, ’”’
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
Computational operation not a formal assertion
Expressions in a file
5
15
The value of another_long_name is 20
Primitive types are the building blocks of a language. They are built-in types that enable storage of pure simple values.
Why does a byte go from -128 to 127? Count from zero.
1234_5678 easier to read
Octal base 8 numeral system
Unicode encoding
\ escape character
All resources hang from the ELE pages.
How many of you have looked through them?