Programming For Non-Techies is a weekly feature for non-techies to learn to program every Friday. If you’re a non-techie and you want to learn to program, or maybe you want a job in tech and need a better understanding of programming, these posts will help you!
If you’re new here, I strongly suggest looking at the previous programming posts since they build on each other.
Variables
Variables are what we use to represent data in memory. The way we use variables depends on the language we’re using. Our examples are in Java, but it is important to understand how much this varies from language to language.
Statically Typed vs Dynamically Typed
Java is a statically typed language. That means that every variable needs a name and type at compile time. The compiler verifies that all values used are the proper type. The opposite is a dynamically typed language, such as Python.
Strongly Typed vs Weakly Typed
Java is strongly typed. The type limits how a variable can be used. It can only hold certain values based on its assigned type. Operations performed with that variable are also limited to ones specially defined by the language. We will cover operations in the future, but addition and subtraction are very common examples.
Is one better than the other?
The advantage to using a language with strong static typing is that the compiler will help you detect errors. This might take a little more time to write. You don’t have to worry about defining the variable types with weak dynamic typing, but you are on your own with errors in logic. We want you to understand the idea so it makes sense when you see other languages.
Primitives
In Java there are 8 primitive types. They are also called value types. Byte, short, int and long are all integer, whole number, values. Float and double are decimal values. A char is a single Unicode character. Boolean is either true or false. The reason there are multiple integer and floating point types has to do with efficiency. Here’s one example. A long and a byte can both be used to represent a value of 100, but a long needs 8 bytes of memory. If you choose the smallest type that will accommodate your needs you will save memory and storage space.
Here’s the technical definition of the primitive data types.
- byte – 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
- short – 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
- int – 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1.
- long – 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1.
- float – single-precision 32-bit IEEE 754 floating point.
- double – double-precision 64-bit IEEE 754 floating point.
- char – single 16-bit Unicode character. It has a minimum value of
'\u0000'
(or 0) and a maximum value of'\uffff'
(or 65,535 inclusive). - boolean – has only two possible values:
true
orfalse
.
References
A reference type refers to an object in memory. We are only going to list the 4 reference types in Java today. Each one of these will be its own lesson in the coming weeks.
- Class types
- Interface types
- Type Variables
- Array Types
Pointers
Pointers refer to an address in memory. The C and C++ languages use pointers heavily. Java does not use pointers, so we can’t give an example of this, but we will come back to it in the future.
Let’s look at Java code
We said that Java uses strong static typing. This means that every variable needs a type. Here’s how to set the name and the type of a variable:
int myInt = 0;
We just defined an int named myInt and initialized it to 0. Remember that we need a semicolon at the end of each line.
We have added variables to last’s week’s example. Try changing the types to see how the compiler enforces types.
public class ExampleProgram2 { public static void main(String[] args) { ExampleProgram2 example = new ExampleProgram2(); byte ifThen = 1; short ifThenElse = 2; int forLoop = 3; long whileLoop = 4; double doWhileLoop = 5.0; int switchStatement = 6; boolean isDoLoop = true; if(isDoLoop) { System.out.println("ifThen"); example.ifThen(ifThen); System.out.println("\nifThenElse"); example.ifThenElse(ifThenElse); System.out.println("\nforLoop"); example.forLoop(forLoop); System.out.println("\nwhileLoop"); example.whileLoop(whileLoop); System.out.println("\ndoWhileLoop"); example.doWhileLoop(doWhileLoop); System.out.println("\nswitchStatement"); example.switchStatement(switchStatement); } } private void ifThen(int x) { if (x > 5) { System.out.println("greater than 5"); } } private void ifThenElse(int x) { if (x > 5) { System.out.println("greater than 5"); } else if (x > 10) { System.out.println("greater than 10"); } else { System.out.println("5 or less"); } } private void forLoop(int x) { for (int i = 0; i < x; i++) { System.out.println("forLoop: " + i); } } private void whileLoop(long x) { while (x < 10) { System.out.println("x = " + x); x++; } } private void doWhileLoop(double x) { do { System.out.println("x = " + x); x++; } while (x < 10); } private void switchStatement(int x) { switch (x) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; case 4: System.out.println("four"); break; case 5: System.out.println("five"); break; case 6: System.out.println("six"); break; case 7: System.out.println("seven"); break; case 8: System.out.println("eight"); break; case 9: System.out.println("nine"); break; case 10: System.out.println("ten"); break; default: System.out.println("error"); break; } } }