🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

W2L1_ValuesTypesVariables_slides.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Values, Types and Variables Software Development 1 (F27SA) Michael Lones Recap Previously I showed you a simple Java program that prints something out to the screen: public class MyProgram { MyProgram.java public static void main(String[] args) {...

Values, Types and Variables Software Development 1 (F27SA) Michael Lones Recap Previously I showed you a simple Java program that prints something out to the screen: public class MyProgram { MyProgram.java public static void main(String[] args) { System.out.println("Hello World!"); } } Hello World! Terminal A Simple Expression One of the simplest things you can do in Java is calculate the result of a numerical expression: public class MyProgram { MyProgram.java public static void main(String[] args) { System.out.println( 2 + 3 ); } } 5 Terminal A Simple Expression One of the simplest things you can do in Java is calculate the result of a numerical expression: public class MyProgram { MyProgram.java public static void main(String[] args) { System.out.println( 2 * 5 + 5 ); } * means multiply by } 15 Terminal A Simple Expression One of the simplest things you can do in Java is calculate the result of a numerical expression: public class MyProgram { MyProgram.java public static void main(String[] args) { System.out.println( 4.5 / 3 + 2 ); } / means divide by } 3.5 Terminal Saving the Result Once you've calculated something, you often want to store it somewhere for later use: public class MyProgram { MyProgram.java public static void main(String[] args) { double result = 4.5 / 3 + 2; System.out.println( result ); } } 3.5 Terminal Saving the Result Once you've calculated something, you often want to store it somewhere for later use: public class MyProgram { MyProgram.java public static void main(String[] args) { double result = 4.5 / 3 + 2; System.out.println( result ); System.out.println( result ); } } 3.5 Terminal 3.5 Values A value is some entity that is manipulated by a program. Examples include: A single number, e.g. 3.5 A piece of text, e.g. "Hello world" A picture, e.g. Types Every value has a type, which is the kind of thing that a value represents: A single number, e.g. 3.5 A piece of text, e.g. "Hello world" A picture, e.g. Variables A variable is a named memory location where you can store a value Its name is usually chosen to give a more specific indicator of what it contains, e.g. aCatPicture A variable can store exactly one value In Java, every variable has a defined type, which indicates the kind of values it can contain Programming Types In programming languages, types are more specific than those we use in common language For example, types of numbers include: Integers, which have no fractional part, e.g. 100, 10, -5 Floating-point numbers, which have a fractional part, e.g. 3.5 Numbers of various sizes, e.g. small numbers, large numbers Java Types Java has two broad categories of types: Primitive types. Numbers, characters and Booleans (which contain true or false values) Object types. These are more complicated data structures held in memory. You'll come across many of these later in the course. For now, the only one you need to know about is the String type, which represents a string of characters. Numbers Java has a selection of different primitive types for representing numbers. Most common are: int : This represents an integer (a whole number) in the range -231 to 231-1. It is relatively efficient to use. double : This represents a floating-point (decimal), number. It can represent numbers very accurately, but is less efficient than using an int. Saving the Result Returning to the example: public class MyProgram { MyProgram.java public static void main(String[] args) { double result = 4.5 / 3 + 2; System.out.println( result ); } } The variable named "result" has a type of "double". This is a type that can be used to represent floating-point numbers with a relatively high degree of precision. Declaration In Java, variables must be declared before they are first used. A declaration has the form: type name; Some example declarations: int anInteger; int age; double decimalValue; double amountOwed; Declaration In Java, variables must be declared before they are first used. A declaration has the form: type name; Some example declarations: A variable can be called int anInteger; anything (with some int age; exceptions). However, double decimalValue; you should choose something informative double amountOwed; whenever possible. Declaration In Java, variables must be declared before they are first used. A declaration has the form: type name; Some example declarations: Most languages have int anInteger; naming conventions. In int age; Java, variable names begin with a lower-case double decimalValue; letter, and multiple double amountOwed; words are combined using "camelCase" Assignment You can assign a value to a variable using = age = 18; // assigns the value 18 You can assign an initial value within the variable declaration, or do this after the declaration: int age = 18; These both do int age; the same thing age = 18; Declaration with Assignment Returning to the example: public class MyProgram { MyProgram.java public static void main(String[] args) { double result = 4.5 / 3 + 2; System.out.println( result ); } } So, this is an example of declaring a variable and assigning a value to it in one line of code. Separate Declaration Returning to the example: public class MyProgram { MyProgram.java public static void main(String[] args) { double result; result = 4.5 / 3 + 2; System.out.println( result ); } } We could also have written it like this, with the declaration and assignment on separate lines Numbers public class NumberDemo { NumberDemo.java public static void main(String[] args) { int age = 18; double amountOwed = 100; System.out.println(age); System.out.println(amountOwed); } } 18 Terminal 100.0 Number Errors public class NumberDemo { NumberDemo.java public static void main(String[] args) { int age = 18.5; double amountOwed = 100; System.out.println(age); System.out.println(amountOwed); } } Type mismatch: cannot convert from double to int Terminal Number Errors public class NumberDemo { NumberDemo.java public static void main(String[] args) { int age; double amountOwed; System.out.println(age); System.out.println(amountOwed); } } The local variable age may not have been initialized Terminal The local variable amountOwed may not have been initialized Number Errors These examples illustrate a couple of things: Java won't let you assign a value to a variable if the variable is not accurate enough to fully represent it Java won't let you use a variable until it has been assigned a value, i.e. until it has been initialized A quick aside: jshell jshell allows you to execute small pieces of Java: A quick aside: jshell To run jshell (Note: requires Java SDK 1.9+): Open a terminal window – in Windows, this is the program "Command Prompt" – In macOS, this is the program "Terminal" Then type jshell and press return Alternatively, try a web version: https://tryjshell.org Casting You can use casting to change the type of a value. Casting has the syntax: (desired_type) value This has various uses when programming. One use is to force a value into a variable when the types are not compatible. Casting public class NumberDemo { NumberDemo.java public static void main(String[] args) { int age = (int) 18.5; double amountOwed = 100.0; System.out.println(age); System.out.println(amountOwed); } } 18 100.0 Note that the value 18.5 has been truncated as a result of casting Other Numeric Types Other numeric types you might come across are: byte : This is used when you only need to represent small integers in the range -128 to 127 short : This is used when you only need to represent integers in the range ±32,768 long : This is used when an int is not big enough. It can represent integers in the range -263 to 263-1 float : This can be used when you need to save memory when representing floating point numbers. It has lower precision than a double. Efficiency In general, use the most efficient type that can represent the numbers you are working with If you're using whole numbers, don't use a floating point type. If you're using floating point numbers, don't use an integer type. If you know the minimum/maximum number that a variable will contain, then consider byte, short or long. Otherwise, int is usually a safe bet. Efficiency The efficiency of a type is related to how much memory it uses, e.g. A short is 16-bit, so it uses 2 bytes of memory An int is 32-bit, so it uses 4 bytes of memory A double is 64-bit, so uses 8 bytes of memory However, memory is cheap these days, so you don't have to worry about this unless you're using loads of data, your code needs to run very fast, or your program is running on an embedded system.

Use Quizgecko on...
Browser
Browser